问题描述
我已经设置了SLACK_TOKEN环境变量.但是"SLACK_TOKEN=os.environ.get('SLACK_TOKEN')"
返回无". SLACK_TOKEN 的类型为 NoneType .我认为os.environ.get无法获取环境变量的值.因此其余代码无法执行.
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
推荐答案
我遇到了类似的问题.我通过从值中删除引号来解决了该问题.例子:我创建了一个local.env文件,其中存储了我的密钥值:
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')
在您的python终端或控制台中,运行命令:*source local.env*
In your python terminal or console,run the command : *source local.env*
****将local.env包含在gitignore中.请确保不要将其推送到git中,以保护您的信息.
****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 :)
这篇关于os.environ.get()不会在Windows中返回环境值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!