sysconfig --- Python の構成情報にアクセスする

バージョン 3.2 で追加.

ソースコード: Lib/sysconfig.py


sysconfig モジュールは、インストールパスのリストや、現在のプラットフォームに関連した構成などの、Python の構成情報 (configuration information) へのアクセスを提供します。

構成変数

A Python distribution contains a Makefile and a pyconfig.h header file that are necessary to build both the Python binary itself and third-party C extensions compiled using setuptools.

sysconfig はこれらのファイルに含まれるすべての変数を辞書に格納し、get_config_vars()get_config_var() でアクセスできるようにします。

Windows では構成変数はだいぶ少なくなります。

sysconfig.get_config_vars(*args)

引数がない場合、現在のプラットフォームに関するすべての構成変数の辞書を返します。

引数がある場合、各引数を構成変数辞書から検索した結果の変数のリストを返します。

各引数において、変数が見つからなかった場合は None が返されます。

sysconfig.get_config_var(name)

1つの変数 name を返します。get_config_vars().get(name) と同じです。

name が見つからない場合、None を返します。

Example of usage:

>>> import sysconfig
>>> sysconfig.get_config_var('Py_ENABLE_SHARED')
0
>>> sysconfig.get_config_var('LIBDIR')
'/usr/local/lib'
>>> sysconfig.get_config_vars('AR', 'CXX')
['ar', 'g++']

インストールパス

Python uses an installation scheme that differs depending on the platform and on the installation options. These schemes are stored in sysconfig under unique identifiers based on the value returned by os.name. The schemes are used by package installers to determine where to copy files to.

Python は現在9つのスキームをサポートしています:

  • posix_prefix: Linux や macOS などの POSIX プラットフォーム用のスキームです。これは Python やコンポーネントをインストールするときに使われるデフォルトのスキームです。

  • posix_home: scheme for POSIX platforms, when the home option is used. This scheme defines paths located under a specific home prefix.

  • posix_user: scheme for POSIX platforms, when the user option is used. This scheme defines paths located under the user's home directory (site.USER_BASE).

  • posix_venv: scheme for Python virtual environments on POSIX platforms; by default it is the same as posix_prefix.

  • nt: scheme for Windows. This is the default scheme used when Python or a component is installed.

  • nt_user: scheme for Windows, when the user option is used.

  • nt_venv: scheme for Python virtual environments on Windows; by default it is the same as nt.

  • venv: a scheme with values from either posix_venv or nt_venv depending on the platform Python runs on.

  • osx_framework_user オプションが利用された場合の、macOS 用のスキームです。

各スキームは、ユニークな識別子を持ったいくつかのパスの集合から成っています。現在 Python は8つのパスを利用します:

  • stdlib: プラットフォーム非依存の、標準 Python ライブラリファイルを格納するディレクトリ.

  • platstdlib: プラットフォーム依存の、標準 Python ライブラリファイルを格納するディレクトリ.

  • platlib: プラットフォーム依存の、site ごとのファイルを格納するディレクトリ。

  • purelib: directory for site-specific, non-platform-specific files ('pure' Python).

  • include: directory for non-platform-specific header files for the Python C-API.

  • platinclude: directory for platform-specific header files for the Python C-API.

  • scripts: スクリプトファイルのためのディレクトリ。

  • data: データファイルのためのディレクトリ。

User scheme

This scheme is designed to be the most convenient solution for users that don't have write permission to the global site-packages directory or don't want to install into it.

Files will be installed into subdirectories of site.USER_BASE (written as userbase hereafter). This scheme installs pure Python modules and extension modules in the same location (also known as site.USER_SITE).

posix_user

Path

インストールディレクトリ

stdlib

userbase/lib/pythonX.Y

platstdlib

userbase/lib/pythonX.Y

platlib

userbase/lib/pythonX.Y/site-packages

purelib

userbase/lib/pythonX.Y/site-packages

include

userbase/include/pythonX.Y

scripts

userbase/bin

data

userbase

nt_user

Path

インストールディレクトリ

stdlib

userbase\PythonXY

platstdlib

userbase\PythonXY

platlib

userbase\PythonXY\site-packages

purelib

userbase\PythonXY\site-packages

include

userbase\PythonXY\Include

scripts

userbase\PythonXY\Scripts

data

userbase

osx_framework_user

Path

