コマンドラインと環境
********************

CPython インタプリタはコマンドラインと環境を読み取って様々な設定を行な
います。

他の実装のコマンドラインスキームは CPython とは異なります。さらなる情
報は 別のPythonの実装 を参照してください。


コマンドライン
==============

Python を起動するとき、以下のうち任意のオプションを指定できます:

   python [-bBdEiOQsRStuUvVWxX3?] [-c command | -m module-name | script | - ] [args]

もちろん、もっとも一般的な利用方法は、単純にスクリプトを起動することで
す:

   python myscript.py


インターフェイスオプション
--------------------------

インタプリタのインターフェイスは UNIX シェルのものに似ていますが、より
多くの起動方法を提供しています:

* tty デバイスに接続された標準入力とともに起動された場合、 EOF (end-
  of-file 文字。 UNIX では "Ctrl-D" で、Windows では "Ctrl-Z, Enter"
  で入力可能) を受け取るまで、コマンドを受け取り、それを実行します。

* ファイル名引数か、標準入力としてファイルを渡された場合、そのファイ
  ル からスクリプトを読み込んで実行します。

* ディレクトリ名を引数に受け取った場合、そのディレクトリから適切な名
  前 のスクリプトファイルを読み込んで実行します。

* "-c コマンド" オプションを利用して起動された場合、 *コマンド* とし
  て 渡された Python の文を実行します。 *コマンド* の部分には改行で区
  切ら れた複数行を指定することもできます。行の先頭の空白文字は Python
  文の 重要要素です！

* "-m モジュール名" として Python モジュールパスにあるモジュールを指
  定 された場合、そのモジュールをスクリプトとして実行します。

非インタラクティブモードでは、入力の全体が実行前にパースされます。

インタプリタによって消費されるオプションリストが終了したあと、継続する
全ての引数は "sys.argv" に渡ります。 – ただし、添字 0 の先頭要素
("sys.argv[0]") はプログラムのソース自体を示す文字列です。

-c <command>

   *command* 内の Python コードを実行します。 *command* は改行によって
   区切られた1行以上の文です。通常のモジュールのコードと同じく、行頭の
   空白文字は意味を持ちます。

   このオプションが指定された場合、 "sys.argv" の最初の要素は ""-c""
   になり、カレントディレクトリが "sys.path" の先頭に追加されます (そ
   のディレクトリにあるモジュールをトップレベルモジュールとして import
   出来るようになります)。

-m <module-name>

   "sys.path" から指定されたモジュール名のモジュールを探し、その内容を
   "__main__" モジュールとして実行します。

   Since the argument is a *module* name, you must not give a file
   extension (".py").  The "module-name" should be a valid Python
   module name, but the implementation may not always enforce this
   (e.g. it may allow you to use a name that includes a hyphen).

   Package names are also permitted. When a package name is supplied
   instead of a normal module, the interpreter will execute
   "<pkg>.__main__" as the main module. This behaviour is deliberately
   similar to the handling of directories and zipfiles that are passed
   to the interpreter as the script argument.

   注釈: このオプションは組み込みモジュールや C で書かれた拡張モジュ
     ールに は利用できません。 Python モジュールファイルを持っていない
     からで す。しかし、コンパイル済みのモジュールは、たとえ元のソース
     ファイ ルがなくても利用可能です。

   If this option is given, the first element of "sys.argv" will be
   the full path to the module file. As with the "-c" option, the
   current directory will be added to the start of "sys.path".

   多くの標準ライブラリモジュールにはスクリプトとして実行された時のた
   めのコードがあります。例えば、 "timeit" モジュールは次のように実行
   可能です:

      python -mtimeit -s 'setup here' 'benchmarked code here'
      python -mtimeit -h # for details

   参考:

     "runpy.run_module()"
        Python コードで直接使える等価な機能

     **PEP 338** - モジュールをスクリプトとして実行する

   バージョン 2.4 で追加.

   バージョン 2.5 で変更: The named module can now be located inside a
   package.

   バージョン 2.7 で変更: Supply the package name to run a "__main__"
   submodule. sys.argv[0] is now set to ""-m"" while searching for the
   module (it was previously incorrectly set to ""-c"")

