"site" --- サイト固有の設定フック
*********************************

**ソースコード:** Lib/site.py

======================================================================

**このモジュールは初期化中に自動的にインポートされます。** 自動インポ
ートはインタプリタの "-S" オプションで禁止できます。

Importing this module normally appends site-specific paths to the
module search path and adds callables, including "help()" to the
built-in namespace. However, Python startup option "-S" blocks this
and this module can be safely imported with no automatic modifications
to the module search path or additions to the builtins.  To explicitly
trigger the usual site-specific additions, call the "main()" function.

バージョン 3.3 で変更: 以前は "-S" を使っているときでも、モジュールを
インポートするとパス変更が起動されていました。

It starts by constructing up to four directories from a head and a
tail part. For the head part, it uses "sys.prefix" and
"sys.exec_prefix"; empty heads are skipped.  For the tail part, it
uses the empty string and then "lib/site-packages" (on Windows) or
"lib/python*X.Y[t]*/site-packages" (on Unix and macOS). (The optional
suffix "t" indicates the *free threading* build, and is appended if
""t"" is present in the "sys.abiflags" constant.) For each of the
distinct head-tail combinations, it sees if it refers to an existing
directory, and if so, adds it to "sys.path" and also inspects the
newly added path for configuration files.

バージョン 3.5 で変更: "site-python" ディレクトリのサポートは削除され
ました。

バージョン 3.13 で変更: Unix では、*フリースレッド* の Python インスト
ールは、"lib/python3.13t/" のように、バージョン別ディレクトリ名の接尾
辞 "t" で識別します。

If a file named "pyvenv.cfg" exists one directory above
sys.executable, sys.prefix and sys.exec_prefix are set to that
directory and it is also checked for site-packages (sys.base_prefix
and sys.base_exec_prefix will always be the "real" prefixes of the
Python installation). If "pyvenv.cfg" (a bootstrap configuration file)
contains the key "include-system-site-packages" set to anything other
than "true" (case-insensitive), the system-level prefixes will not be
searched for site-packages; otherwise they will.

パス設定ファイルは "*name*.pth" という形式の名前をもつファイルで、上の
4つのディレクトリのひとつにあります。その内容は "sys.path" に追加され
る追加項目(一行に一つ)です。存在しない項目は "sys.path" へは決して追加
されませんが、項目がファイルではなくディレクトリを参照しているかどうか
はチェックされません。項目が "sys.path" へ二回以上追加されることはあり
ません。空行と "#" で始まる行は読み飛ばされます。 "import" で始まる(そ
してその後ろにスペースかタブが続く)行は実行されます。

注釈:

  ".pth" ファイル内の実行可能な行は、特定のモジュールが実際に使用され
  るかどうかに関係なく、Pythonの起動時に毎回実行されます。したがって、
  その影響は最小限に抑えられるべきです。実行可能な行の主な目的は、対応
  するモジュールをインポート可能にすることです(サードパーティ製インポ
  ートフックのロード、 "PATH" の調整など)。その他の初期化は、モジュー
  ルが実際にインポートされたときに行われます。コードのチャンクを1行に
  制限することは、より複雑なものをここに入れないようにするための意図的
  な手段です。

バージョン 3.13 で変更: ".pth" ファイルは、最初に UTF-8 で、失敗した場
合は *locale encoding* でデコードされるようになります。

例えば、 "sys.prefix" と "sys.exec_prefix" が "/usr/local" に設定され
ていると仮定します。そのときPython X.Y ライブラリは
"/usr/local/lib/python*X.Y*" にインストールされています。ここにはサブ
ディレクトリ "/usr/local/lib/python*X.Y*/site-packages" があり、その中
に三つのサブディレクトリ "foo", "bar" および "spam" と二つのパス設定フ
ァイル "foo.pth" と "bar.pth" をもつと仮定します。 "foo.pth" には以下
のものが記載されていると想定してください:

   # foo パッケージの設定

   foo
   bar
   bletch

また、 "bar.pth" には:

   # bar パッケージの設定

   bar

が記載されているとします。そのとき、次のバージョンごとのディレクトリが
"sys.path" へこの順番で追加されます:

   /usr/local/lib/pythonX.Y/site-packages/bar
   /usr/local/lib/pythonX.Y/site-packages/foo

"bletch" は存在しないため省略されるということに注意してください。
"bar" ディレクトリは "foo" ディレクトリの前に来ます。なぜなら、
"bar.pth" がアルファベット順で "foo.pth" の前に来るからです。また、
"spam" はどちらのパス設定ファイルにも記載されていないため、省略されま
す。


"sitecustomize"
===============

After these path manipulations, an attempt is made to import a module
named "sitecustomize", which can perform arbitrary site-specific
customizations. It is typically created by a system administrator in
the site-packages directory.  If this import fails with an
"ImportError" or its subclass exception, and the exception's "name"
attribute equals "'sitecustomize'", it is silently ignored.  If Python
is started without output streams available, as with "pythonw.exe" on
Windows (which is used by default to start IDLE), attempted output
from "sitecustomize" is ignored.  Any other exception causes a silent
and perhaps mysterious failure of the process.


"usercustomize"
===============

After this, an attempt is made to import a module named
"usercustomize", which can perform arbitrary user-specific
customizations, if "ENABLE_USER_SITE" is true.  This file is intended
to be created in the user site-packages directory (see below), which
is part of "sys.path" unless disabled by "-s".  If this import fails
with an "ImportError" or its subclass exception, and the exception's
"name" attribute equals "'usercustomize'", it is silently ignored.

