简体   繁体   English

Python3不采用源命令设置的环境变量

[英]Python3 not taking environment variables set by source command

I have written a config file as follows: 我写了一个配置文件,如下所示:

bucket_name=some-bucket

I run the command source config_de_local where config_de_local is the file name in which variables to be set are stored. 我运行命令source config_de_local ,其中config_de_local是存储要设置的变量的文件名。

Then I run the command: echo $bucket_name and I get the o/p: some-bucket . 然后我运行命令: echo $bucket_name然后得到o / p: some-bucket However, the config file is still not able to import any variable, it states all as null. 但是,配置文件仍然无法导入任何变量,它全部表示为null。 This is how I am importing a variable: bucket_name = os.getenv('bucket_name', '') 这就是我导入变量的方式: bucket_name = os.getenv('bucket_name', '')

However, in the terminal, if I run the command: export bucket_name=some-bucket and then in Python3 shell, I run the line: print(os.getenv('bucket_name','')) I get the desired output. 但是,在终端中,如果运行以下命令: export bucket_name=some-bucket ,然后在Python3 shell中运行以下行: print(os.getenv('bucket_name',''))我将获得所需的输出。

Note: I am running Python3 in a virtualenv; 注意:我正在virtualenv中运行Python3。 I activate it and then set my env variables via source and export . 我激活它,然后通过sourceexport设置环境变量。

The reason that this isn't working is because un-exported environment variables will only be visible to the current process (which is Bash in your case), but not any child processes (eg Python). 之所以不起作用,是因为未导出的环境变量仅对当前进程(在您的情况下为Bash)可见,而对任何子进程(例如Python)不可见。 An export is indeed necessary or the variable could specifically be specified when running Python like this: 确实需要导出,或者可以在运行Python时特别指定该变量,如下所示:

fots ~ $ bucket_name=some-bucket python3
Python 3.7.2 (default, Jan 13 2019, 12:50:01) 
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.getenv('bucket_name', '')
'some-bucket'

In the case you wish to source them, a simple solution is to export these variables in the script that you source (which will in turn export them in your current shell). 在您希望获取它们的情况下,一个简单的解决方案是在您获取的脚本中导出这些变量(这又会将它们导出到当前shell中)。

eg 例如

fots ~ $ cat config_de_local
export bucket_name=some-bucket
fots ~ $ source config_de_local
fots ~ $ python3
Python 3.7.2 (default, Jan 13 2019, 12:50:01) 
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.getenv('bucket_name', '')
'some-bucket'

There are various ways to feed these exported environment variables to your application. 有多种方法可以将这些导出的环境变量提供给您的应用程序。 A simple shell script wrapping the program would work (as you are doing). 包装该程序的简单外壳脚本将起作用(如您所做的那样)。 This script would activate the virtualenv and then source the file. 该脚本将激活virtualenv,然后获取文件。

Alternatively, if the app is a daemon that runs in the background, creating a systemd or supervisor service would work too as you can also specify environment variables in the service definition. 另外,如果该应用程序是在后台运行的守护程序,那么创建systemd或超级用户服务也可以,因为您还可以在服务定义中指定环境变量。

Hope this helps! 希望这可以帮助!
Fotis Fotis

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM