Why take this installation process as an example? Because the mainstream Linux being developed and deployed is CentOS, and the version most people are currently using is CentOS Linux release 7.x, which has integrated Python2.7 and PIP. If not, you can install it through yum. CentOS 6 comes with Python2.6.6 and does not have PIP. Most open Source Python projects currently require Python at 2.7 or higher, so if you are deploying your project on a CentOS 6 machine, you will need to upgrade Python and install PIP. That seems like it, but there are a lot of problems with the implementation, so I’ll walk you through it step by step to give you a root understanding of Python multi-environment installation.

No other requirements, just install PIP

PIP installation is generally used in two ways, one is yum installation, one is the source code package installation, this direct search in Baidu can also be found a lot of useful content. Here are two ways to introduce respectively

centos7

If you do not have piPs on your machine, execute the following command

yum -y install python-pip

Copy the code

If the installation fails, follow the instructions to resolve the problem. Some people may need to install epEL-Release first

yum -y install epel-release

yum -y install python-pip

Copy the code

centos6

cd /opt/

wget https://pypi.python.org/packages/11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/pip-9.0.1.tar.gz#md5=35f01da33009719497f01a4ba69d63c9



The tar - XZVF PIP - 9.0.1. Tar. Gz

cdPIP - 9.0.1

python setup.py install

Copy the code

Latest methods (not for everyone)

This is also the method described in the official documentation, but will not succeed for some operating systems and Python versions

Download the installation script

cd /opt/

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

Run the installation script

python get-pip.py

Copy the code

Note: PIP is associated with whichever version of Python the installation script is run with

 python3 get-pip.py    Run the installation script. PIP is associated with python3

 python2 get-pip.py    Run the installation script. PIP is associated with PYTHon2

Copy the code

Multiple Python installations coexist

Before going on, it is recommended to watch ==> Until you understand the virtual environment, you really know the Python environment

I’m going to use Centos6, which is more representative, and there are all kinds of problems that can happen, but you might not, depending on the order in which you do it.

As mentioned earlier, centos6 has Python2.6 by default, so let’s go ahead and install a Python2.7 version for demonstration purposes. You asked me why I didn’t go directly to Python3.7+, no reason, just want to make trouble (because the company project does not support Python3, and must be python2.7 or higher, subsequent upgrade 3.7) without further words, look at the following command:

# step 1

cd /opt/

Wget HTTP: / / http://python.org/ftp/python/2.7.3/Python-2.7.3.tar.bz2# Download the Python version package

The tar - JXVF Python - 2.7.3. Tar..bz2Unzip the file

# step 2

CD Python - 2.7.3# enter folder

Check the installation platform properties, whether the system has the library required for compilation, and whether the version of the library meets the compilation requirements

./configure 

make all &&  make install && make clean && make distclean  Compile, install, and clean

# step 3

mv /usr/bin/python /usr/bin/python26.6.  Move back up the original Python instruction

ln -s /usr/local/bin/python27. /usr/bin/python Set the Python bin command to soft link pointing

Copy the code

Enter Python -v. The following output is displayed

[root @ alpha - silvergrd - 107-13 Python - 2.7.3]python -V

Python 27.3.

Copy the code

Ln -s /usr/local/bin/python2.7 /usr/bin/python2.7 ln -s /usr/local/bin/python2.7 All my effects are as shown

Do I need to explain this place? You see the officer to ask the colleague beside good or Baidu on the line, do not repeat.

Pay attention to

If you have already executed the previous header # only want to install PIP, then you are in trouble, if not, then you are not in big trouble!!

PIP was installed first

Python2.7 -m PIP install scrapy PIP install scrapy PIP install scrapy PIP install scrapy PIP install scrapy This is because Python2.7 has no matching PIP associated with it


while
python -m pip install scrapyWill not be

cd /opt/

cdPIP - 9.0.1

Python2.7 setup. Py installPIP is associated with Python2.7

Copy the code

Note: PIP is associated with whichever version of Python is used to run the installation script and is executed again without this error

PIP not installed, now want to install PIP

cd /opt/

wget https://pypi.python.org/packages/11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/pip-9.0.1.tar.gz#md5=35f01da33009719497f01a4ba69d63c9



The tar - XZVF PIP - 9.0.1. Tar. Gz

cdPIP - 9.0.1

# 2 Either way, it depends

Python2.7 setup.py install (python setup.py install)

Copy the code

How to use

Now that multiple versions of Python and PIP are installed, how do you use it to download a package? Take installing a scrapy as an example

python27. -m pip install scrapy -i https://pypi.tuna.tsinghua.edu.cn/simple

# or

python -m pip install scrapy -i https://pypi.tuna.tsinghua.edu.cn/simple



# python2.7: The language version to use

# -m: Run modules as scripts

# -i: use the mirror source

Copy the code

How to solve all kinds of errors

Because it is not written while operating, in fact, in the implementation of the process encountered a lot of errors, but THESE errors I do not remember in which steps occurred, all unified classification here, if you happen to encounter, you can directly follow the solution, Centos6 more problems, CentoS7 will not have so many problems. Centos6 is used as an example because there are many problems.

No module named setuptools

wget --no-check-certificate https://bootstrap.pypa.io/ez_setup.py

python2.7 ez_setup.py --insecure

Copy the code

pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available

If the — with-ssl parameter is not included in the./configure process, SSL functionality is not available in the default installation. PIP requires SSL modules, and this functionality is not available because it is not specified.

The solution is to compile and install python2.7.3 again using the following procedure:

The openSSL-devel package is missing

rpm -aq|grep openssl-devel

# yum install openssl-devel

yum install openssl-devel -y 

View the installation result

rpm -aq|
grep openssl 

