影响Python行为的环境变量
目录
影响Python行为的环境变量国庆前来一篇。
环境变量1. PYTHONHOME该变量影响python标准库的位置。 如果PYTHONHOME设置的是单个目录,那么prefix和exec_prefix都是该目录。如果要指定两者的不同值,那么设置值就像这样的格式:prefix:exec_prefix 2. PYTHONPATH这个变量是影响python搜索module模块的路径。这个变量值的格式就像linux-shell的PATH。 默认:默认搜索路径值是依赖安装目录的,一般都是prefix/lib/pythonversion 一些额外的路径,是会放在PYTHONPATH默认值的前面追加。 通过sys.path可以在代码层面修改搜索路径。 3. PYTHONSTARTUP如果这个值是一个可读文件名字,那么在这个文件里的python命令将被执行,执行的时机是在python解释器打印提示信息前,在交互模式下。 利用这个我们可以改变交互模式下的提示信息。 4. PYTHONOPTIMIZE如果设置了这个是一个非空的字符串,等价于指定了python解释器参数-O选项,如果设置的是一个整数,等价于设置了多次-O选项。 5. PYTHONBREAKPOINT如果设置了这个,且这个值指定的是一个已点号间隔的可执行python路径。这个路径指定的模块,所包含的可执行对象都会被import,然后当在代码中用到sys.breakpointhook()时,就是在调用这个模块里的可执行对象。下面没有翻译了,能力有限: Python 3.7才有的 6. PYITHONDEBUGIf 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. 7. PYTHONINSPECTIf this is set to a non-empty string it is equivalent to specifying the?-i?option 8. PYTHONUNBUFFERED如果设置了该值且是一个非空字符串,等价于命令行参数加了-u选项 9. PYTHONVERBOSEIf 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. 10. PYTHONCASEOKIf this is set,Python ignores case in?import?statements. This only works on Windows and OS X. 11. PYTHONDONTWRITEBYTECODE如果设置为一个非空的字符串,那么python不会去创建.pyc文件在导入源码模块。这个等价于命令行加-B选项。 12. PYTHONHASHSEED如果这个值不设置或者设置为random值,那么将会使用一个随机的值作为seed值用在str,bytes,datetime对象的hash算法中。seed值影响多次hash的随机性,如果seed值相同,那么多次运行python解释器,这三个对象的相同值hash将会相同。如果时随机,那么多次运行python解释器,相同值hash也会不同。 设置固定值的目的就是为了允许可重复hash. 如解释器自测,或者允许一个python进程cluster共享hash值。 这个固定值必须是一个decimal number 在[0,4294967295]范围内。特别是的0值将会关闭hash随机性。 13. PYTHONIOENCODING在解释器启动前设置,它将覆盖stdin/stdout/stderr的编码,值格式是:encodingname:errorhandler。 14. PYTHONNOUSERSITE如果设置了这个那么,user site-packages directory 将不会加入到sys.path中 See also?PEP 370?– Per user site-packages directory 15. PYTHONUSERBASE定义user base directory,用于计算user site-packages-directory 和 Distutils installation paths 用于python setup.py install --user 安装到用户路径。 16. PYTHONEXECUTABLE如果设置了这个环境变量, 那么sys.argv[0] 将会被设置为这个值, 17. PYTHONWARNINGSThis is equivalent to the?-W?option. 18. PYTHONFAULTHANDLERIf 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. 19. PYTHONTRACEMALLOCIf 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. 20. PYTHONPROFILEIMPORTTIMEIf 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. 21. PYTHONASYNCIODEBUGIf this environment variable is set to a non-empty string,enable the?debug mode?of the?asyncio?module. 22. PYTHONMALLOChttps://docs.python.org/3/using/cmdline.html#envvar-PYTHONMALLOC 23. PYTHONMALLOCSTATShttps://docs.python.org/3/using/cmdline.html#envvar-PYTHONMALLOCSTATS 24. PYTHONLEGACYWINDOWSFSENCODINGhttps://docs.python.org/3/using/cmdline.html#envvar-PYTHONLEGACYWINDOWSFSENCODING 25. PYTHONLEGACYWINDOWSSTDIOhttps://docs.python.org/3/using/cmdline.html#envvar-PYTHONLEGACYWINDOWSSTDIO 26. PYTHONCOERCECLOCALEhttps://docs.python.org/3/using/cmdline.html#envvar-PYTHONCOERCECLOCALE 27. PYTHONDEVMODEhttps://docs.python.org/3/using/cmdline.html#envvar-PYTHONDEVMODE 28. PYTHONUTF8输入设置为1,那么python解释器时UTF-8模式,即使当前文件设置了文件编码也会使用这个UTF-8。 也就是说: 参考点这里 Debug模式下支持的环境变量PYTHONTHREADDEBUGIf set,Python will print threading debug info. PYTHONDUMPREFSIf set,Python will dump objects and reference counts still alive after shutting down the interpreter. 对于当前工作路径没有环境变量可以指定,默认时启动程序所在路径,但是可以通过os.chdir(‘路径‘)改变。然后通过os.getcwd()获取。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |