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

Miscellaneous options

More than a dozen miscellaneous options are available for the python command. While most options are available in both Python 2 and Python 3, there may be some differences between versions. It is best to double-check https://docs.python.org/2.7/using/cmdline.html if questions arise (make sure to switch to the version you're using).

Each option is explained here:

  • -b, -bb: Provide a warning when comparing bytes/bytesarray with str or bytes with int. A double b will provide an error rather than a warning.
  • -B: Do not write .pyc bytecode files when importing source modules. Related to PYTHONDONTWRITEBYTECODE.
  • -d: Turn on parser debugging output. Related to PYTHONDEBUG.
  • -E: Ignore all PYTHON* environment variables, such as PYTHONDEBUG, that are set.
  • -i: When a script is the first argument to the python command, or the -c option is used, this option causes the Python interpreter to enter interactive mode after executing the script or command. This mode change occurs even if sys.stdin isn't a Terminal. This is useful when an exception is thrown and a developer needs to interactively review the stack trace.
  • -I: Run the interpreter in isolated mode (automatically implies -E and -s options as well). Isolated mode causes sys.path to not capture the script's directory or the user's site-packages directory. In addition, all PYTHON* environment variables are ignored. Additional restrictions can be employed to prevent a user from injecting malicious code into the Python program.
  • -J: Reserved for use by Jython implementation.
  • -O, -OO: Turn on basic optimizations. As mentioned in the Comparing source code to byte code recipe in Chapter 1, Working with Python Modules, this removes assert statements from the Python code. Related to PYTHONOPTIMIZE. Using -OO also removes docstrings from the code.
  • -q: Quiet mode; prevents the Python interpreter from displaying copyright and version messages, even in interactive mode. Useful when running programs that read data from remote systems and don't need that information presented.
  • -R: Irrelevant for Python 3.3 or newer. Turns on hash randomization by salting __hash__() values for str, bytes, and datetime. They are constant within an inpidual Python process, but are randomized between Python calls. Related to PYTHONHASHSEED.
  • -s: Do not add the user's site-packages directory to sys.path. This would require the user to explicitly provide the path to the desired site-packages.
  • -S: Disables importing the site module and site-dependent modifications of sys.path. Even if site is explicitly imported later, these modifications are still disabled. A call to site.main() is required to allow them.
  • -u: Forces unbuffered binary output from the stdout and stderr streams. Does not affect the text I/O layer in interactive mode or block-buffering in non-interactive mode. Related to PYTHONUNBUFFERED.
  • -v, -vv: Prints a message every time a module is initialized, indicating the location (file or built-in module) that loads it; also gives information about module cleanup when exiting. Using -vv, a message is printed every time a file is checked when searching for a module. Related to PYTHONVERBOSE.
  • -W <arg>: Controls when warnings are printed; by default, each warning is only printed once for each code line that causes the warning. Multiple -W options may be used, each with a different argument; if a warning matches more than one option, the last matching option is returned. Related to PYTHONWARNINGS.

Available arguments are:

    • ignore: Ignore all warnings
    • default: Explicitly request the default behavior, that is, print each warning once per source code line, regardless of how often the line is processed
    • all: Print a warning every time it occurs; multiple messages may be printed if a warning is triggered multiple times by the same line of code, such as within a loop
    • module: Print a warning the first time it occurs in each module
    • once: Print a warning the first time it occurs in the program
    • error: Instead of printing a warning, an exception will be raised

The warnings module can be imported into a Python program to control warnings from within the program:

  • -x: Skips the first source code line. As *NIX scripts normally have something such as #!/usr/bin/python as the first line to specify where to look for the Python environment, this option skips that line. Thus, this allows use of non-Unix #!<command> formats.
  • -X <value>: Reserved for implementation-specific options, as well as for passing arbitrary values and retrieving them via the sys._xoptions dictionary.

    Currently, the following values are defined:
    • faulthandler: Enables the faulthandler module, which dumps Python tracebacks when there are program errors.
    • showrefcount: Only works when debugging. Outputs the total reference count and number of used memory blocks when a program finishes or after each interactive session statement.
    • tracemalloc: Starts tracing Python memory allocations via the tracemalloc module. By default, the most recent frame is stored in the traceback.
    • showalloccount: When a program finishes, the total count of allocated objects for each type is returned. Only works when COUNT_ALLOCS is defined when Python is built.
主站蜘蛛池模板: 巫溪县| 沈阳市| 郓城县| 汉沽区| 凤山市| 罗平县| 胶州市| 连云港市| 池州市| 双峰县| 旌德县| 宁陵县| 应城市| 武隆县| 湘西| 鄂伦春自治旗| 江门市| 高阳县| 莒南县| 花莲县| 呼和浩特市| 高雄县| 兴业县| 荥经县| 冷水江市| 山丹县| 乌鲁木齐市| 仪陇县| 罗江县| 仙桃市| 云霄县| 海兴县| 成武县| 原阳县| 盐源县| 开鲁县| 措勤县| 嵊泗县| 永川市| 南雄市| 百色市|