os.environ.get() does not return the Environment Value in windows?

守給你的承諾、 提交于 2019-12-08 01:59:38

问题


I already set SLACK_TOKEN environment Variable. But "SLACK_TOKEN=os.environ.get('SLACK_TOKEN')" is returning "None". The type of SLACK_TOKEN is NoneType. I think os.environ.get not fetching value of environment variable. so rest of the code is not executing.

import os
from slackclient import SlackClient


SLACK_TOKEN= os.environ.get('SLACK_TOKEN') #returning None
print(SLACK_TOKEN) # None
print(type(SLACK_TOKEN)) # NoneType class

slack_client = SlackClient(SLACK_TOKEN)
print(slack_client.api_call("api.test")) #{'ok': True}
print(slack_client.api_call("auth.test")) #{'ok': False, 'error': 'not_authed'}


def list_channels():
    channels_call = slack_client.api_call("channels.list")
    if channels_call['ok']:
        return channels_call['channels']
    return None

def channel_info(channel_id):
    channel_info = slack_client.api_call("channels.info", channel=channel_id)
    if channel_info:
        return channel_info['channel']
    return None

if __name__ == '__main__':
    channels = list_channels()
    if channels:
        print("Channels: ")
        for c in channels:
            print(c['name'] + " (" + c['id'] + ")")
            detailed_info = channel_info(c['id'])
            if detailed_info:
                print(detailed_info['latest']['text'])
    else:
        print("Unable to authenticate.") #Unable to authenticate

回答1:


I faced similar issue.I fixed it by removing quotes from the values. Example: I created a local.env file wherein I stored my secret key values :

*local.env:*
export SLACK_TOKEN=xxxxxyyyyyyyzzzzzzz

*settings.py:*
SLACK_TOKEN = os.environ.get('SLACK_TOKEN')

In your python terminal or console,run the command : *source local.env*

****Involve local.env in gitignore.Make sure you dont push it to git as you have to safeguard your information.

This is applicable only to the local server.Hope this helps.Happy coding :)




回答2:


In my case, I write wrong content in env file:

SLACK_TOKEN=xxxxxyyyyyyyzzzzzzz

I forgot export befor it, the correct should be:

export SLACK_TOKEN=xxxxxyyyyyyyzzzzzzz


来源:https://stackoverflow.com/questions/42978739/os-environ-get-does-not-return-the-environment-value-in-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!