Compile and install python2.7 again and use the following procedure to implement the compile and install

cd /opt/Python2.7.3

./configure --with-ssl # add SSL option

make all &&  make install && make clean && make distclean

Copy the code

No module named _sqlite3

yum install sqlite-devel

cd /opt/Python27.3.

./configure

make all &&  make install && make clean && make distclean

Copy the code

Yum is unavailable

vi /usr/bin/yum

Copy the code

The first line #! /usr/bin/python uses Python. If your Version of Python changes, that is, python -v is not the original content, you need to change this content to the original Python content, for example

mv /usr/bin/python /usr/bin/python999

Copy the code

So #! /usr/bin/python is changed to #! The/usr/bin/python999. Of course, you can also replace the contents of the yum source file that matches the current Python version, but it is not recommended (trouble).

/lib64/libc.so.6:version glibc_2.14

Linux/Centos /lib64/libc.so.6: version ‘glibc_2.14’ not found

No module named yum

In this case, you can use the following methods to rectify the fault: download the original system package again

Download the RPM package of the corresponding operating system

wget http://mirrors.ustc.edu.cn/centos/ / OS 6.10/x86_64/Packages/python-2.6.6-66.el6_8.x86_64.rpm

wget http://mirrors.ustc.edu.cn/centos/ / OS 6.10/x86_64/Packages/python-devel-2.6.6-66.el6_8.x86_64.rpm

wget http://mirrors.ustc.edu.cn/centos/ / OS 6.10/x86_64/Packages/python-libs-2.6.6-66.el6_8.x86_64.rpm

wget http://mirrors.ustc.edu.cn/centos/ / OS 6.10/x86_64/Packages/python-pycurl-7.19.0-9.el6.x86_64.rpm

wget http://mirrors.ustc.edu.cn/centos/ / OS 6.10/x86_64/Packages/python-setuptools-0.6.10-4.el6_9.noarch.rpm

wget http://mirrors.ustc.edu.cn/centos/ / OS 6.10/x86_64/Packages/python-urlgrabber-3.9.1-11.el6.noarch.rpm

wget http://mirrors.ustc.edu.cn/centos/ / OS 6.10/x86_64/Packages/python-iniparse-0.3.1-2.1.el6.noarch.rpm

wget http://mirrors.ustc.edu.cn/centos/ / OS 6.10/x86_64/Packages/rpm-python-4.8.0-59.el6.x86_64.rpm

wget http://mirrors.ustc.edu.cn/centos/ / OS 6.10/x86_64/Packages/yum-3.2.29-81.el6.centos.noarch.rpm

wget http://mirrors.ustc.edu.cn/centos/ / OS 6.10/x86_64/Packages/yum-metadata-parser-1.1.2-16.el6.x86_64.rpm

wget http://mirrors.ustc.edu.cn/centos/ / OS 6.10/x86_64/Packages/yum-utils-1.1.30-41.el6.noarch.rpm

wget http://mirrors.ustc.edu.cn/centos/ / OS 6.10/x86_64/Packages/yum-plugin-fastestmirror-1.1.30-41.el6.noarch.rpm

wget http://mirrors.ustc.edu.cn/centos/ / OS 6.10/x86_64/Packages/yum-plugin-protectbase-1.1.30-41.el6.noarch.rpm

wget http://mirrors.ustc.edu.cn/centos/ / OS 6.10/x86_64/Packages/yum-plugin-aliases-1.1.30-41.el6.noarch.rpm

wget http://mirrors.ustc.edu.cn/centos/ / OS 6.10/x86_64/Packages/yum-utils-1.1.30-41.el6.noarch.rpm

wget http://mirrors.ustc.edu.cn/centos/ / OS 6.10/x86_64/Packages/yum-utils-1.1.30-41.el6.noarch.rpm

wget http://mirrors.ustc.edu.cn/centos/ / OS 6.10/x86_64/Packages/yum-utils-1.1.30-41.el6.noarch.rpm

wget http://mirrors.ustc.edu.cn/centos/ / OS 6.10/x86_64/Packages/yum-utils-1.1.30-41.el6.noarch.rpm

wget http://mirrors.ustc.edu.cn/centos/ / OS 6.10/x86_64/Packages/yum-3.2.29-81.el6.centos.noarch.rpm

wget http://mirrors.ustc.edu.cn/centos/ / OS 6.10/x86_64/Packages/yum-utils-1.1.30-41.el6.noarch.rpm



Install (depending on what is missing)

rpm -Uvh --replacepkgs rpm-python*.rpm

rpm -Uvh --replacepkgs yum*.rpm

# or

RPM -uvh --replacepkgs yum*. RPM -force --nodeps

Copy the code

No module named zlib

yum install zlib

# recompile Python

./configure --with-zlib

make all &&  make install && make clean && make distclean

Copy the code

pip is a package and cannot be directly executed

pip install -U pip

pip install -U setuptools

pip install wheel

Copy the code

conclusion

Some of the things I didn’t go into detail about and some of the things I forgot about are probably different for each person, so I picked the one that had the most problems. I’ve already done Python2.6 and Python2.7, so add the other Python versions and do the same again. The only thing that has changed is the package download.

So if you have any questions, leave a comment at the bottom, and I’ll answer them all, as well as remember what I forgot in the process.

In fact, venv and Conda can also be used to solve such problems, but I don’t want to rely on third-party tools, so I didn’t study this. I will add this article later.

Pay attention and don’t get lost

The article continues to update every week, you can wechat search “ten minutes to learn programming” the first time to read and urge more, if this article is written well, feel something ~ for praise 👍 for attention ❤️ for share ❤️ everyone’s support and recognition, is the biggest power of my creation, we see the next article!