2. 在类Unix环境下使用Python

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

2.1.1. 在Linux中

Python预装在大多数Linux发行版上,并作为一个包提供给所有其他用户。 但是,您可能想要使用的某些功能在发行版提供的软件包中不可用。这时您可以从源代码轻松编译最新版本的Python。

如果Python没有预先安装并且不在发行版提供的库中,您可以轻松地为自己使用的发行版创建包。 参阅以下链接:

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.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.