いくつかの非Unixシステムでは、 "sys.prefix" と "sys.exec_prefix" は空
で、パス操作は省略されます。しかし、 "sitecustomize" と
"usercustomize" のインポートはそのときでも試みられます。


readline の設定
===============

On systems that support "readline", this module will also import and
configure the "rlcompleter" module, if Python is started in
interactive mode and without the "-S" option. The default behavior is
to enable tab completion and to use "~/.python_history" as the history
save file.  To disable it, delete (or override) the
"sys.__interactivehook__" attribute in your "sitecustomize" or
"usercustomize" module or your "PYTHONSTARTUP" file.

バージョン 3.4 で変更: rlcompleter とhistory のアクティベーションが自
動で行われるようになりました。


モジュールの内容
================

site.PREFIXES

   siteパッケージディレクトリのprefixのリスト。

site.ENABLE_USER_SITE

   ユーザサイトディレクトリのステータスを示すフラグ。 "True" の場合、
   ユーザサイトディレクトリが有効で "sys.path" に追加されていることを
   意味しています。 "False" の場合、ユーザによるリクエスト(オプション
   "-s" か "PYTHONNOUSERSITE")によって、 "None" の場合セキュリティ上の
   理由(ユーザまたはグループIDと実効IDの間のミスマッチ)あるいは管理者
   によって、ユーザサイトディレクトリが無効になっていることを示してい
   ます。

site.USER_SITE

   実行中の Python のユーザ site-packages ディレクトリへのパスです。
   "getusersitepackages()" をまだ呼び出していない場合は "None" のこと
   もあります。 デフォルトは、UNIX と非フレームワークの macOS ビルドで
   は "~/.local/lib/python*X.Y*[t]/site-packages"、macOS のフレームワ
   ークビルドでは "~/Library/Python/*X.Y*/lib/python/site-packages"、
   Windows では "*%APPDATA%*\Python\Python*XY*\site-packages" です。
   省略可能な "t" はフリースレッドビルドを示します。 これはサイトディ
   レクトリなので、内部の ".pth" ファイルは処理されます。

site.USER_BASE

   ユーザの site-packages のベースとなるディレクトリへのパスです。
   "getuserbase()" がまだ呼び出されていなければ "None" かもしれません
   。デフォルト値は  UNIX と frameworkなしの macOS ビルドでは
   "~/.local" 、macOS framework ビルドでは "~/Library/Python/*X.Y*" 、
   Windows では "*%APPDATA%*\Python" です。この値は、スクリプト、デー
   タファイル、Python モジュールなどのインストール先のディレクトリを
   ユーザーインストールスキーム で計算するのに使われます。
   "PYTHONUSERBASE" も参照してください。

site.main()

   モジュール検索パスに標準のサイト固有ディレクトリを追加します。この
   関数は、Python インタプリタが "-S" で開始されていない限り、このモジ
   ュールインポート時に自動的に呼び出されます。

   バージョン 3.3 で変更: この関数は以前は無条件に呼び出されていました
   。

site.addsitedir(sitedir, known_paths=None)

   sys.path にディレクトリを追加し、その ".pth" ファイル群を処理します
   。典型的には "sitecustomize" か "usercustomize" 内で使われます(上述
   )。

site.getsitepackages(prefixes=None)

   全てのグローバルな site-packages ディレクトリのリストを返します。

   For each directory given in *prefixes* (or "PREFIXES" if *prefixes*
   is "None"), this function will compute its site-packages
   subdirectory depending on the system environment, and will return a
   list of full paths, which are not checked for existence.

   Added in version 3.2.

   バージョン 3.3 で変更: Added the optional *prefixes* parameter.

site.getuserbase()

   ユーザのベースディレクトリへのパス "USER_BASE" を返します。未初期化
   であればこの関数は "PYTHONUSERBASE" を参考にして、設定もします。

   Added in version 3.2.

site.getusersitepackages()

   ユーザ固有の site パッケージのディレクトリへのパス "USER_SITE" を返
   します。未初期化であればこの関数は "USER_BASE" を参考にして、設定も
   します。 ユーザ固有の site パッケージが "sys.path" に追加されたかど
   うかを確認するには "ENABLE_USER_SITE" を使ってください。

   Added in version 3.2.


Command-line interface
======================

"site" モジュールはユーザディレクトリをコマンドラインから得る手段も提
供しています:

   $ python -m site --user-site
   /home/user/.local/lib/python3.11/site-packages

引数なしで呼び出された場合、"sys.path" の中身を表示し、続けて
"USER_BASE" とそのディレクトリが存在するかどうか、 "USER_SITE" とその
ディレクトリが存在するかどうか、最後に "ENABLE_USER_SITE" の値を、標準
出力に出力します。

--user-base

   ユーザのベースディレクトリを表示します。

--user-site

   ユーザの site-packages ディレクトリを表示します。

両方のオプションが指定された場合、ユーザのベースとユーザの site が(常
にこの順序で) "os.pathsep" 区切りで表示されます。

いずれかのオプションが与えられた場合に、このスクリプトは次のいずれかの
終了コードで終了します: ユーザの site-packages が有効ならば "0" 、ユー
ザにより無効にされていれば "1" 、セキュリティ的な理由あるいは管理者に
よって無効にされている場合 "2" 、そして何かエラーがあった場合は 2 より
大きな値。

参考:

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

  * sys.path モジュール検索パスの初期化 -- "sys.path". の初期化。
