I have been invited to write an article on how to configure Python 3 on CentOS 7. Usually I choose to directly put my early years to write an article source code compilation MongoDB throw in the past, let them see the source code compilation Python section, but that section is not too detailed, and was recently urged by a lot of people, so it is better to write a separate article.

CentOS 7.3 installs Python 2 by default, and does not provide the Python 3 installation package in the default official yum source. Some users wanted to upgrade to Python3 but could actually have all sorts of problems causing errors, as opposed to the radical Fedora community, which changed the default version to Python3 at 23 (if I remember correctly).

Let’s start with the system environment I’m using, a newly created Docker container. Run cat /etc/redhat-release to check whether CentOS 7.3 is running.

systeminfo

There are two main ways to install a Python environment on a pure CentOS system. One is to compile and install the source code, and the other is to install the RPM package that has been made. As a personal habit, let’s first look at how to install Python 3.6 and configure the virtual environment using source code compilation.

Use source code to compile and install

Based on the environment

  • Install a few necessary packages first to facilitate subsequent operations
➜ yum install wget GCC make ➜ # wgetCopy the code

install_gcc_wget

  • Download the source package from the Python website
➜ wget HTTP: / / https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xzCopy the code
  • Unpack, uncompress
➜ xz -d python-3.6.1.tar. xz ➜ tar -xvf python-3.6.1.tarCopy the code

decompress_archives

  • compile
➜ CD python-3.6.1 ➜./configure --prefix=/usr/local/python3.6 --enable-optimizations ➜Copy the code

–prefix is the desired installation directory, — enable-Optimizations are the optimizations (LTO, PGO, etc.). With this flag, performance is about 10% better (if memory is correct), but it will increase compilation time significantly. However, LTO and PGO are beyond the scope of today’s article. It is suggested that those interested in LTO can look at the specific implementation of GCC. I’m probably not going to be able to write about it hahaha because I can’t stop writing about it.

The following

➜  make
➜  make installCopy the code

make_error

Zlib is not available, so we need to install the dependency

➜ yum install zlib - develCopy the code

The reason for this dependency package is that Python has an important built-in zipImport module for importing modules from Zip packages. If there is no zlib package, it cannot be unzipped and the module cannot be used.

When our dependencies were installed, we re-executed the above steps to compile the installation and found that it was successful.

➜ / usr/local/python3.6 / bin/python3 - version Python 3.6.1Copy the code

install_success

At this point, our Python 3 installation is complete.

  • check

It’s not enough to compile and install. Let’s do some checking.

When we run the Python terminal and type import bz2, we find that the module does not exist. If you’re not familiar with this module, that’s ok. So let’s say “import sqlite3”. Those of you who are familiar with Python are probably familiar with SQlite3, but if you’re still not familiar with sqlite3, you can use the arrow keys. Do you notice that the output becomes something like ^[[D^[[A?

arrow_keys_problem

Those of you familiar with Python source code know what I’m talking about. But if you’re not familiar with it, it doesn’t matter. I’ll tell you. Normally, on the terminal we would expect the previous command to appear when typing the up arrow key, and the left arrow key to move the cursor to the left. But reality is different from our expectations. Why?

There is a little bit of a historical reason for the input device, which is simply the need for a module to escape user input. Back to the main point of this article, the readline module was missing when we compiled and installed it. Now that the problem has been identified, the solution is actually quite simple

➜ yum install readline - develCopy the code

Once the installation is complete, repeat the above steps to compile && install.

  • Check again

We have just solved the problem of arrow key input, but the two modules I mentioned still do not import, so let’s take a closer look at the output of our compilation process. There is a section like this (the output may be inconsistent depending on the system environment)

Python build finished successfully!
The necessary bits to build these optional modules were not found:

_curses               bz2                   _dbm
_gdbm                 _lzma                 _sqlite3
_tkinter              readline

To find the necessary bits, look in setup.py in detect_modules() for the module's name.Copy the code

some_optional_modules_were_not_found

Following the tips above, it’s obvious that we’re missing some (optional) modules that you can ignore if you don’t think you’ll need them. These modules, I use a little bit more, even the TK module that most people don’t touch hahaha. The solution is simply to install the corresponding modules.

➜ # 解决 import bz2 ➜ yum install bzip2-devel ➜ # 解决 import curses ➜ yum install ncurses-devel ➜ # 解决 import sqlite3 ➜ yum install sqlite-devel ➜ # ➜ yum install GDBM -devel ➜ # ➜ yum install xz-devel ➜ # 解决 _tkinter error notification ➜ yum install tk-devel ➜ # 解决 ➜ yum install readline-devel error notification ➜ yum install tk-devel ➜Copy the code

When these modules are installed, recompile will find that the reminder has disappeared, and install.

all_modules_were_found

import_successful

Use the RPM package for installation

The IUS community, Inline with Upstream Stable, is a community that provides new versions of RPM packages. Specific use can view the official documents simply say as long as the following command operation can be.

➜  yum -y install https://centos7.iuscommunity.org/ius-release.rpmCopy the code

yum_install_ius

After adding the IUS, create cache metadata before installing the IUS

➜ yum makecache ➜ yum install python36u ➜ yum -y install python36u- PIP ➜ yum -y install python36u-develCopy the code

yum_install_python36u

Enter python3 from the terminal.

Environment configuration

The installation of Python is only written above, which is directly available if it is installed using RPM. But if you’re compiling from source, you don’t have to enter a bunch of paths every time. So the solution is simple, just add a link.

➜ ln -s/usr/local/python3.6 / bin/python3 / usr/bin/python3Copy the code

ln

Also, try to avoid environmental contamination when multiple Python versions exist on the system. I personally recommend using VirtualEnv to create a separate virtual environment, as I do in my daily life. However, when we have installed Python 3.6, we can directly create the virtual environment by executing the following commands.

➜  python3 -m venv py3
➜  source py3/bin/activate
(py3) ➜  python -V
Python 3.6.1Copy the code

conclusion

There are two main ways to install Python 3.6 on CentOS 7: source code compilation and RPM package installation.

To quickly use source configuration environment but don’t want to care about the specific reasons for the reader, can be used directly on my lot provides the script for installation raw.githubusercontent.com/tao12345666…

In Python 3.6, python3 -m venv venv_name can be used directly to create a virtual environment.


You can subscribe to my official account [MoeLove] via the following QR code.