目录
国庆前来一篇。
Python解释器其实也是一个软件,运行再操作系统环境下,所以Python是受操作系统环境变量影响的。所以环境变量也是需要关注的。特别是当我们使用sys模块时,很多都必须考虑环境变量。
命令行指定变量值会覆盖环境变量值。
该变量影响python标准库的位置。
默认:库是在prefix/lib/pythonversion 和 exec_prefix/lib/pythonversion。 prefix和exec_prefix是和安装有关的目录,两个默认是/usr/local
如果PYTHONHOME设置的是单个目录,那么prefix和exec_prefix都是该目录。如果要指定两者的不同值,那么设置值就像这样的格式:prefix:exec_prefix
这个变量是影响python搜索module模块的路径。这个变量值的格式就像linux-shell的PATH。
出了是路径外,还可以是一个纯粹包含python代码文件的zip压缩包。
默认:默认搜索路径值是依赖安装目录的,一般都是prefix/lib/pythonversion
一些额外的路径,是会放在PYTHONPATH默认值的前面追加。
通过sys.path可以在代码层面修改搜索路径。
如果这个值是一个可读文件名字,那么在这个文件里的python命令将被执行,执行的时机是在python解释器打印提示信息前,在交互模式下。
利用这个我们可以改变交互模式下的提示信息。
如果设置了这个是一个非空的字符串,等价于指定了python解释器参数-O选项,如果设置的是一个整数,等价于设置了多次-O选项。
如果设置了这个,且这个值指定的是一个已点号间隔的可执行python路径。这个路径指定的模块,所包含的可执行对象都会被import,然后当在代码中用到sys.breakpointhook()时,就是在调用这个模块里的可执行对象。下面没有翻译了,能力有限:
?If not set, or set to the empty string, it is equivalent to the value “pdb.set_trace”. Setting this to the string “0” causes the default implementation of?sys.breakpointhook()?to do nothing but return immediately.
Python 3.7才有的
If this is set to a non-empty string it is equivalent to specifying the?-d?option. If set to an integer, it is equivalent to specifying?-d?multiple times.
If this is set to a non-empty string it is equivalent to specifying the?-i?option
.This variable can also be modified by Python code using?os.environ?to force inspect mode on program termination.
如果设置了该值且是一个非空字符串,等价于命令行参数加了-u选项
If this is set to a non-empty string it is equivalent to specifying the?-v?option. If set to an integer, it is equivalent to specifying?-v?multiple times.
If this is set, Python ignores case in?import?statements. This only works on Windows and OS X.
如果设置为一个非空的字符串,那么python不会去创建.pyc文件在导入源码模块。这个等价于命令行加-B选项。
如果这个值不设置或者设置为random值,那么将会使用一个随机的值作为seed值用在str,bytes,datetime对象的hash算法中。seed值影响多次hash的随机性,如果seed值相同,那么多次运行python解释器,这三个对象的相同值hash将会相同。如果时随机,那么多次运行python解释器,相同值hash也会不同。
设置固定值的目的就是为了允许可重复hash. 如解释器自测,或者允许一个python进程cluster共享hash值。
这个固定值必须是一个decimal number 在[0,4294967295]范围内。特别是的0值将会关闭hash随机性。
在解释器启动前设置,它将覆盖stdin/stdout/stderr的编码,值格式是:encodingname:errorhandler。
encodingname和:errorhandler都是可选的并且意义同str.encode
如果设置了这个那么,user site-packages directory 将不会加入到sys.path中
See also?PEP 370?– Per user site-packages directory
定义user base directory,用于计算user site-packages-directory 和 Distutils installation paths 用于python setup.py install --user 安装到用户路径。
如果设置了这个环境变量, 那么sys.argv[0] 将会被设置为这个值,
?instead of the value got through the C runtime. Only works on Mac OS X.
This is equivalent to the?-W?option.
If this environment variable is set to a non-empty string,?faulthandler.enable()?is called at startup: install a handler for?SIGSEGV,?SIGFPE,?SIGABRT,?SIGBUS?and?SIGILL?signals to dump the Python traceback. This is equivalent to?-X?faulthandler?option.
If this environment variable is set to a non-empty string, start tracing Python memory allocations using the?tracemalloc?module. The value of the variable is the maximum number of frames stored in a traceback of a trace. For example,?PYTHONTRACEMALLOC=1?stores only the most recent frame. See the?tracemalloc.start()?for more information.
If this environment variable is set to a non-empty string, Python will show how long each import takes. This is exactly equivalent to setting?-X?importtime?on the command line.
If this environment variable is set to a non-empty string, enable the?debug mode?of the?asyncio?module.
https://docs.python.org/3/using/cmdline.html#envvar-PYTHONMALLOC
https://docs.python.org/3/using/cmdline.html#envvar-PYTHONMALLOCSTATS
https://docs.python.org/3/using/cmdline.html#envvar-PYTHONLEGACYWINDOWSFSENCODING
https://docs.python.org/3/using/cmdline.html#envvar-PYTHONLEGACYWINDOWSSTDIO
https://docs.python.org/3/using/cmdline.html#envvar-PYTHONCOERCECLOCALE
https://docs.python.org/3/using/cmdline.html#envvar-PYTHONDEVMODE
输入设置为1,那么python解释器时UTF-8模式,即使当前文件设置了文件编码也会使用这个UTF-8。
也就是说:
sys.getfilesystemencoding() 返回‘UTF-8‘
locale.getpreferredencoding()
sys.stdin, sys.stdout, sys.stderr
参考点这里
If set, Python will print threading debug info.
If set, Python will dump objects and reference counts still alive after shutting down the interpreter.
没有环境变量可以指定,默认时启动程序所在路径,但是可以通过os.chdir(‘路径‘)改变。然后通过os.getcwd()获取。
原文:https://www.cnblogs.com/ZJiQi/p/11612936.html