-

   標準入力 ("sys.stdin") からコマンドを読み込みます。標準入力がターミ
   ナルだった場合、暗黙的に "-i" オプションが指定されます。

   このオプションが指定された場合、 "sys.argv" の最初の要素は ""-"" で
   、カレントディレクトリが "sys.path" の先頭に追加されます。

   参考:

     "runpy.run_path()"
        Python コードで直接使える等価な機能

<script>

   *script* 内の Python コードを実行します。 *script* は、Python ファ
   イル、 "__main__.py" ファイルがあるディレクトリ、 "__main__.py" フ
   ァイルがある zip ファイルのいずれかの、ファイルシステム上の (絶対ま
   たは相対) パスでなければなりません。

   このオプションが指定された場合、 "sys.argv" の最初の要素はコマンド
   ラインで指定されたスクリプト名になります。

   スクリプト名が Python ファイルを直接指定していた場合、そのファイル
   を含むディレクトリが "sys.path" の先頭に追加され、そのファイルは
   "__main__" モジュールとして実行されます。

   スクリプト名がディレクトリか zip ファイルを指定していた場合、スクリ
   プト名が "sys.path" に追加され、その中の "__main__.py" ファイルが
   "__main__" モジュールとして実行されます。

   バージョン 2.5 で変更: Directories and zipfiles containing a
   "__main__.py" file at the top level are now considered valid Python
   scripts.

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".

参考: インタプリタを起動する


一般オプション
--------------

-?
-h
--help

   全コマンドラインオプションの短い説明を出力します。

   バージョン 2.5 で変更: The "--help" variant.

-V
--version

   Python のバージョン番号を表示して終了します。出力の例:

      Python 2.5.1

   バージョン 2.5 で変更: The "--version" variant.


その他のオプション
------------------

-b

   Issue a warning when comparing "unicode" with "bytearray". Issue an
   error when the option is given twice ("-bb").

   Note that, unlike the corresponding Python 3.x flag, this will
   **not** emit warnings for comparisons between "str" and "unicode".
   Instead, the "str" instance will be implicitly decoded to "unicode"
   and Unicode comparison used.

   バージョン 2.6 で追加.

-B

   If given, Python won’t try to write ".pyc" or ".pyo" files on the
   import of source modules.  See also "PYTHONDONTWRITEBYTECODE".

   バージョン 2.6 で追加.

-d

   パーサーのデバッグ出力を有効にします。(エキスパート専用です。コンパ
   イルオプションに依存します)。 "PYTHONDEBUG" も参照してください。

-E

   全ての "PYTHON*" 環境変数を無視します。例えば、 "PYTHONPATH" や
   "PYTHONHOME" などです。

   バージョン 2.2 で追加.

-i

   最初の引数にスクリプトが指定された場合や "-c" オプションが利用され
   た場合、 "sys.stdin" がターミナルに出力されない場合も含めて、スクリ
   プトかコマンドを実行した後にインタラクティブモードに入ります。
   "PYTHONSTARTUP" ファイルは読み込みません。

   このオプションはグローバル変数や、スクリプトが例外を発生させるとき
   にそのスタックトレースを調べるのに便利です。 "PYTHONINSPECT" も参照
   してください。

-O

   Turn on basic optimizations.  This changes the filename extension
   for compiled (*bytecode*) files from ".pyc" to ".pyo".  See also
   "PYTHONOPTIMIZE".

-OO

   "-O" の最適化に加えて、ドキュメンテーション文字列の除去も行ないます
   。

-Q <arg>

   Division control. The argument must be one of the following:

   "old"
      division of int/int and long/long return an int or long
      (*default*)

   "new"
      new division semantics, i.e. division of int/int and long/long
      returns a float

   "warn"
      old division semantics with a warning for int/int and long/long

   "warnall"
      old division semantics with a warning for all uses of the
      division operator

   参考:

     "Tools/scripts/fixdiv.py"
        for a use of "warnall"

     **PEP 238** – Changing the division operator

