This document is for an old version of Python that is no longer supported. You should upgrade and read the Python 当前稳定版本的文档.

# 2. 在类Unix环境下使用Python

## 2.1. 获得并安装Python的最新版本

### 2.1.1. 在Linux中

Python预装在大多数Linux发行版上，并作为一个包提供给所有其他用户。 但是，您可能想要使用的某些功能在发行版提供的软件包中不可用。这时您可以从源代码轻松编译最新版本的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/Fedora_Draft_Documentation/0.1/html/RPM_Guide/ch-creating-rpms.html

  对于Fedora用户

http://www.slackbook.org/html/package-management-making-packages.html

  对于Slackware用户

### 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.1.3. 在OpenSolaris系统上

你可以从 OpenCSW 获取、安装及使用各种版本的Python。比如 `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

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

警告

`make install` can overwrite or masquerade the `python` binary. `make altinstall` is therefore recommended instead of `make install` since it only installs `exec_prefix/bin/pythonversion`.

## 2.3. 与Python相关的路径和文件

这取决于本地安装惯例； `prefix` （ `${prefix}` ）和 `exec_prefix` （ `${exec_prefix}` ） 取决于安装，应解释为GNU软件；它们可能相同。

例如，在大多数Linux系统上，两者的默认值是 `/usr` 。

文件/目录 | 含义
--- | ---
`exec_prefix/bin/python` | 解释器的推荐位置
`prefix/lib/pythonversion`, `exec_prefix/lib/pythonversion` | 包含标准模块的目录的推荐位置
`prefix/include/pythonversion`, `exec_prefix/include/pythonversion` | 包含开发Python扩展和嵌入解释器所需的include文件的目录的推荐位置
`~/.pythonrc.py` | User-specific initialization file loaded by the user module; not used by default or by most applications.

## 2.4. 杂项

To easily use Python scripts on Unix, you need to make them executable, e.g. with

    $ chmod +x script

并在脚本的顶部放置一个合适的Shebang线。一个很好的选择通常是:

    #!/usr/bin/env python

which searches for the Python interpreter in the whole `PATH`. However, some Unices may not have the env command, so you may need to hardcode `/usr/bin/python` as the interpreter path.

要在Python脚本中使用shell命令，请查看 `subprocess` 模块。

## 2.5. Editors and IDEs

There are a number of IDEs that support Python programming language. Many editors and IDEs provide syntax highlighting, debugging tools, and PEP 8 checks.

Please go to Python Editors and Integrated Development Environments for a comprehensive list.