Command Line@python

用着学,学着用


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 or Ctrl-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 as command. Here command 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 <command>: 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 <module-name>: 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.

<script>: 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

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 201,924评论 5 474
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,781评论 2 378
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 148,813评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,264评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,273评论 5 363
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,383评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,800评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,482评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,673评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,497评论 2 318
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,545评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,240评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,802评论 3 304
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,866评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,101评论 1 258
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,673评论 2 348
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,245评论 2 341

推荐阅读更多精彩内容