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. Installing IDLE
~~~~~~~~~~~~~~~~~~~~~~~~

In some cases, IDLE might not be included in your Python installation.

* For Debian and Ubuntu users:

     sudo apt update
     sudo apt install idle

* For Fedora, RHEL, and CentOS users:

     sudo dnf install python3-idle

* For SUSE and OpenSUSE users:

     sudo zypper install python3-idle

* For Alpine Linux users:

     sudo apk add python3-idle


2.1.2. 在 FreeBSD 和 OpenBSD 上
-------------------------------

* FreeBSD 用戶應使用以下命令增加套件：

     pkg install python3

* OpenBSD 用戶應使用以下命令增加套件：

     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，首先要做的是獲取原始碼。你可以下載最新版本的
原始碼，也可以直接提取最新的 clone（克隆）。（如果你想要貢獻修補程式碼
，也會需要一份 clone。）

建置過程由幾個常用命令組成：

   ./configure
   make
   make install

特定 Unix 平臺的配置選項和注意事項通常會詳細地記錄在 Python 原始碼樹
(source tree) 根目錄下的 README.rst 檔案中。

警告:

  "make install" 可以覆蓋或偽裝 "python3" 二進位制檔案。因此，建議使用
  "make altinstall" 而不是 "make install"，因為它只安裝
  "*exec_prefix*/bin/python*version*"。


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 擴充套件和嵌入直譯器所需   |
| "*exec_prefix*/include/python*version*"         | include 檔案之目錄的推薦位置 。            |
+-------------------------------------------------+--------------------------------------------+


2.4. 雜項
=========

要在 Unix 上使用 Python 腳本，你需要讓他們是可執行的 (executable)，例
如用

   $ chmod +x script

並在腳本的頂部放一個合適的 Shebang。以下通常是個好選擇：

   #!/usr/bin/env python3

將在整個 "PATH" 中搜索 Python 直譯器。然而某些 Unix 系統可能沒有
**env** 命令，因此你可能需要將 "/usr/bin/python3" 寫死 (hardcode) 成直
譯器路徑。

要在 Python 腳本中使用 shell 命令，請見 "subprocess" 模組。


2.5. 客製化 OpenSSL
===================

1. 要使用你所選擇發行商 (vendor) 的 OpenSSL 配置和系統信任儲存區
   (system trust store)，請找到包含 "openssl.cnf" 檔案的目錄或位於
   "/etc" 的符號連結 (symlink)。在大多數發行版上，該檔案會是在
   "/etc/ssl" 或者 "/etc/pki/tls" 中。該目錄亦應包含一個 "cert.pem" 檔
   案和/或一個 "certs" 目錄。

      $ find /etc/ -name openssl.cnf -printf "%h\n"
      /etc/ssl

2. 下載、建置並安裝 OpenSSL。請確保你使用 "install_sw" 而不是
   "install"。"install_sw" 的目標不會覆蓋 "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. 使用客製化 OpenSSL 建置 Python（參見配置 "--with-openssl" 和 "--
   with-openssl-rpath" 選項）

      $ 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

備註:

  OpenSSL 的修補釋出版 (patch releases) 具有向後相容的 ABI。你不需要重
  新編譯 Python 來更新 OpenSSL。使用一個新的版本來替代客製化 OpenSSL
  安裝版就可以了。
