用着学,学着用
The CPython interpreter scans the command line and the environment for various settings.
CPython implementation details: Other implementations' command line schemes may differ.
Command Line
When invoking Python, you may specify any of these options:
python [-bBdEhiIOqsSuvVWx?] [-c command | -m module-name | script | -] [args]
The most common use case is, of course, a simple invocation of a script:
python myscript.py
Interface options
The interpreter interface resembles that of the UNIX shell, but provides some additional methods of invocation:
- When called with standard input connected to a tty device, it prompts for commands and executes them until an EOF (an end-of file character, you can produce that with
Ctrl-D
on UNIX orCtrl-z, Enter
on Windows) is read. - When called with a file name argument or with a file as standard input, it reads and executes a script form that file.
- When called with a firectory name argument, it reads and executes an appropriately named script from that directory.
- When called with
-c command
, it executes the Python statement(s) given ascommand
. Herecommand
may contain multiple statements separated by newlines. Leading whitespace is significant in Python statements! - When called with
-m module-name
, the given module is located on the Python module path and executed as a script.
In non-interactive mode, the entire input is parsed before it is executed.
An intervace option terminates the list of options consumed by the interpreter, all consecutive arguments will end up in sys.argv
- note that the first element, subscript zero, is a string reflecting the program's source.
-c
: Execute the Python code in command. command can be one or more statements separated by newlines, with significant leading whitespace as in normal module code. NOTE If this option is given, the first element of sys.argv
will be "-c" and the current directory will be added to the start of sys.path
(allowing modules in that directory to be imported as top level modules).
-m
: Search sys.path for the named module and execute its contents as the __main__
module. See also: https://docs.python.org/3/using/cmdline.html#cmdoption-m
-
: Read commands from standard input (sys.stdin). If standard input is a terminal, -i
is implied. NOTE if this option is given, the first element of sys.argv
will be "-" and the current directory will be added to the start of sys.path
.
: Execute the Python code contained in script, which must be a filesystem path (absolute or relative) referring to either a Python file, a directory containing a
__main__.py
file, or a zipfile containing a __main__.py
file.
If no interface option is given, -i
is implied, sys.argv[0]
is an empty string ("") and the current directory will be added to the start of sys.path
. Also, tab-completion and history editing is automatically enabled, if available on your platform.
Generic options
-?
/-h
/--help
: Print a short desciption of all command line options.
-V
/--version
: Print the Python version number and exit.
Miscellaneous options
-b
: Issue a warning when comparing bytes
or bytearray
with str
or bytes
with int
. Issue an error when the options is given twice (-bb
).
-B
: If given, Python won't try to write .pyc
files on the import of source modules.
--check-hash-based-pycs
(default|always|never): Control the validation behavior of hash-based .pyc files. When set to default, checked and unchecked hash-based bytecode cache files are validated according to their default semantics. When set to always, all hased-based .pyc files, whether checked or unchecked, are validated against their corresponding source file. When set to never, hash-based .pyc files are not validated against their corresponding source files. Note the semantics of timestampe-based .pyc files are unaffected by this option.
-d
: Turn on parser debugging output (for expert only, depending on compilation options).
-E
: Ignore all PYTHON* enironment variables.
-i
: When a script is passed as first argument or the -c
option is used, enter interactive mode after executing the script or the command, even when sys.stdin does not appear to be a terminal. The PYTHONSTARTUP file is not read. NOTE this can be useful to inspect global variables or a stack trace when a script raises an exception.
-I
: Run Python in isolated mode. This also implies -E
and -s
. In isolated mode sys.path contains neither the script's directory nor the user's site-packages directory. All PYTHON* environment variables are ignored, too. Further restrictions may be imposed to prevent the user from injecting mallicious code.
-O
: Remove assert statements and any code conditional on the value of debug. Augment the filename for compiled files by adding .opt-1 before the .pyc extension.
-OO
: Do -O
and also discard docstrings. Augment the filename for compiled files by adding .opt-2 before the .pyc extension.
-q
: Don't display the copyright and version messages even in interactive mode.
-R
: Turn on hash randomization. This option only has an effect it the PYTHONHASHSEED environment variable is set to 0, since hash randomization is enabled by default.
-s
: Don't add the user site-packages directory to sys.path.
-S
: Disable the import of the module site and the site-dependent manipulations of sys.path that it entails. Also disable these manipulations if site is explicitly imported later (call site.main() if you want them to be triggered).
-u
: Force the stdout and stderr streams to be unbuffered. This option has no effect on the stdin stream.
-v
: Print a message each time a module is initialized, showing the place (filename or built-in module) from which it is loaded. WHen given twice (-vv
), print a message for each file that is checked for when searching for a module. Also provides information on module clenup at exit.
-W
(arg): Warning control. Details see also: https://docs.python.org/3/using/cmdline.html#cmdoption-w
-x
: Skip the first line of the source, allowing use of non-Unix forms of #!cmd
. This is intended for a DOS specific hack only.
-X
: Reserved for various implementation-specific options. See also: https://docs.python.org/3/using/cmdline.html#id5
Options you shouldn't use
-J
: Reserved for use by Jython.
Reference
官方文档:https://docs.python.org/3/using/cmdline.html