Linux Admin 简明教程

Set Up Python with CentOS Linux

Python 是一种广泛使用的解释型语言,为在 Linux(和其他操作系统上)编写脚本应用程序的世界带来了专业性。Perl 曾经是行业标准,但 Python 在很多方面都超越了 Perl。

Python is a widely used interpreted language that has brought professionalism to the world of coding scripted applications on Linux (and other operating systems). Where Perl was once the industry standard, Python has surpassed Perl in many respects.

Python 相对于 Perl 的一些优势包括:

Some strengths of Python versus Perl are −

  1. Rapid progression in refinement

  2. Libraries that are standard to the language

  3. Readability of the code is thought out in language definition

  4. Many professional frameworks for everything from GUI support to web-development

Python 可以执行 Perl 可以执行的所有操作,并且在很多情况下以更好的方式执行。虽然 Perl 仍然在 Linux 管理员的工具箱中占有一席之地,但学习 Python 是作为技能组合的一个极好选择。

Python can do anything Perl can do, and in a lot of cases in a better manner. Though Perl still has its place amongst the toolbox of a Linux admin, learning Python is a great choice as a skill set.

Python 最大的缺点有时与其优点有关。从历史上看,Python 最初是设计用于教授编程的。有时,其“易于阅读”和“以正确方式做事”的核心基础会在编写简单代码时导致不必要的复杂性。另外,其标准库在从版本 2.X 过渡到版本 3.X 时造成了问题。

The biggest drawbacks of Python are sometimes related to its strengths. In history, Python was originally designed to teach programming. At times, its core foundations of "easily readable" and "doing things the right way" can cause unnecessary complexities when writing a simple code. Also, its standard libraries have caused problems in transitioning from versions 2.X to 3.X.

实际上,Python 脚本用于 CentOS 的核心,对于操作系统的功能至关重要。因此,将我们的开发 Python 环境与 CentOS 的核心 Python 环境隔离开来非常重要。

Python scripts are actually used at the core of CentOS for functions vital to the functionality of the operating system. Because of this, it is important to isolate our development Python environment from CentOS' core Python environment.

对于初学者,当前有两种 Python 版本 − Python 2.XPython 3.X

For starters, there are currently two versions of Python − Python 2.X and Python 3.X.

这两个阶段仍然处于积极生产中,尽管版本 2.X 很快就要弃用(已经持续了几年)。两个活跃版本 Python 的原因基本上是修复了版本 2.X 的缺点。这要求版本 3.X 的一些核心功能被重新制作,以一种版本 2.X 脚本无法支持的方式。

Both stages are still in active production, though version 2.X is quickly closing in on depreciation (and has been for a few years). The reason for the two active versions of Python was basically fixing the shortcomings of version 2.X. This required some core functionality of version 3.X to be redone in ways it could not support some version 2.X scripts.

基本上,克服这一过渡的最佳方式是 − 为 3.X 开发,并为旧脚本关注最新的 2.X 版本。当前,CentOS 7.X 依赖于一个半当前版本的 2.X。

Basically, the best way to overcome this transition is − Develop for 3.X and keep up with the latest 2.X version for legacy scripts. Currently, CentOS 7.X relies on a semi-current revision of version 2.X.

截至目前,最新的 Python 版本为 − 3.4.62.7.13

As of this writing, the most current versions of Python are − 3.4.6 and 2.7.13.

不要让这一切混淆或得出任何关于 Python 的结论。设置 Python 环境实际上非常简单。有了 Python 框架和库,实现此任务实际上非常容易。

Don’t let this confuse or draw any conclusions of Python. Setting up a Python environment is really pretty simple. With Python frameworks and libraries, this task is actually really easy to accomplish.

在设置 Python 环境之前,我们需要一个合理的环境。首先,让我们确保 CentOS 安装已完全更新,并安装一些构建实用程序。

Before setting up our Python environments, we need a sane environment. To start, let’s make sure our CentOS install is fully updated and get some building utilities installed.

Step 1 - 更新 CentOS。

Step 1 − Update CentOS.

[root@CentOS]# yum -y update

Step 2 - 安装构建实用程序。

Step 2 − Install build utilities.

[root@CentOS]# yum -y groupinstall "development tools"

Step 3 - 安装一些需要的软件包。

Step 3 − Install some needed packages.

[root@CentOS]# yum install -y zlib-dev openssl-devel sqlite-devel bip2-devel

现在我们需要从源代码安装当前的 Python 2.X 和 3.X。

Now we need to install current Python 2.X and 3.X from source.

  1. Download compressed archives

  2. Extract files

  3. Compile source code

让我们从为 /usr/src/ 中的每个 Python 安装创建一个构建目录开始。

Let’s start by creating a build directory for each Python install in /usr/src/

[root@CentOS]# mkdir -p /usr/src/pythonSource

现在让我们为每个 Python 安装,下载 源 tar 包。

Now let’s download the source tarballs for each −

[root@CentOS]# wget https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tar.xz
[root@CentOS]# wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tar.xz

现在我们需要从归档中提取每个。

Now we need to extract each from the archive.

Step 1 − 安装 xz-libs 并提取 tar 包。

Step 1 − Install xz-libs and extract the tarballs.

[root@CentOS]# yum install xz-libs
[root@CentOS python3]# xz -d ./*.xz
[root@CentOS python3]# ls
Python-2.7.13.tar  Python-3.6.0.tar
[root@CentOS python3]#

Step 2 − 从 tar 包中解包每个安装程序。

Step 2 − Untar each installer from its tarball.

[root@CentOS]# tar -xvf ./Python-2.7.13.tar
[root@CentOS]# tar -xvf ./Python-3.6.0.tar

Step 3 − 进入每个目录并运行 configure 脚本。

Step 3 − Enter each directory and run the configure script.

[root@CentOS]# ./configure --prefix=/usr/local
root@CentOS]# make altinstall

Note − 务必使用 altinstall ,而不是 install。这将使 CentOS 和 Python 的开发版本保持分离。否则,你可能会破坏 CentOS 的功能。

Note − Be sure to use altinstall and not install. This will keep CentOS and development versions of Python separated. Otherwise, you may break the functionality of CentOS.

现在,你将看到编译过程开始。泡杯咖啡,休息 15 分钟,直到编译完成。由于我们已安装了 Python 所需的所有依赖项,因此编译过程应该会顺利完成,而不会出错。

You will now see the compilation process begins. Grab a cup of coffee and take a 15minute break until completion. Since we installed all the needed dependencies for Python, the compilation process should complete without error.

让我们确保已安装了 2.X 的最新版本 Python。

Let’s make sure we have the latest 2.X version of Python installed.

[root@CentOS Python-2.7.13]# /usr/local/bin/python2.7 -V
Python 2.7.13
[root@CentOS Python-2.7.13]#

Note − 你将希望为指向 Python 2.X 开发环境的 shebang 行添加前缀。

Note − You will want to prefix the shebang line pointing to our development environment for Python 2.X.

[root@CentOS Python-2.7.13]# cat ver.py
#!/usr/local/bin/python2.7
import sys
print(sys.version)
[root@CentOS Python-2.7.13]# ./ver.py
2.7.13 (default, Jan 29 2017, 02:24:08)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]

就这样,我们为 2.X 和 3.X 版本分别安装了 Python。从这里,我们可以使用 pipvirtualenv 等每个功能和实用程序来进一步减轻管理 Python 环境和软件包安装的负担。

Just like that, we have separate Python installs for versions 2.X and 3.X. From here, we can use each and utilities such as pip and virtualenv to further ease the burden of managing Python environments and package installation.