社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

为什么python无法检索环境变量的值,即使该变量在通过bash测试时具有值?[复制品]

Aviral Srivastava • 4 年前 • 345 次点击  

我想使用环境变量中的值。我把它放在 ~/.bashrc 我和file能够在新的shell和当前的shell中看到这些(显然,在 source 它)。但是,当我在python3 shell中导入相同的变量(在现有的和新的shell中)时,返回的值总是 None .

我读了很多书,发现 relatable answer 但它没有解决我的问题,或者说,我无法理解它。

我的bashrc文件是:

SLACK_URL="https://hooks.slack.com/"

我可以在终端中看到值,但在python shell中看不到:

ubuntu@ip-A.B.C.D:~$ echo $SLACK_URL 
https://hooks.slack.com/
ubuntu@ip-A.B.C.D:~$ python3
Python 3.6.8 (default, Jan 14 2019, 11:02:34) 
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.getenv('SLACK_URL')
>>> 

我希望看到实际变量的值,但是我得到了 没有 .

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40232
 
345 次点击  
文章 [ 1 ]  |  最新文章 4 年前
John Zwinck
Reply   •   1 楼
John Zwinck    4 年前

你需要 export .bashrc文件中的变量,以便其他程序可以看到它:

export SLACK_URL="https://hooks.slack.com/"

关于什么的更多信息 出口 是的,请看这里: https://askubuntu.com/questions/58814/how-do-i-add-environment-variables