.bash_profile中是否有特定的存储环境变量的格式?
Is there a specific format of storing environment variables in .bash_profile?
以下是我用来向 slack 发送消息的 python 代码。当我尝试从环境中获取 api_key 时,以下代码会引发错误,但是当我将 webhook
替换为实际的 API 键时,它工作得很好。
import requests
import json
import os
data = {
"text" : "hi there"
}
webhook = os.environ.get("SLACK_API_KEY")
requests.post(webhook, json.dumps(data))
SLACK_API_KEY 是一个环境变量,我存储在我系统的 .bash_profile 文件夹中. API 键具有以下格式:
https://hooks.slack.com/services/alpha_numeric/alpha_numeric/alpha_numeric
这就是我将 API 密钥存储在我的 .bash_profile 文件夹中的方式:
export SLACK_API_KEY="https://hooks.slack.com/services/alpha_numeric/alpha_numeric/alpha_numeric"
这是我尝试从环境中导出 api_key 时的错误。
Traceback (most recent call last):
File "/Users/nikhilsawal/OneDrive/investment_portfolio/helper_functions.py", line 10, in <module>
requests.post(webhook, json.dumps(data))
File "/Users/nikhilsawal/OneDrive/investment_portfolio/track_proj/lib/python3.8/site-packages/requests/api.py", line 119, in post
return request('post', url, data=data, json=json, **kwargs)
File "/Users/nikhilsawal/OneDrive/investment_portfolio/track_proj/lib/python3.8/site-packages/requests/api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "/Users/nikhilsawal/OneDrive/investment_portfolio/track_proj/lib/python3.8/site-packages/requests/sessions.py", line 516, in request
prep = self.prepare_request(req)
File "/Users/nikhilsawal/OneDrive/investment_portfolio/track_proj/lib/python3.8/site-packages/requests/sessions.py", line 449, in prepare_request
p.prepare(
File "/Users/nikhilsawal/OneDrive/investment_portfolio/track_proj/lib/python3.8/site-packages/requests/models.py", line 314, in prepare
self.prepare_url(url, params)
File "/Users/nikhilsawal/OneDrive/investment_portfolio/track_proj/lib/python3.8/site-packages/requests/models.py", line 388, in prepare_url
raise MissingSchema(error)
requests.exceptions.MissingSchema: Invalid URL 'None': No schema supplied. Perhaps you meant http://None?
[Finished in 0.149s]
您的环境变量是否正确存储,要检查您的环境变量,您可以在 shell 中执行 :echo $SLACK_API_KEY
或者您可以执行 env
并在其后执行 grep。
注意 ~/.bash_profile
如果 user-specific 设置和操作(仅登录 shells)或者有 .bashrc
执行交互 non-login shells.
我建议将您的变量放在 ~/.profile
中,然后不要忘记 source ~/.profile
或您决定放置环境变量的任何地方。
如果它们不存在,您可以创建它们,我推荐这个post,它解释了差异。
您也可以直接从脚本中设置变量:
os.environ["SLACK_API_KEY"] = "value"
用 ~/.bash_profile
对你的情况做了一个小测试:
以下是我用来向 slack 发送消息的 python 代码。当我尝试从环境中获取 api_key 时,以下代码会引发错误,但是当我将 webhook
替换为实际的 API 键时,它工作得很好。
import requests
import json
import os
data = {
"text" : "hi there"
}
webhook = os.environ.get("SLACK_API_KEY")
requests.post(webhook, json.dumps(data))
SLACK_API_KEY 是一个环境变量,我存储在我系统的 .bash_profile 文件夹中. API 键具有以下格式:
https://hooks.slack.com/services/alpha_numeric/alpha_numeric/alpha_numeric
这就是我将 API 密钥存储在我的 .bash_profile 文件夹中的方式:
export SLACK_API_KEY="https://hooks.slack.com/services/alpha_numeric/alpha_numeric/alpha_numeric"
这是我尝试从环境中导出 api_key 时的错误。
Traceback (most recent call last):
File "/Users/nikhilsawal/OneDrive/investment_portfolio/helper_functions.py", line 10, in <module>
requests.post(webhook, json.dumps(data))
File "/Users/nikhilsawal/OneDrive/investment_portfolio/track_proj/lib/python3.8/site-packages/requests/api.py", line 119, in post
return request('post', url, data=data, json=json, **kwargs)
File "/Users/nikhilsawal/OneDrive/investment_portfolio/track_proj/lib/python3.8/site-packages/requests/api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "/Users/nikhilsawal/OneDrive/investment_portfolio/track_proj/lib/python3.8/site-packages/requests/sessions.py", line 516, in request
prep = self.prepare_request(req)
File "/Users/nikhilsawal/OneDrive/investment_portfolio/track_proj/lib/python3.8/site-packages/requests/sessions.py", line 449, in prepare_request
p.prepare(
File "/Users/nikhilsawal/OneDrive/investment_portfolio/track_proj/lib/python3.8/site-packages/requests/models.py", line 314, in prepare
self.prepare_url(url, params)
File "/Users/nikhilsawal/OneDrive/investment_portfolio/track_proj/lib/python3.8/site-packages/requests/models.py", line 388, in prepare_url
raise MissingSchema(error)
requests.exceptions.MissingSchema: Invalid URL 'None': No schema supplied. Perhaps you meant http://None?
[Finished in 0.149s]
您的环境变量是否正确存储,要检查您的环境变量,您可以在 shell 中执行 :echo $SLACK_API_KEY
或者您可以执行 env
并在其后执行 grep。
注意 ~/.bash_profile
如果 user-specific 设置和操作(仅登录 shells)或者有 .bashrc
执行交互 non-login shells.
我建议将您的变量放在 ~/.profile
中,然后不要忘记 source ~/.profile
或您决定放置环境变量的任何地方。
如果它们不存在,您可以创建它们,我推荐这个post,它解释了差异。
您也可以直接从脚本中设置变量:
os.environ["SLACK_API_KEY"] = "value"
用 ~/.bash_profile
对你的情况做了一个小测试: