I need to set a environment variables in the python, and I try the commands bellow
我需要在python中设置一个环境变量,然后我尝试下面的命令
import os
basepath = os.putenv('CPATH','/Users/cat/doc')
basepath = os.getenv('CPATH','/Users/cat/doc')
And when I print the varible, they are not set: print basepath None
当我打印varible时,它们没有设置:print basepath None
What i'm doing wrong?
我做错了什么?
Rephrasing the question, I would like to create a base path based on a evironment variable. I'm testing this:
换句话说,我想基于环境变量创建一个基本路径。我正在测试这个:
os.environ["CPATH"] = "/Users/cat/doc"
print os.environ["CPATH"]
base_path=os.getenv('C_PATH')
And when I try to print the basepath: print basepath it always return None
当我尝试打印基道时:print basepath它总是返回None
3 个解决方案
#1
5
Try this one.
试试这个。
os.environ["CPATH"] = "/Users/cat/doc"
Python documentation is quite clear about it.
Python文档非常清楚。
Calling putenv() directly does not change os.environ, so it’s better to modify os.environ.
直接调用putenv()不会改变os.environ,所以最好修改os.environ。
Based on the documentation, the os.getenv()
is available on most flavors of Unix and Windows. OS X is not listed.
根据文档,os.getenv()可用于大多数Unix和Windows版本。 OS X未列出。
Use rather the snippet below to retrieve the value.
请使用下面的代码段来检索值。
value = os.environ.get('CPATH')
#2
2
Use os.environ
:
使用os.environ:
os.environ['CPATH'] = '/Users/cat/doc'
print os.environ['CPATH'] # /Users/cat/doc
print os.environ.get('CPATH') # /Users/cat/doc
See the above link for more details:
有关详细信息,请参阅上面的链接:
If the platform supports the putenv() function, this mapping may be used to modify the environment as well as query the environment. putenv() will be called automatically when the mapping is modified.
如果平台支持putenv()函数,则此映射可用于修改环境以及查询环境。修改映射时将自动调用putenv()。
Note Calling putenv() directly does not change os.environ, so it’s better to modify os.environ.
注意直接调用putenv()不会改变os.environ,因此最好修改os.environ。
#3
0
It's a good practice to restore the environment variables at function completion. You may need something like the modified_environ
context manager describe in this question to restore the environment variables.
在函数完成时恢复环境变量是一种很好的做法。您可能需要类似此问题中描述的modified_environ上下文管理器来恢复环境变量。
Usage example:
用法示例:
with modified_environ(CPATH='/Users/cat/doc'):
call_my_function()
#1
5
Try this one.
试试这个。
os.environ["CPATH"] = "/Users/cat/doc"
Python documentation is quite clear about it.
Python文档非常清楚。
Calling putenv() directly does not change os.environ, so it’s better to modify os.environ.
直接调用putenv()不会改变os.environ,所以最好修改os.environ。
Based on the documentation, the os.getenv()
is available on most flavors of Unix and Windows. OS X is not listed.
根据文档,os.getenv()可用于大多数Unix和Windows版本。 OS X未列出。
Use rather the snippet below to retrieve the value.
请使用下面的代码段来检索值。
value = os.environ.get('CPATH')
#2
2
Use os.environ
:
使用os.environ:
os.environ['CPATH'] = '/Users/cat/doc'
print os.environ['CPATH'] # /Users/cat/doc
print os.environ.get('CPATH') # /Users/cat/doc
See the above link for more details:
有关详细信息,请参阅上面的链接:
If the platform supports the putenv() function, this mapping may be used to modify the environment as well as query the environment. putenv() will be called automatically when the mapping is modified.
如果平台支持putenv()函数,则此映射可用于修改环境以及查询环境。修改映射时将自动调用putenv()。
Note Calling putenv() directly does not change os.environ, so it’s better to modify os.environ.
注意直接调用putenv()不会改变os.environ,因此最好修改os.environ。
#3
0
It's a good practice to restore the environment variables at function completion. You may need something like the modified_environ
context manager describe in this question to restore the environment variables.
在函数完成时恢复环境变量是一种很好的做法。您可能需要类似此问题中描述的modified_environ上下文管理器来恢复环境变量。
Usage example:
用法示例:
with modified_environ(CPATH='/Users/cat/doc'):
call_my_function()