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


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


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

Python comes preinstalled on most Linux distributions, and is
available as a package on all others.  However there are certain
features you might want to use that are not available on your distro's
package.  You can compile the latest version of Python from source.

In the event that the latest version of Python doesn't come
preinstalled and isn't in the repositories as well, you can make
packages for your own distro.  Have a look at the following links:

参考:

  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.1.1. IDLE のインストール
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

場合によっては、 IDLE は Python インストールに含まれていない可能性があ
ります。

* Debian と Ubuntu ユーザー:

     sudo apt update
     sudo apt install idle

* Fedora や RHEL 、 CentOS ユーザーは:

     sudo dnf install python3-idle

* SUSE と OpenSUSE ユーザーは:

     sudo zypper install python3-idle

* Alpine Linux ユーザーは:

     sudo apk add python3-idle


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/<ここにアーキテクチャを挿入>/python-<バージョン>.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 のビルド
====================

参考:

  If you want to contribute to CPython, refer to the devguide, which
  includes build instructions and other tips on setting up
  environment.

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
grab a fresh clone. You will also need to install the build
requirements.

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

   ./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. カスタムの 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.
