2. Unix プラットフォームで Python を使う

2.1. 最新バージョンの Python の取得とインストール

2.1.1. Linux

ほとんどの Linux ディストリビューションでは Python はプリインストールされており、それ以外でもパッケージとして利用可能です。しかし、ディストリビューションのパッケージでは利用したい機能が使えない場合があります。最新版の Python をソースから簡単にコンパイルすることができます。

Python がプリインストールされておらず、リポジトリにも無い場合、ディストリビューション用のパッケージを簡単につくることができます。以下のリンクを参照してください:

2.1.2. FreeBSD と OpenBSD

  • FreeBSD ユーザーが Python パッケージを追加するには次のようにしてください:

    pkg install python3
    
  • OpenBSD ユーザーが Python パッケージを追加するには次のようにしてください:

    pkg_add -r python
    
    pkg_add ftp://ftp.openbsd.org/pub/OpenBSD/4.2/packages/<insert your architecture here>/python-<version>.tgz
    

    例えば、i386 ユーザーが Python 2.5.1 を取得するには次のようにします:

    pkg_add ftp://ftp.openbsd.org/pub/OpenBSD/4.2/packages/i386/python-2.5.1p2.tgz
    

2.1.3. OpenSolaris

OpenCSW から Python を入手することができます。 Python の様々なバージョンが利用可能でインストールすることができます。e.g. pkgutil -i python27.

2.2. Python のビルド

If you want to compile CPython yourself, first thing you should do is get the source. You can download either the latest release's source or just grab a fresh clone. (If you want to contribute patches, you will need a clone.)

ビルド手順は通常のコマンドで行います

./configure
make
make install

Configuration options and caveats for specific Unix platforms are extensively documented in the README.rst file in the root of the Python source tree.

警告

make installpython3 バイナリを上書きまたはリンクを破壊してしまうかもしれません。そのため、make install の代わりに exec_prefix/bin/pythonversion のみインストールする make altinstall が推奨されています。

2.4. その他

Python スクリプトを Unix で簡単に使うためには、例えば次のようにしてスクリプトを実行可能ファイルにし、

$ chmod +x script

適切な shebang 行をスクリプトの先頭に置きます。たいていの場合良い方法は

#!/usr/bin/env python3

で、PATH 全体から Python インタープリターを探します。しかし、いくつかの Unix は env コマンドを持たないので、インタープリターのパスを /usr/bin/python3 のようにハードコードしなければならないかもしれません。

シェルコマンドを Python スクリプトから使うには、 subprocess モジュールを参照してください。

2.5. Custom OpenSSL

  1. To use your vendor's OpenSSL configuration and system trust store, locate the directory with openssl.cnf file or symlink in /etc. On most distribution the file is either in /etc/ssl or /etc/pki/tls. The directory should also contain a cert.pem file and/or a certs directory.

    $ find /etc/ -name openssl.cnf -printf "%h\n"
    /etc/ssl
    
  2. Download, build, and install OpenSSL. Make sure you use install_sw and not install. The install_sw target does not override openssl.cnf.

    $ curl -O https://www.openssl.org/source/openssl-VERSION.tar.gz
    $ tar xzf openssl-VERSION
    $ pushd openssl-VERSION
    $ ./config \
        --prefix=/usr/local/custom-openssl \
        --libdir=lib \
        --openssldir=/etc/ssl
    $ make -j1 depend
    $ make -j8
    $ make install_sw
    $ popd
    
  3. Build Python with custom OpenSSL (see the configure --with-openssl and --with-openssl-rpath options)

    $ pushd python-3.x.x
    $ ./configure -C \
        --with-openssl=/usr/local/custom-openssl \
        --with-openssl-rpath=auto \
        --prefix=/usr/local/python-3.x.x
    $ make -j8
    $ make altinstall
    

注釈

Patch releases of OpenSSL have a backwards compatible ABI. You don't need to recompile Python to update OpenSSL. It's sufficient to replace the custom OpenSSL installation with a newer version.