【Python】Python3.7.3-Pythonコマンドラインパラメータの詳細

27333 ワード

文書ディレクトリ
  • Pythonコマンドラインパラメータ概要
  • -c cmdパラメータ例
  • -m modパラメータ例
  • fileパラメータ例
  • -パラメータ例
  • コマンドラインオプション詳細
  • -bオプション
  • -Bオプション
  • -dオプション
  • -Eオプション
  • -h/-?/--helpオプション
  • -iオプション
  • -Iオプション
  • -Oおよび-Oオプション
  • -qオプション
  • -sオプション
  • -Sオプション
  • -uオプション
  • -vオプション
  • -V/--versionオプション
  • -Wargオプション
  • -xオプション
  • -X optオプション
  • --check-hash-based-pycs always|default|neverオプション
  • 参照
  • Pythonコマンドラインパラメータの概要
    -helpオプションを使用して、Python 3.7.3でサポートされているすべてのコマンドラインパラメータをリストします.
    C:\Users\tsu5>py -3 --help
    usage: C:\Python\Python3.7.3\python.exe [option] ... [-c cmd | -m mod | file | -] [arg] ...
    

    python.exeの後ろについてもいいです
  • [option]...:複数のオプションがあり、各オプションについては後述する章で詳細に説明します.
  • [-c cmd|-m mod|file|-][arg]...:実行するPythonプログラムおよび可能なパラメータを様々に指定
  • [-c cmd]:Python文を実行して
  • を終了
  • [-m mod]:ライブラリモジュールをスクリプトとして実行する(他のオプションは-mの前に置く必要がある)
  • オプションリストも終了する.
  • [file]:Pythonスクリプト
  • を実行
  • [-]:Python実行後、インタラクティブプロンプトインタフェースが表示され、標準入力からPython文が読み出され、実行される.
  • [arg]...:指定された1つ以上のargは、Pythonスクリプト/ライブラリモジュール
  • にパラメータとして渡されます.

    -c cmdパラメータの例
    私はよくこのような方法でPythonを簡単な計算機と見なしています.
    C:\Users\tsu5>py -3  -c "print(1080/24)"
    45.0
    

    -m modパラメータの例
    C:\Users\tsu5>py.exe -3 -m pydoc
    pydoc - the Python documentation tool
    ...
    
    C:\Users\tsu5>py.exe -3 -m unittest
    ----------------------------------------------------------------------
    Ran 0 tests in 0.000s
    OK
    
    python -m venvを使用して仮想環境を作成することもできます.詳細は、pyvenvが古いことを参照してください.python-m venvコマンドの使用
    fileパラメータの例
    (py3.7_env) tony@ubtu-nas918:~$ cat hello3.py
    #!/usr/bin/python3
    
    print("Hello, Python!")
    
    (py3.7_env) tony@ubtu-nas918:~$ python hello3.py
    Hello, Python!
    

    -パラメータの例
    (py3.7_env) tony@ubtu-nas918:~$ python - <<EOF
    > print("Hello, Standard in!")
    > EOF
    Hello, Standard in!
    

    または省略-パラメータ
    (py3.7_env) tony@ubtu-nas918:~$ python <<EOF
    print("Hello, Standard in!")
    EOF
    
    Hello, Standard in!
    

    コマンドラインオプションの詳細
    -bオプション
    -b     : issue warnings about str(bytes_instance), str(bytearray_instance)
             and comparing bytes/bytearray with str. (-bb: issue errors)
    

    str(bytes_instance)、str(bytearray_instance)、bytes/bytearrayをstrと比較した場合に警告メッセージが生成されます.-bbオプションを使用すると、エラーメッセージが生成されます.
    次は同じPythonスクリプトです.-bオプションは指定されていません.-bオプションを指定します.-bbオプションを指定します.実行効果です.
    # mybytes.py  :   bytes  ,            。
    (py3.7_env) tony@ubtu-nas918:~$ cat mybytes.py
    
    my_bytes=b'0\x300\x31'
    print("my_bytes type is: " + str(type(my_bytes)))
    print(str(my_bytes))
    
    #    -b  ,python           
    (py3.7_env) tony@ubtu-nas918:~$ python mybytes.py
    my_bytes type is: <class 'bytes'>
    b'0001'
    
    #   -b  ,python          ,      ,      
    (py3.7_env) tony@ubtu-nas918:~$ python -b mybytes.py
    my_bytes type is: <class 'bytes'>
    --->      :mybytes.py:4: BytesWarning: str() on a bytes instance
    --->      :  print(str(my_bytes))
    b'0001'
    
    #   -bb  ,python      ,      。
    (py3.7_env) tony@ubtu-nas918:~$ python -bb mybytes.py
    my_bytes type is: <class 'bytes'>
    --->      :Traceback (most recent call last):
    --->      :  File "mybytes.py", line 4, in <module>
    --->      :    print(str(my_bytes))
    --->      :BytesWarning: str() on a bytes instance
    
    

    -Bオプション
    -B     : don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x
    

    Pythonスクリプトをインポートするときは保存しません.pycファイル;環境変数PYTHONDONTWRITEBYTECODE=xに相当
    次の例では、Bオプションが指定されていない場合には、_main__.pyスクリプトの場合、現在のディレクトリの下に__が作成されます.pycache__生成するサブディレクトリをそのディレクトリの下に保存する.pycファイル.
    (py3.7_env) tony@ubtu-nas918:~$ cat __main__.py
    print("Hello, Python!")
    
    (py3.7_env) tony@ubtu-nas918:~$ python .
    Hello, Python!
    
    #    __pycache__   
    (py3.7_env) tony@ubtu-nas918:~$ ls
    __main__.py __pycache__/ 
    
    #    .pyc  :
    (py3.7_env) tony@ubtu-nas918:~$ ls -l __pycache__
    total 4
    -rw-rw-r-- 1 tony tony 128 Mar 29 14:13 __main__.cpython-37.pyc 
    

    Pythonスクリプトの実行時に-Bオプションが指定すると、このスクリプトのみが実行され、保存されません.pycファイル.環境変数PYTHONDONTWRITEBYTECODE=xが定義も保存されない.pycファイル.
    (py3.7_env) tony@ubtu-nas918:~/opt-B$ cat __main__.py
    print("Hello, Python!")
    
    #   -B  ,     __pycache__    .pyc  
    (py3.7_env) tony@ubtu-nas918:~/opt-B$ python -B .
    Hello, Python!
    
    #     __pycache__  
    (py3.7_env) tony@ubtu-nas918:~/opt-B$ ls
    __main__.py
    
    #       
    (py3.7_env) tony@ubtu-nas918:~$ export PYTHONDONTWRITEBYTECODE=x
    
    #    -B  ,         
    (py3.7_env) tony@ubtu-nas918:~/opt-B$ python .
    Hello, Python!
    
    #       __pycache__    .pyc  
    (py3.7_env) tony@ubtu-nas918:~/opt-B$ ls
    __main__.py
    

    -dオプション
    -d     : debug output from parser; also PYTHONDEBUG=x
    

    parserによって生成されたデバッグ情報を印刷します.環境変数PYTHONDEBUG=xに相当
    -Eオプション
    -E     : ignore PYTHON* environment variables (such as PYTHONPATH)
    

    PYTHON*環境変数を無視(PYTHONPATHなど)
    -h/-?/--helpオプション
    -h     : print this help message and exit (also --help)
    

    ヘルプ情報を印刷して終了します.(ヘルプもサポートされています.)
    -iオプション
    -i     : inspect interactively after running script; forces a prompt even
             if stdin does not appear to be a terminal; also PYTHONINSPECT=x
    

    スクリプトを実行した後、インタラクティブなチェックに入ります.標準入力が端末でなくても、プロンプトが表示されます.環境変数PYTHONINSPECT=xに相当します.
    #  Python       ,   >>>   ,         python  
    (py3.7_env) tony@ubtu-nas918:~$ python -i hello3.py
    Hello, world
    Hello, Python!
    10
    >>> print(s)
    Hello, world
    >>>
    

    -Iオプション
    -I     : isolate Python from the user's environment (implies -E and -s)
    

    Pythonをユーザーの環境から隔離する(Eと-sオプションを隠す)
    -Oと-Oオプション
    -O     : remove assert and __debug__-dependent statements; add .opt-1 before
             .pyc extension; also PYTHONOPTIMIZE=x
    -OO    : do -O changes and also discard docstrings; add .opt-2 before
             .pyc extension
    

    -O:assertと_を削除debug__依存文保存されています.pycファイル拡張子の前に、追加します.opt-1文字;PYTHONOPTMIZE=x-OOに等しい:-Oの動作を実行し、docstringsをさらに失う.はい.pycファイル拡張子の前に、追加します.opt-2文字
    (py3.7_env) tony@ubtu-nas918:~/opt-B$ python -O .
    Hello, Python!
    
    (py3.7_env) tony@ubtu-nas918:~/opt-B$ python -OO .
    Hello, Python!
    
    #    .pyc      ,   .opt-1 .opt-2
    (py3.7_env) tony@ubtu-nas918:~/opt-B$ ls __pycache__/
    __main__.cpython-37.opt-1.pyc  __main__.cpython-37.opt-2.pyc  __main__.cpython-37.pyc
    

    -qオプション
    -q     : don't print version and copyright messages on interactive startup
    

    インタラクティブ起動時にバージョンと著作権情報を印刷しない
    #    -q  ,     ,          
    (py3.7_env) tony@ubtu-nas918:~/opt-B$ python
    Python 3.7.3 (default, Mar 26 2019, 01:59:45)
    [GCC 5.4.0 20160609] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    
    #   -q  ,         >>>
    (py3.7_env) tony@ubtu-nas918:~/opt-B$ python -q
    >>>
    

    -sオプション
    -s     : don't add user site directory to sys.path; also PYTHONNOUSERSITE
    

    sys.pathにサイトディレクトリを追加します.PYTHONNOUSERSITEに等しい.
    -Sオプション
    -S     : don't imply 'import site' on initialization
    

    初期化時に「import site」を実行しないでください.テストしてみると効果は
    #   -S  
    tony@ubtu-nas918:~/opt-B$ python3.7 -S
    Python 3.7.3 (default, Mar 26 2019, 01:59:45)
    [GCC 5.4.0 20160609] on linux
    >>> help(quit) #   help  
    Traceback (most recent call last):
      File "", line 1, in <module>
    NameError: name 'help' is not defined
    >>> quit() #   quit()  
    Traceback (most recent call last):
      File "", line 1, in <module>
    NameError: name 'quit' is not defined
    >>> import sys
    >>> sys.path # sys.path      
    ['', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload']
    >>>  #   Python
    
    #       sys.path  
    tony@ubtu-nas918:~/opt-B$ python3.7
    Python 3.7.3 (default, Mar 26 2019, 01:59:45)
    [GCC 5.4.0 20160609] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sys
    >>> sys.path
    ['', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload', '/usr/local/lib/python3.7/dist-packages', '/usr/lib/python3/dist-packages']
    

    -uオプション
    -u     : force the stdout and stderr streams to be unbuffered;
             this option has no effect on stdin; also PYTHONUNBUFFERED=x
    

    強制標準出力stdoutと標準入力stderrストリームはバッファなしである.このオプションは標準入力stdinに無効です.環境変数PYTHONONONONBUFFERED=xに相当
    -vオプション
    -v     : verbose (trace import statements); also PYTHONVERBOSE=x
             can be supplied multiple times to increase verbosity
    

    冗長出力(import文を追跡するために使用される);PYTHONVERBOSE=xと等価に複数回使用してトラッキング冗長性を高めることができる.
    -V/--versionオプション
    -V     : print the Python version number and exit (also --version)
             when given twice, print more information about the build
    

    Pythonバージョン番号を印刷し、終了します(つまり-version).-VVを使用すると、より多くの情報が印刷されます.
    #      
    (py3.7_env) tony@ubtu-nas918:~$ python -V
    Python 3.7.3
    
    #       
    (py3.7_env) tony@ubtu-nas918:~$ python -VV
    Python 3.7.3 (default, Mar 26 2019, 01:59:45)
    [GCC 5.4.0 20160609]
    
    #    , -VV  
    (py3.7_env) tony@ubtu-nas918:~$ python -VVV
    Python 3.7.3 (default, Mar 26 2019, 01:59:45)
    [GCC 5.4.0 20160609]
    

    -Wargオプション
    -W arg : warning control; arg is action:message:category:module:lineno
             also PYTHONWARNINGS=arg
    

    警告制御;パラメータはaction:message:category:module:lineno;環境変数PYTONWARNINGS=arg argに相当する選択肢が多く、一般的には
  • ignore:すべての警告情報を無視
  • default:デフォルト警告情報
  • に明確に設定
  • all:警告情報が表示されるたびに印刷されます(注意:あるサイクルで警告が発生すると、多くの情報が生成される可能性があります)
  • .
  • module:モジュールによって生成された警告情報は、最初の発生時にのみ
  • を印刷する.
  • once:プログラムによって生成する警告情報は、1回目の発生時のみ
  • を印刷する.
  • error:警告情報をエラーとします.

  • 完全なargフォーマットは、action:message:category:module:lineです.詳細はPythonドキュメントを参照してください.
    -xオプション
    -x     : skip first line of source, allowing use of non-Unix forms of #!cmd
    

    Pythonソースファイルの最初の行を省略し、UNIX以外のフォーマットを使用できます.cmd.
    -X optオプション
    -X opt : set implementation-specific option
    

    特定のインプリメンテーションのオプションを設定します.詳細は以下を参照してください.https://docs.python.org/zh-cn/3/using/cmdline.html
    -check-hash-based-pycs always|default|neverオプション
    --check-hash-based-pycs always|default|never:
        control how Python invalidates hash-based .pyc files
    

    ただPythonはどのようにハッシュベースを使用しますか?pycファイルが無効です.3つの選択肢があります.
  • default:checkedとuncheckedのハッシュベースのバイトコードキャッシュファイルに対して、デフォルトの意味に従って有効性を検証する
  • always:ハッシュベースのすべての.pycファイルは、checkedでもuncheckedでも、対応するソースコードと比較して有効性を検証します.
  • never:ハッシュベースのすべての.pycファイルは、対応するソースコードと比較して有効性を検証しません.

  • このオプションはタイムスタンプベースには影響しません.pycファイルの有効性検証方法.
    リファレンス
    Python 3.7.3の完全なコマンドラインパラメータのリスト
    C:\Users\tsu5>py -3 --help
    usage: C:\Python\Python3.7.3\python.exe [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments (and corresponding environment variables):
    -b     : issue warnings about str(bytes_instance), str(bytearray_instance)
             and comparing bytes/bytearray with str. (-bb: issue errors)
    -B     : don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x
    -c cmd : program passed in as string (terminates option list)
    -d     : debug output from parser; also PYTHONDEBUG=x
    -E     : ignore PYTHON* environment variables (such as PYTHONPATH)
    -h     : print this help message and exit (also --help)
    -i     : inspect interactively after running script; forces a prompt even
             if stdin does not appear to be a terminal; also PYTHONINSPECT=x
    -I     : isolate Python from the user's environment (implies -E and -s)
    -m mod : run library module as a script (terminates option list)
    -O     : remove assert and __debug__-dependent statements; add .opt-1 before
             .pyc extension; also PYTHONOPTIMIZE=x
    -OO    : do -O changes and also discard docstrings; add .opt-2 before
             .pyc extension
    -q     : don't print version and copyright messages on interactive startup
    -s     : don't add user site directory to sys.path; also PYTHONNOUSERSITE
    -S     : don't imply 'import site' on initialization
    -u     : force the stdout and stderr streams to be unbuffered;
             this option has no effect on stdin; also PYTHONUNBUFFERED=x
    -v     : verbose (trace import statements); also PYTHONVERBOSE=x
             can be supplied multiple times to increase verbosity
    -V     : print the Python version number and exit (also --version)
             when given twice, print more information about the build
    -W arg : warning control; arg is action:message:category:module:lineno
             also PYTHONWARNINGS=arg
    -x     : skip first line of source, allowing use of non-Unix forms of #!cmd
    -X opt : set implementation-specific option
    --check-hash-based-pycs always|default|never:
        control how Python invalidates hash-based .pyc files
    file   : program read from script file
    -      : program read from stdin (default; interactive mode if a tty)
    arg ...: arguments passed to program in sys.argv[1:]
    
    Other environment variables:
    PYTHONSTARTUP: file executed on interactive startup (no default)
    PYTHONPATH   : ';'-separated list of directories prefixed to the
                   default module search path.  The result is sys.path.
    PYTHONHOME   : alternate  directory (or ;).
                   The default module search path uses \python{major}{minor}.
    PYTHONCASEOK : ignore case in 'import' statements (Windows).
    PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.
    PYTHONFAULTHANDLER: dump the Python traceback on fatal errors.
    PYTHONHASHSEED: if this variable is set to 'random', a random value is used
       to seed the hashes of str, bytes and datetime objects.  It can also be
       set to an integer in the range [0,4294967295] to get hash values with a
       predictable seed.
    PYTHONMALLOC: set the Python memory allocators and/or install debug hooks
       on Python memory allocators. Use PYTHONMALLOC=debug to install debug
       hooks.
    PYTHONCOERCECLOCALE: if this variable is set to 0, it disables the locale
       coercion behavior. Use PYTHONCOERCECLOCALE=warn to request display of
       locale coercion and locale compatibility warnings on stderr.
    PYTHONBREAKPOINT: if this variable is set to 0, it disables the default
       debugger. It can be set to the callable of your debugger of choice.
    PYTHONDEVMODE: enable the development mode.