Python3 installation under Linux centos

Most systems under Linux come with a version of python2.x by default. The most common version is python2.6 or python2.7. The default python is depended on by many programs in the system. For example, yum under centos is written by python2, so the default version Don't delete it easily, otherwise there will be some problems. If you need to use the latest Python3, then we can compile and install the source package to a separate directory, which has no effect on the default environment of the system. The two environments of python3 and python2 can coexist.

  First go to the python official website to download the source package of python3, URL: https://www.python.org/. Here you can choose the first download. The downloaded source package is: Python-3.5.1.tgz, ready to be installed. (python comes with pip)

  Some necessary modules are needed before python installation, such as openssl, readline, etc. If there is no such module, there will be some problems later, for example, without openssl, ssl-related functions are not supported, and pip3 will report an error directly when installing the module; there is no readline Then the python interactive interface delete key and arrow keys cannot be used normally. As for what modules are needed, python will give a prompt after the make, and you can install it through the prompts. In addition, thanks to the gardener Glory_Lion for the reply; the following is the required module Pre-installed dependencies:

yum -y install zlib zlib-devel
yum -y install bzip2 bzip2-devel
yum -y install ncurses ncurses-devel
yum -y install readline readline-devel
yum -y install openssl openssl-devel
yum -y install openssl-static
yum -y install xz lzma xz-devel
yum -y install sqlite sqlite-devel
yum -y install gdbm gdbm-devel
yum -y install tk tk-devel
   安装上面这些python内置模块基本上就比较全了,接下来可以安装python了,编译过程中会自动包含这些依赖.

  Release files:

tar -xvzf Python-3.5.1.tgz

  Enter the directory:

cd Python-3.5.1/

  Configure (specify) the installation directory (install to the /usr/python directory), other options are generally default:

./configure --prefix=/usr/python

  Next compile the source code:

make

  Execute the installation:

make install

  The whole process takes about 5-10 minutes. After the installation is successful, the installation directory is in /usr/python.

  The original python in the system is in /usr/bin/python. You can see through ls -l that python is a soft link that links to python2.7 in this directory

  We don't need to delete this, do not make any changes to the original default environment, just create a new python3 soft link, but when you need to execute python3 code, python should be changed to python3, or the python script header interpreter should be changed to

#!/usr/bin/python3

  Create a soft link here as follows:

$ ln -s /usr/python/bin/python3 /usr/bin/python3

  This is established, and you can call python3 by directly executing the python3 command in the future. In addition, if you look closely at the bin directory under the python installation directory, in fact, python3 is also a soft link, linking to python3.5.1, so multiple links are also for more Version management is more convenient.

将python添加到操作系统的 PATH 环境变量:
回到home目录:
cd
ls -al
修改PATH环境变量
vi .bash_profile

Modify PATHReload .bash_profile for the changes to take effect

source .bash_profile
重新打开一个终端,如果配置没有生效,则注销一下用户,即可生效。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324981186&siteId=291194637