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_add -r python
OpenBSD users use:
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¶
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
可以覆盖或伪装 python3
二进制文件。因此,建议使用 make altinstall
而不是 make install
,因为后者只安装了 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 python3
将在整个 PATH
中搜索Python解释器。但是,某些Unix系统可能没有 env 命令,因此可能需要将 /usr/bin/python3
硬编码为解释器路径。
要在Python脚本中使用shell命令,请查看 subprocess
模块。
2.5. Editors¶
Vim and Emacs are excellent editors which support Python very well. For more information on how to code in Python in these editors, look at:
Geany is an excellent IDE with support for a lot of languages. For more information, read: https://www.geany.org/
Komodo edit is another extremely good IDE. It also has support for a lot of languages. For more information, read https://komodoide.com/.