インストールディレクトリ

stdlib

userbase/lib/python

platstdlib

userbase/lib/python

platlib

userbase/lib/python/site-packages

purelib

userbase/lib/python/site-packages

include

userbase/include/pythonX.Y

scripts

userbase/bin

data

userbase

Home scheme

"home スキーム" の背後にある考え方は、Python モジュールを個人用のモジュール置き場でビルドし、維持するというものです。このスキームの名前は Unixの「ホーム」ディレクトリの概念からとりました。というのも、 Unixのユーザにとって、自分のホームディレクトリを /usr//usr/local/ のようにレイアウトするのはよくあることだからです。とはいえ、このスキームはどのオペレーティングシステムのユーザでも使えます。

posix_home

Path

インストールディレクトリ

stdlib

home/lib/python

platstdlib

home/lib/python

platlib

home/lib/python

purelib

home/lib/python

include

home/include/python

platinclude

home/include/python

scripts

home/bin

data

home

Prefix scheme

あるインストール済みの Python を使ってモジュールのビルド/インストールを (例えば setup スクリプトを実行して) 行いたいけれども、 別のインストール済みの Python のサードパーティ製モジュール置き場 (あるいは、そう見えるようなディレクトリ構造) に、ビルドされた モジュールをインストールしたい場合には、"prefix スキーム" が便利です。そんな作業はまったくありえそうにない、と思うなら、確かにその通りです --- "home スキーム" を先に説明したのもそのためです。とはいえ、prefix スキームが有用なケースは少なくとも二つあります。

First, consider that many Linux distributions put Python in /usr, rather than the more traditional /usr/local. This is entirely appropriate, since in those cases Python is part of "the system" rather than a local add-on. However, if you are installing Python modules from source, you probably want them to go in /usr/local/lib/python2.X rather than /usr/lib/python2.X.

Another possibility is a network filesystem where the name used to write to a remote directory is different from the name used to read it: for example, the Python interpreter accessed as /usr/local/bin/python might search for modules in /usr/local/lib/python2.X, but those modules would have to be installed to, say, /mnt/@server/export/lib/python2.X.

posix_prefix

Path

インストールディレクトリ

stdlib

prefix/lib/pythonX.Y

platstdlib

prefix/lib/pythonX.Y

platlib

prefix/lib/pythonX.Y/site-packages

purelib

prefix/lib/pythonX.Y/site-packages

include

prefix/include/pythonX.Y

platinclude

prefix/include/pythonX.Y

scripts

prefix/bin

data

prefix

nt

Path

インストールディレクトリ

stdlib

prefix\Lib

platstdlib

prefix\Lib

platlib

prefix\Lib\site-packages

purelib

prefix\Lib\site-packages

include

prefix\Include

platinclude

prefix\Include

scripts

prefix\Scripts

data

prefix

Installation path functions

sysconfig provides some functions to determine these installation paths.

sysconfig.get_scheme_names()

現在 sysconfig でサポートされているすべてのスキームを格納したタプルを返します。

sysconfig.get_default_scheme()

Return the default scheme name for the current platform.

バージョン 3.10 で追加: This function was previously named _get_default_scheme() and considered an implementation detail.

バージョン 3.11 で変更: When Python runs from a virtual environment, the venv scheme is returned.

sysconfig.get_preferred_scheme(key)

Return a preferred scheme name for an installation layout specified by key.

key must be either "prefix", "home", or "user".

The return value is a scheme name listed in get_scheme_names(). It can be passed to sysconfig functions that take a scheme argument, such as get_paths().

バージョン 3.10 で追加.

バージョン 3.11 で変更: When Python runs from a virtual environment and key="prefix", the venv scheme is returned.

sysconfig._get_preferred_schemes()

Return a dict containing preferred scheme names on the current platform. Python implementers and redistributors may add their preferred schemes to the _INSTALL_SCHEMES module-level global value, and modify this function to return those scheme names, to e.g. provide different schemes for system and language package managers to use, so packages installed by either do not mix with those by the other.

End users should not use this function, but get_default_scheme() and get_preferred_scheme() instead.

バージョン 3.10 で追加.

sysconfig.get_path_names()

現在 sysconfig でサポートされているすべてのパス名を格納したタプルを返します。

sysconfig.get_path(name[, scheme[, vars[, expand]]])