-R

   Turn on hash randomization, so that the "__hash__()" values of str,
   bytes and datetime objects are 「salted」 with an unpredictable
   random value. Although they remain constant within an individual
   Python process, they are not predictable between repeated
   invocations of Python.

   This is intended to provide protection against a denial-of-service
   caused by carefully-chosen inputs that exploit the worst case
   performance of a dict construction, O(n^2) complexity.  See
   http://www.ocert.org/advisories/ocert-2011-003.html for details.

   Changing hash values affects the order in which keys are retrieved
   from a dict.  Although Python has never made guarantees about this
   ordering (and it typically varies between 32-bit and 64-bit
   builds), enough real-world code implicitly relies on this non-
   guaranteed behavior that the randomization is disabled by default.

   See also "PYTHONHASHSEED".

   バージョン 2.6.8 で追加.

-s

   "user site-packages directory" を "sys.path" に追加しません。

   バージョン 2.6 で追加.

   参考: **PEP 370** – ユーザごとの "site-packages" ディレクトリ

-S

   Disable the import of the module "site" and the site-dependent
   manipulations of "sys.path" that it entails.

-t

   Issue a warning when a source file mixes tabs and spaces for
   indentation in a way that makes it depend on the worth of a tab
   expressed in spaces.  Issue an error when the option is given twice
   ("-tt").

-u

   Force stdin, stdout and stderr to be totally unbuffered.  On
   systems where it matters, also put stdin, stdout and stderr in
   binary mode.

   Note that there is internal buffering in "file.readlines()" and
   File Objects ("for line in sys.stdin") which is not influenced by
   this option.  To work around this, you will want to use
   "file.readline()" inside a "while 1:" loop.

   "PYTHONUNBUFFERED" も参照してください。

-v

   モジュールが初期化されるたびに、それがどこ(ファイル名やビルトインモ
   ジュール) からロードされたのかを示すメッセージを出力します。 二重に
   指定された場合("-vv")は、モジュールを検索するときにチェックされた各
   ファイルに対してメッセージを出力します。また、終了時のモジュールク
   リーンアップに関する情報も提供します。 "PYTHONVERBOSE" も参照してく
   ださい。

-W arg

   警告制御。 Python の警告機構はデフォルトでは警告メッセージを
   "sys.stderr" に出力します。典型的な警告メッセージは次のような形式で
   す:

      file:line: category: message

   デフォルトでは、各警告は発生したソース行ごとに一度だけ表示されます
   。このオプションは、警告をどれくらいの頻度で表示するかを制御します
   。

   複数の "-W" オプションを指定することができます。警告が1つ以上のオプ
   ションとマッチしたときは、最後にマッチしたオプションのアクションが
   有効になります。不正な "-W" オプションは無視されます (最初の警告が
   発生したときに、不正なオプションに対する警告メッセージが表示されま
   す)。

   Starting from Python 2.7, "DeprecationWarning" and its descendants
   are ignored by default.  The "-Wd" option can be used to re-enable
   them.

   警告は Python プログラムの中から "warnings" モジュールを利用して制
   御することができます。

   The simplest form of argument is one of the following action
   strings (or a unique abbreviation) by themselves:

   "ignore"
      全ての警告を無視する。

   "default"
      明示的にデフォルトの動作(ソース行ごとに1度警告を表示する)を要求
      する。

   "all"
      警告が発生するたびに表示する (これは、ループの中などで同じソース
      行により繰り返し警告が発生された場合に、大量のメッセージを表示し
      ます)。

   "module"
      各モジュールで最初に発生した警告を表示する。

   "once"
      プログラムで最初に発生した警告だけを表示する。

   "error"
      警告メッセージを表示する代わりに例外を発生させる。

   引数の完全形は次のようになります:

      action:message:category:module:line

   ここで、 *action* は上で説明されたものですが、残りのフィールドにマ
   ッチしたメッセージにだけ適用されます。空のフィールドは全ての値にマ
   ッチします。空のフィールドの後ろは除外されます。 *message* フィール
   ドは表示される警告メッセージの先頭に、大文字小文字を無視してマッチ
   します。 *category* フィールドは警告カテゴリにマッチします。これは
   クラス名でなければなりません。 *category* のマッチは、メッセージの
   実際の警告カテゴリーが指定された警告カテゴリーのサブクラスかどうか
   をチェックします。完全なクラス名を指定しなければなりません。
   *module* フィールドは、(完全正規形(fully-qualified)の) モジュール名
   に対してマッチします。このマッチは大文字小文字を区別します。 *line*
   フィールドは行番号にマッチします。 0 は全ての行番号にマッチし、省略
   した時と同じです。

   参考: "warnings" – 警告モジュール

     **PEP 230** – Warning framework

     "PYTHONWARNINGS"

