影响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()获取。

影响Python行为的环境变量的更多相关文章

  1. python安装和环境变量的配置

    python安装和环境变量的配置 研究生阶段学习的需求,简单的学习了python的语法和基础之后产生了兴趣,有了想从基础把python学好用好的想法.因此在忙碌的学习中抽出时间,在每天花几个小时学习p ...

  2. Python安装与环境变量

    Python安装与环境变量的配置  python下载: Python安装包下载地址:http://www.python.org/ 根据实际的操作系统,安装合适的安装版本.    Python安装: 本 ...

  3. Windows下的Python安装与环境变量的配置

    Windows下的Python安装与环境变量的配置 第一步:python下载: Python安装包下载地址:http://www.python.org/ 第二步:python安装: 双击下载包,进入P ...

  4. 【转】python 修改os环境变量

    举一个很简单的例子,如果你发现一个包或者模块,明明是有的,但是会发生这样的错误: >>> from algorithm import *Traceback (most recent ...

  5. 第一篇 Python安装与环境变量的配置

    开发语言有很多种,为什么选Python? 先对各种开发语言做个初识和分类如下:高级语言:Python Java.PHP C# Go ruby C++... ---> 字节码低级语言:C.汇编 - ...

  6. 转摘Python安装与环境变量的配置

    Python安装与环境变量的配置   python下载: Python安装包下载地址:http://www.python.org/ 根据实际的操作系统,安装合适的安装版本. Python安装: 本文以 ...

  7. linux设置python虚拟环境的环境变量

    针对 linux系统中 python虚拟环境 设置环境变量 2种方法: 1.在建好的虚拟环境的 venv/bin/active 文件中,写入需要的环境变量,再进入虚拟环境: 如 配置文件路径 JERR ...

  8. Python安装与环境变量配置 入门详解 - 精简归纳

    Python安装与环境变量配置 入门详解 - 精简归纳 JERRY_Z. ~ 2020 / 9 / 24 转载请注明出处!️ 目录 Python安装与环境变量配置 入门详解 - 精简归纳 一.下载Py ...

  9. 【python之路1】python安装与环境变量配置

    直接搜索 Python,进入官网,找到下载,根据个人电脑操作系统下载相应的软件.小编的是windows os .下载python-2.7.9.msi 安装包  双击安装程序,进入安装步骤.在安装过程中 ...

随机推荐

  1. 残差residual VS 误差 error

    https://blog.csdn.net/jmydream/article/details/8764869 In statistics and optimization, statistical e ...

  2. Layui 文件上传 附带data数据

    配置项中增加参数: , data: { CaseId: function () { return $("#CaseId option:selected").val(); }, Ca ...

  3. Linux的开机启动流程

    Linux的开机启动流程 1.开机BIOS自检                                             --> 检查CPU,硬盘等硬件信息 2.MBR[Major ...

  4. Xamarin.Forms移动开发系列3:项目剖析

    摘要 本文主要进行Xamarin.Forms应用程序剖析. 前言 本文介绍Xamarin.Forms应用程序剖析. 由于本系列重点研究对象为Xamarin.Forms,所以对Xamarin.Andro ...

  5. [LeetCode] 289. Game of Life 生命游戏

    According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...

  6. 关于Dev-C++用户所必须知道的知识

    开启这几个警告选项,才能拥有GCC编译器的较为全面的报错和警告 另外,目前Dev-C早已停止更新(停滞在版本号5.1,内置的GCC为4.9版本),且使用其检测数组变量也挺麻烦的,实在不推荐使用.

  7. shell 中 贪婪匹配 和 非贪婪匹配

    举个栗子: v=jfedu.shiyiwen.com echo ${v%.*} 一个%为非贪婪匹配,即匹配最短结果.%从右到左进行非贪婪匹配,匹配什么呢? 匹配已 .*的字符. 那么当然是匹配到了.c ...

  8. sql 自动增加排序 并且初始值是000001

    declare @co_num  int,                @CoNum varchar(6) select co_num=count(*)+1 from tab             ...

  9. IntelliJ IDEA 提交代码时出现:Code analysis failed with exception: com.intellij.psi......

    IntelliJ IDEA 提交代码时出现:Code analysis failed with exception: com.intellij.psi...... 错误原因: 当我们勾选Perform ...

  10. 一个简单 System.Threading.Tasks.Dataflow.BufferBlock 示例

    直接贴代码了: using System; using System.Threading.Tasks; using System.Threading.Tasks.Dataflow; namespace ...