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


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


2.1.1. Linux
------------

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

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

参考:

  https://www.debian.org/doc/manuals/maint-guide/first.en.html
     Debian ユーザー向け

  https://en.opensuse.org/Portal:Packaging
     OpenSuse ユーザー向け

  https://docs.fedoraproject.org/en-US/package-
  maintainers/Packaging_Tutorial_GNU_Hello/
     Fedora ユーザー向け

  https://slackbook.org/html/package-management-making-packages.html
     Slackware ユーザー向け


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.2. Python のビルド
====================

CPython を自分でコンパイルしたい場合は、まず ソース を入手します。 最
新リリース版のソースをダウンロード、あるいはソースリポジトリから新しく
クローン を作成してください。(パッチの作成に貢献したい場合はクローンが
必要になるでしょう。)

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

   ./configure
   make
   make install

Configure のオプション や特定の Unix プラットフォームにおける注意点は
Python ソースツリーのルートにある README.rst に細かく記載されています
。

警告:

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


2.3. Python に関係するパスとファイル
====================================

これらはローカルインストールの慣例に応じて変化します; "prefix" と
"exec_prefix" はインストール状況に依存していて、GNU ソフトウェアによっ
て解釈されます; この二つは同じである場合があります。

例えば、ほとんどの Linux システムでは、両方のデフォルトが "/usr" です
。

+-------------------------------------------------+--------------------------------------------+
| ファイル/ディレクトリ                           | 意味                                       |
|=================================================|============================================|
| "*exec_prefix*/bin/python3"                     | インタプリタの推奨される場所               |
+-------------------------------------------------+--------------------------------------------+
| "*prefix*/lib/python*version*",                 | 標準モジュールを格納するディレクトリの、推 |
| "*exec_prefix*/lib/python*version*"             | 奨される場所。                             |
+-------------------------------------------------+--------------------------------------------+
| "*prefix*/include/python*version*",             | Python 拡張や Python の埋込みに必要となる  |
| "*exec_prefix*/include/python*version*"         | include ファイルを格納するデ ィレクトリの  |
|                                                 | 推奨される場所。                           |
+-------------------------------------------------+--------------------------------------------+


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.