-x

   Unix 以外の形式の "#!cmd" を使うために、ソースの最初の行をスキップ
   します。これは、DOS専用のハックのみを目的としています。

-3

   Warn about Python 3.x possible incompatibilities by emitting a
   "DeprecationWarning" for features that are removed or significantly
   changed in Python 3 and can’t be detected using static code
   analysis.

   バージョン 2.6 で追加.

   See Python 2 から Python 3 への移植 for more details.


使うべきでないオプション
------------------------

-J

   Jython のために予約されています。

-U

   Turns all string literals into unicodes globally.  Do not be
   tempted to use this option as it will probably break your world.
   It also produces ".pyc" files with a different magic number than
   normal.  Instead, you can enable unicode literals on a per-module
   basis by using:

      from __future__ import unicode_literals

   at the top of the file.  See "__future__" for details.

-X

   Reserved for alternative implementations of Python to use for their
   own purposes.


環境変数
========

These environment variables influence Python’s behavior, they are
processed before the command-line switches other than -E.  It is
customary that command-line switches override environmental variables
where there is a conflict.

PYTHONHOME

   標準 Python ライブラリの場所を変更します。デフォルトでは、ライブラ
   リは "*prefix*/lib/python*version*" と
   "*exec_prefix*/lib/python*version*" から検索されます。ここで、
   "*prefix*" と "*exec_prefix*" はインストール依存のディレクトリで、
   両方共デフォルトでは "/usr/local" です。

   "PYTHONHOME" が1つのディレクトリに設定されている場合、その値は
   "*prefix*" と "*exec_prefix*" の両方を置き換えます。それらに別々の
   値を指定したい場合は、 "PYTHONHOME" を "*prefix*:*exec_prefix*" の
   ように指定します。

PYTHONPATH

   モジュールファイルのデフォルトの検索パスを追加します。この環境変数
   のフォーマットはシェルの "PATH" と同じで、 "os.pathsep" (Unix なら
   コロン、 Windows ならセミコロン) で区切られた1つ以上のディレクトリ
   パスです。存在しないディレクトリは警告なしに無視されます。

   通常のディレクトリに加えて、 "PYTHONPATH" のエントリはピュアPython
   モジュール(ソース形式でもコンパイルされた形式でも) を含む zip ファ
   イルを参照することもできます。拡張モジュールは zip ファイルの中から
   import することはできません。

   デフォルトの検索パスはインストール依存ですが、通常は
   "*prefix*/lib/python*version*" で始まります。 (上の "PYTHONHOME" を
   参照してください。) これは *常に* "PYTHONPATH" に追加されます。

   上の インターフェイスオプション で説明されているように、追加の検索
   パスディレクトリが "PYTHONPATH" の手前に追加されます。検索パスは
   Python プログラムから "sys.path" 変数として操作することができます。

PYTHONSTARTUP

   If this is the name of a readable file, the Python commands in that
   file are executed before the first prompt is displayed in
   interactive mode.  The file is executed in the same namespace where
   interactive commands are executed so that objects defined or
   imported in it can be used without qualification in the interactive
   session.  You can also change the prompts "sys.ps1" and "sys.ps2"
   in this file.

PYTHONY2K

   Set this to a non-empty string to cause the "time" module to
   require dates specified as strings to include 4-digit years,
   otherwise 2-digit years are converted based on rules described in
   the "time" module documentation.

PYTHONOPTIMIZE

   この変数に空でない文字列を設定するのは "-O" オプションを指定するの
   と等価です。整数を設定した場合、 "-O" を複数回指定したのと同じにな
   ります。

PYTHONDEBUG

   この変数に空でない文字列を設定するのは "-d" オプションを指定するの
   と等価です。整数を指定した場合、 "-d" を複数回指定したのと同じにな
   ります。

