官术网_书友最值得收藏!

How it works...

There are a large number of Python-specific environment variables available. Some of them are:

  • PYTHONHOME: Used to change the location of the standard Python libraries. By default, libraries are searched in /usr/local/lib/<python_version>.
  • PYTHONPATH: Modifies the default search path for modules files; the format is the same as the shell's PATH.
    While directories are normally placed in the PYTHONPATH, inpidual entries can point to ZIP files that contain pure Python modules. These zipfile modules can be either source code or compiled Python files.
  • PYTHONSTARTUP: Executes Python commands in the indicated startup file before the interactive mode prompt appears. The file is executed in the same namespace as the interactive prompt, so objects defined or imported in the startup file can be used natively, that is, dot-nomenclature is not necessary.

Interactive mode prompts can be modified via this file. Specifically, the sys.ps1 (>>>) and sys.ps2 (...) prompts used in interactive mode can be changed to other symbols.

Also, the sys.__interactivehook__ hook can be modified via this file. The hook configures the rlcompleter module, which defines how Python will complete valid identifiers and keywords for the GNU readline module. In other words, the hook is responsible for setting up Python tab-completion for commands and setting the default command history file to ~/.python_history.

  • PYTHONOPTIMIZE: If set to a non-empty string, it is the same as using the -O option. If set to a string number, for example, "2", it is the same as setting -O multiple times.
  • PYTHONDEBUG: If set to a non-empty string, it is the same as using the -d option. If set to a string number, for example, "2", it is the same as setting -d multiple times.
  • PYTHONINSPECT: If set to a non-empty string, it is the same as using the -i option. This environment variable can also be modified using Python code by using the os.environ command to force inspection mode when the program ends.
  • PYTHONUNBUFFERED: When set to a non-empty string, this acts in the same way as the -u option.
  • PYTHONVERBOSE: If set to a non-empty string, it is the same as using the -v option. If set to an integer value, it is the same as setting -v multiple times.
  • PYTHONCASEOK: When set, Python will ignore character case in import statements. This is only applicable to Windows and macOS.
  • PYTHONDONTWRITEBYTECODE: When set to a non-empty string, the interpreter will not write bytecode (.pyc) files when importing source code files. This is the same functionality as using the -B option.
  • PYTHONHASHSEED: When set to random or not set at all, a random value is used to seed hash digests for str, bytes, and datetime objects. If set to an integer value, the integer will be used as the seed value for generating hashes; this allows reproducibility of results.
  • PYTHONIOENCODING: If set prior to running the interpreter, encoding is overridden for stdin, stdout, and stderr; the syntax used is encodingname:errorhandler. Both parts of the syntax are optional and have the same meaning as the str.encode() function.

    As of Python version 3.6, encoding specified by this variable is ignored on Windows when using the interactive console unless PYTHONLEGACYWINDOWSSTDIO is set.

  • PYTHONNOUSERSITE: When set, Python will not add the user site-packages directory to sys.path.
  • PYTHONUSERBASE: Defines the user base directory. The base directory is used to computer the path for site-packages and the Distutils installation paths when calling python setup.py install -user.
  • PYTHONEXECUTABLE: When set, sys.argv[0] is set to the value passed in rather than the value in the C runtime. This variable only works with macOS.
  • PYTHONWARNINGS: When set, this is the same as using the -W option; setting it to a comma-separated string is equivalent to setting multiple -Ws.
  • PYTHONFAULTHANDLER: When set to a non-empty string, during Python startup the faulthandler.enable() function is called. This is the same as using the -X faulthandler option.
  • PYTHONTRACEMALLOC: When set to a non-empty string, the tracemalloc module starts to trace Python memory allocations. The variable value specified dictates how many frames are stored in the traceback.
  • PYTHONASYNCIODEBUG: When set to a non-empty string, the asyncio module's debug mode is enabled.
  • PYTHONMALLOC: Sets Python's memory allocators, as well as installing debug hooks.

The memory allocators available include:

    • malloc: Uses the C malloc() function for all domains
    • pymalloc: Uses the pymalloc allocator for PYMEM_DOMAIN_MEM and PYMEM_DOMAIN_OBJ domains, but uses C's malloc() function for the PYMEM_DOMAIN_RAW domain

The debug hooks available include:

    • debug: Installs debug hooks on top of the default memory allocator.
    • malloc_debug: Same as malloc (previously shown), but also installs debug hooks.
    • pymalloc_debug: Same as pymalloc (previously shown), but also installs debug hooks.
  • When Python is compiled in debug mode, pymalloc_debug is set and debug hooks are used automatically. When compiled in release mode, the normal pymalloc mode is set. If neither of the pymalloc modes is available, regular malloc modes are used.
  • PYTHONMALLOCSTATS: When set to a non-empty string, Python prints the statistics for the pymalloc allocator every time a new pymalloc object is created and when the program shuts down. If pymalloc is not available, then this variable is ignored.
  • PYTHONLEGACYWINDOWSENCODING: When set, the default filesystem encoding and error mode revert to pre-3.6 version values. If using 3.6 or later, encoding is set to utf-8 and error mode is set to surrogatepass. This is only available on Windows systems.
  • PYTHONLEGACYWINDOWSTDIO: When set, the new console reader and writer are not used, causing Unicode characters to be encoded based on the active console code page rather than UTF-8. This is only available on Windows systems.
  • PYTHONTHREADDEBUG: When set, Python will print debug information for threading (only set when Python is compiled in debug mode).
  • PYTHONDUMPREFS: When set, Python will dump objects and reference counts that are still alive after shutting down the interpreter (only set when Python is compiled in debug mode).
主站蜘蛛池模板: 江永县| 池州市| 嘉兴市| 江安县| 泗阳县| 石阡县| 乌兰浩特市| 北安市| 新竹县| 宁陵县| 廉江市| 周口市| 宁都县| 永福县| 冕宁县| 临潭县| 乌苏市| 桓仁| 巢湖市| 巢湖市| 新河县| 朝阳区| 青田县| 景洪市| 孟州市| 资溪县| 深圳市| 江陵县| 临安市| 呼图壁县| 若羌县| 焦作市| 岳普湖县| 旬邑县| 罗田县| 永仁县| 扎兰屯市| 保亭| 襄樊市| 黄陵县| 苗栗县|