「Python」os.environ

来源:互联网 发布:中建五局投资公司 知乎 编辑:程序博客网 时间:2024/05/20 20:04
Sina Weibo:小锋子Shawn
Tencent E-mail:403568338@qq.com
http://blog.csdn.net/dgyuanshaofeng/article/details/77955501

1、Python2的文档os.environ
2、下面是文档的翻译和理解:
A mapping object representing the string environment. For example, environ['HOME'] is the pathname of
your home directory (on some platforms), and is equivalent to getenv("HOME") in C.
映射对象表示字符串环境,额,不清楚。在IPython中输入os.environ,然后会显示一堆字符串。比如:
'LIB'、'PYTHONPATH'、'PATH'、'CUDA_PATH',第二个和第三个我们看到比较多。通常,我们用这行代码指定tensorflow使用指定的GPU。如下:
import os os.environ['CUDA_VISIBLE_DEVICES']='0'
指定在GPU0上训练我们的网络。因为tenforflow如果不指定GPU的话,会占用所有GPU。
后面的就不用管了。
This mapping is captured the first time the os module is imported, typically during Python startup as part of processing site.py. Changes to the environment made after this time are not reflected in os.environ, except for changes made by modifying os.environ directly.
If the platform supports the putenc() 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.
Note: Calling putenv() directly does not change os.environ, so it's better to modify os.environ.
Note: On some platforms, including FreeBSD and Mac OS X, setting environ may cause memory leaks.   Refer to the system documentation for putenv().
If putenv() is not provided, a modified copy of this mapping may be passed to the appropriate process-creation functions to cause child process to use a modified environment.
If the platform supports the unsetenc() function, you can delete items in this mapping to unset environment variables. unsetenv() will be called automatically when an item is deleted from os.environ, and when one of the pop() or clear() methods is called.
Changed in version 2.6: Also unset environment variables when calling os.environ.clear() and os.environ.pop().

原创粉丝点击