• 影响Python行为的环境变量


    影响Python行为的环境变量


    国庆前来一篇。

    Python解释器其实也是一个软件,运行再操作系统环境下,所以Python是受操作系统环境变量影响的。所以环境变量也是需要关注的。特别是当我们使用sys模块时,很多都必须考虑环境变量。
    命令行指定变量值会覆盖环境变量值。

    环境变量

    1. PYTHONHOME

    该变量影响python标准库的位置。
    默认:库是在prefix/lib/pythonversion 和 exec_prefix/lib/pythonversion。 prefix和exec_prefix是和安装有关的目录,两个默认是/usr/local

    如果PYTHONHOME设置的是单个目录,那么prefix和exec_prefix都是该目录。如果要指定两者的不同值,那么设置值就像这样的格式:prefix:exec_prefix

    2. PYTHONPATH

    这个变量是影响python搜索module模块的路径。这个变量值的格式就像linux-shell的PATH。
    出了是路径外,还可以是一个纯粹包含python代码文件的zip压缩包。

    默认:默认搜索路径值是依赖安装目录的,一般都是prefix/lib/pythonversion

    一些额外的路径,是会放在PYTHONPATH默认值的前面追加。

    通过sys.path可以在代码层面修改搜索路径。

    3. PYTHONSTARTUP

    如果这个值是一个可读文件名字,那么在这个文件里的python命令将被执行,执行的时机是在python解释器打印提示信息前,在交互模式下。

    利用这个我们可以改变交互模式下的提示信息。

    4. PYTHONOPTIMIZE

    如果设置了这个是一个非空的字符串,等价于指定了python解释器参数-O选项,如果设置的是一个整数,等价于设置了多次-O选项。

    5. PYTHONBREAKPOINT

    如果设置了这个,且这个值指定的是一个已点号间隔的可执行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才有的

    6. PYITHONDEBUG

    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.

    7. PYTHONINSPECT

    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.

    8. PYTHONUNBUFFERED

    如果设置了该值且是一个非空字符串,等价于命令行参数加了-u选项

    9. PYTHONVERBOSE

    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.

    10. PYTHONCASEOK

    If 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。
    encodingname和:errorhandler都是可选的并且意义同str.encode

    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-directoryDistutils installation paths 用于python setup.py install --user 安装到用户路径。

    16. PYTHONEXECUTABLE

    如果设置了这个环境变量, 那么sys.argv[0] 将会被设置为这个值,
     instead of the value got through the C runtime. Only works on Mac OS X.

    17. PYTHONWARNINGS

    This is equivalent to the -W option.

    18. PYTHONFAULTHANDLER

    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.

    19. PYTHONTRACEMALLOC

    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.

    20. PYTHONPROFILEIMPORTTIME

    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.

    21. PYTHONASYNCIODEBUG

    If this environment variable is set to a non-empty string, enable the debug mode of the asyncio module.

    22. PYTHONMALLOC

    https://docs.python.org/3/using/cmdline.html#envvar-PYTHONMALLOC

    23. PYTHONMALLOCSTATS

    https://docs.python.org/3/using/cmdline.html#envvar-PYTHONMALLOCSTATS

    24. PYTHONLEGACYWINDOWSFSENCODING

    https://docs.python.org/3/using/cmdline.html#envvar-PYTHONLEGACYWINDOWSFSENCODING

    25. PYTHONLEGACYWINDOWSSTDIO

    https://docs.python.org/3/using/cmdline.html#envvar-PYTHONLEGACYWINDOWSSTDIO

    26. PYTHONCOERCECLOCALE

    https://docs.python.org/3/using/cmdline.html#envvar-PYTHONCOERCECLOCALE

    27. PYTHONDEVMODE

    https://docs.python.org/3/using/cmdline.html#envvar-PYTHONDEVMODE

    28. PYTHONUTF8

    输入设置为1,那么python解释器时UTF-8模式,即使当前文件设置了文件编码也会使用这个UTF-8。

    也就是说:
    sys.getfilesystemencoding() 返回'UTF-8'
    locale.getpreferredencoding()
    sys.stdin, sys.stdout, sys.stderr

    参考点这里

    Debug模式下支持的环境变量

    PYTHONTHREADDEBUG

    If set, Python will print threading debug info.

    PYTHONDUMPREFS

    If set, Python will dump objects and reference counts still alive after shutting down the interpreter.

    对于当前工作路径

    没有环境变量可以指定,默认时启动程序所在路径,但是可以通过os.chdir('路径')改变。然后通过os.getcwd()获取。

  • 相关阅读:
    Redhat6 —— Yum详解
    jQuery——bind绑定多事件用法
    织梦之路——织梦自由列表页分页链接绝对路径化(SEO)
    PHP算法——等宽等像素值截取字符串
    PHP算法——文件大小排序(KB、MB、GB、TB)
    HDU 3647 Tetris
    HDU 2442 Bricks
    ZOJ 2112 Dynamic Rankings
    HDU 3001 Travelling
    HDU 3559 Frost Chain
  • 原文地址:https://www.cnblogs.com/ZJiQi/p/11612936.html
Copyright © 2020-2023  润新知