PYTHONINSPECT

   この変数に空でない文字列を設定するのは "-i" オプションを指定するの
   と等価です。

   この変数は Python コードから "os.environ" を使って変更して、プログ
   ラム終了時のインスペクトモードを強制することができます。

PYTHONUNBUFFERED

   この変数に空でない文字列を設定するのは "-u" オプションを指定するの
   と等価です。

PYTHONVERBOSE

   この変数に空でない文字列を設定するのは "-v" オプションを指定するの
   と等価です。整数を設定した場合、 "-v" を複数回指定したのと同じにな
   ります。

PYTHONCASEOK

   If this is set, Python ignores case in "import" statements.  This
   only works on Windows, OS X, OS/2, and RiscOS.

PYTHONDONTWRITEBYTECODE

   If this is set, Python won’t try to write ".pyc" or ".pyo" files on
   the import of source modules.  This is equivalent to specifying the
   "-B" option.

   バージョン 2.6 で追加.

PYTHONHASHSEED

   If this variable is set to "random", the effect is the same as
   specifying the "-R" option: a random value is used to seed the
   hashes of str, bytes and datetime objects.

   "PYTHONHASHSEED" が整数値に設定された場合、その値はハッシュランダム
   化が扱う型の hash() 生成の固定シードに使われます。

   その目的は再現性のあるハッシュを可能にすることです。例えばインター
   プリタ自身の自己テストや Python プロセスのクラスタでハッシュ値を共
   有するのに用います。

   The integer must be a decimal number in the range [0,4294967295].
   Specifying the value 0 will lead to the same hash values as when
   hash randomization is disabled.

   バージョン 2.6.8 で追加.

PYTHONIOENCODING

   Overrides the encoding used for stdin/stdout/stderr, in the syntax
   "encodingname:errorhandler".  The ":errorhandler" part is optional
   and has the same meaning as in "str.encode()".

   バージョン 2.6 で追加.

PYTHONNOUSERSITE

   この環境変数が設定されている場合、 Python は "ユーザ site-packages
   ディレクトリ" を "sys.path" に追加しません。

   バージョン 2.6 で追加.

   参考: **PEP 370** – ユーザごとの "site-packages" ディレクトリ

PYTHONUSERBASE

   "user base directory" を設定します。これは "python setup.py install
   --user" 時に "user site-packages directory" と Distutils
   installation paths のパスを計算するのに使われます。

   バージョン 2.6 で追加.

   参考: **PEP 370** – ユーザごとの "site-packages" ディレクトリ

PYTHONEXECUTABLE

   この環境変数が設定された場合、 "sys.argv[0]" に、C ランタイムから取
   得した値の代わりにこの環境変数の値が設定されます。Mac OS X でのみ動
   作します。

PYTHONWARNINGS

   これは "-W" オプションと等価です。カンマ区切りの文字列を設定するの
   は "-W" を複数回指定するのと等価です。

PYTHONHTTPSVERIFY

   If this environment variable is set specifically to "0", then it is
   equivalent to implicitly calling "ssl._https_verify_certificates()"
   with "enable=False" when "ssl" is first imported.

   Refer to the documentation of "ssl._https_verify_certificates()"
   for details.

   バージョン 2.7.12 で追加.


デバッグモード変数
------------------

以下の環境変数は、"--with-pydebug" ビルドオプションを指定して構成され
たデバッグビルド版の Python でのみ効果があります。

PYTHONTHREADDEBUG

   設定された場合、 Python はスレッドデバッグ情報を表示します。

   バージョン 2.6 で変更: Previously, this variable was called
   "THREADDEBUG".

PYTHONDUMPREFS

   設定された場合、 Python はインタプリタのシャットダウン後に残ってい
   るオブジェクトと参照カウントをダンプします。

PYTHONMALLOCSTATS

   If set, Python will print memory allocation statistics every time a
   new object arena is created, and on shutdown.

PYTHONSHOWALLOCCOUNT

   If set and Python was compiled with "COUNT_ALLOCS" defined, Python
   will dump allocations counts into stderr on shutdown.

   バージョン 2.7.15 で追加.

PYTHONSHOWREFCOUNT

   If set, Python will print the total reference count when the
   program finishes or after each statement in the interactive
   interpreter.

   バージョン 2.7.15 で追加.