scheme で指定されたインストールスキームから、パス name に従ってインストールパスを返します。

nameget_path_names() が返すリストに含まれる値でなければなりません。

sysconfig はインストールパスを、パス名、プラットフォーム、展開される変数に従って格納します。例えば、nt スキームでの stdlib パスは {base}/Lib になります。

get_path() はパスを展開するのに get_config_vars() が返す変数を利用します。すべての変数は各プラットフォームにおいてデフォルト値を持っていて、この関数を呼び出したときにデフォルト値を取得する場合があります。

scheme が指定された場合、get_scheme_names() が返すリストに含まれる値でなければなりません。指定されなかった場合は、現在のプラットフォームでのデフォルトスキームが利用されます。

If vars is provided, it must be a dictionary of variables that will update the dictionary returned by get_config_vars().

expandFalse に設定された場合、パスは変数を使って展開されません。

name が見つからない場合、KeyError を送出します。

sysconfig.get_paths([scheme[, vars[, expand]]])

インストールスキームに基づいたすべてのインストールパスを格納した辞書を返します。詳しい情報は get_path() を参照してください。

scheme が指定されない場合、現在のプラットフォームでのデフォルトスキーマが使用されます。

vars を指定する場合、パスを展開するために使用される辞書を更新する値の辞書でなければなりません。

expand に偽を指定すると、パスは展開されません。

scheme が実在するスキームでなかった場合、get_paths()KeyError を発生させます。

その他の関数

sysconfig.get_python_version()

MAJOR.MINOR の型の Python バージョン番号文字列を返します。'%d.%d' % sys.version_info[:2] に似ています。

sysconfig.get_platform()

現在のプラットフォームを識別するための文字列を返します。

これはプラットフォーム依存のビルドディレクトリやプラットフォーム依存の配布物を区別するために使われます。 典型的には、('os.uname()' のように) OSの名前とバージョン、アーキテクチャを含みますが、厳密にはOSに依存します。 例えば Linux ではカーネルのバージョンはそれほど重要ではありません。

返される値の例:

  • linux-i586

  • linux-alpha (?)

  • solaris-2.6-sun4u

Windows では以下のどれかを返します:

  • win-amd64 (64bit Windows on AMD64, 別名 x86_64, Intel64, EM64T)

  • win32 (その他すべて - 具体的には sys.platform が返す値)

macOS では以下のどれかを返します:

  • macosx-10.6-ppc

  • macosx-10.4-ppc64

  • macosx-10.3-i386

  • macosx-10.4-fat

その他の非 POSIX プラットフォームでは、現在のところ単に sys.platform を返します。

sysconfig.is_python_build()

実行中の Python インタプリタがソースからビルドされ、かつそのビルドされた場所から実行されている場合に True を返します。 make install を実行した場所からの実行やバイナリインストーラによるインストールではいけません。

sysconfig.parse_config_h(fp[, vars])

config.h スタイルのファイルを解析します。

fpconfig.h スタイルのファイルを指すファイルライクオブジェクトです。

name/value ペアを格納した辞書を返します。第二引数にオプションの辞書が渡された場合、新しい辞書ではなくその辞書を利用し、ファイルから読み込んだ値で更新します。

sysconfig.get_config_h_filename()

pyconfig.h のパスを返します。

sysconfig.get_makefile_filename()

Makefile のパスを返します。

sysconfig をスクリプトとして使う

Python の -m オプションを使えば、sysconfig をスクリプトとして使用できます:

$ python -m sysconfig
Platform: "macosx-10.4-i386"
Python version: "3.2"
Current installation scheme: "posix_prefix"

Paths:
        data = "/usr/local"
        include = "/Users/tarek/Dev/svn.python.org/py3k/Include"
        platinclude = "."
        platlib = "/usr/local/lib/python3.2/site-packages"
        platstdlib = "/usr/local/lib/python3.2"
        purelib = "/usr/local/lib/python3.2/site-packages"
        scripts = "/usr/local/bin"
        stdlib = "/usr/local/lib/python3.2"

Variables:
        AC_APPLE_UNIVERSAL_BUILD = "0"
        AIX_GENUINE_CPLUSPLUS = "0"
        AR = "ar"
        ARFLAGS = "rc"
        ...

これは、get_platform()get_python_version()get_path() および get_config_vars() が返す情報を標準出力に出力します。