python foundation - python installation and problem solving

Keywords: Python pip SSL OpenSSL

Python 3.7.3 installation

The default version of Python installed on CentOS 7 is 2.7.5. The older version of python that comes with the system is dependent on many other software environments of the system, so you cannot uninstall the original Python, but you can install another version of Python

[root@python ~]# python -V
Python 2.7.5

Preparing for installation

#Possible dependencies for Python 3
[root@python ~]# yum install gcc gcc-c++ make zlib* libffi-devel openssl-devel -y
[root@python ~]# wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz
[root@python ~]# ll Python-3.7.3.tar.xz
-rw-r--r--. 1 root root 17108364 3 February 2619:00 Python-3.7.3.tar.xz
[root@python ~]# tar xf Python-3.7.3.tar.xz -C /usr/local/src

Modify Setup file to start ssl module

[root@python ~]# vim /usr/local/src/Python-3.7.3/Modules/Setup.dist
# Socket module helper for socket(2)
_socket socketmodule.c timemodule.c

# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
SSL=/usr/local/ssl
_ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto

Install python3

[root@python ~]# cd /usr/local/src/Python-3.7.3
[root@python Python-3.7.3]# ./configure --with-ssl --prefix=/usr/local/python3.7.3
[root@python Python-3.7.3]# make && make install
#Create a new soft connection to the newly installed Python 3.7.3
[root@python ~]# ln -s /usr/local/python3.7.3/bin/python3.7 /usr/bin/python3
#Check if ssl was installed successfully
[root@python ~]# python3
>>> import ssl
>>>

During the. /configure process, if the --with-ssl parameter is not added, the functionality of SSL involved in the default software installation is not available, but SSL modules are required for the pip3 process

Install pip

In general, the Python 2 that comes with the Linux system does not have pip installed. Python 3.7.3 comes with pip and can be viewed in the bin directory of Python 3.7.3

[root@python bin]# pwd
/usr/local/python3.7.3/bin
[root@python bin]# ll pip*
-rwxr-xr-x. 1 root root 244 3 27/05:18 pip
-rwxr-xr-x. 1 root root 244 3 27/05:18 pip3
-rwxr-xr-x. 1 root root 244 3 27/05:18 pip3.7
#Create Soft Connections
[root@python ~]# ln -s /usr/local/python3.7.3/bin/pip3 /usr/bin/pip3
#When working with pip, you may be prompted that the PIP version does not match and needs to be updated to execute the update command
[root@python ~]# pip3 install --upgrade pip
Successfully installed pip-20.0.2
[root@python bin]# pip3 -V
pip 20.0.2 from /usr/local/python3.7.3/lib/python3.7/site-packages/pip (python 3.7)

Install Virtual Environment Virtualenv

What is Virtualenv Virtualenv is used to create a virtual Python environment, a project-specific Python environment where different projects can create different Pthon environments (dependencies, versions, etc.). Each environment is completely isolated from each other and there is no need to worry about conflicts with the Python environment of other projects or with the global Python environment.

Install virtualenv module using pip command

[root@python ~]# pip3 install virtualenv
Successfully installed appdirs-1.4.3 distlib-0.3.0 filelock-3.0.12 importlib-[root@python ~]# metadata-1.5.2 six-1.14.0 virtualenv-20.0.14 zipp-3.1.0
ln -s /usr/local/python3.6.7/bin/virtualenv /usr/bin/virtualenv
[root@python ~]# virtualenv --version
15.1.0

New Virtual Environment

[root@python ~]# virtualenv py1 #Create a virtual environment named py1
New python executable in /root/py1/bin/python
Installing setuptools, pip, wheel...done.  

Using the command virtualenv directly, you can create a separate Python runtime environment. With the parameter no-site-packages, third-party packages that have been installed into the system Python environment are not copied into the separate environment

Run virtual environment

[root@python ~]# cd /root/py1/bin/
[root@python bin]# source activate
(py1) [root@python bin]#

Exit virtual environment

(py1) [root@python bin]# deactivate
[root@python bin]#

Other Commands

View the current virtual machine environment directory worken Switch virtual environment workon venv2 Exit virtual environment deactivate Delete virtual environment rmvirtualenv venv

Virtual Environment Management Tools

[root@python ~]# pip3 install virtualenvwrapper
Successfully installed pbr-5.4.4 stevedore-1.32.0 virtualenv-clone-0.5.3 virtualenvwrapper-4.8.4
[root@python ~]# find / -name virtualenvwrapper.sh
/usr/local/python3.7.3/bin/virtualenvwrapper.sh
[root@python ~]# vim .bashrc
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/python3.7.3/bin/virtualenvwrapper.sh
#test
[root@python ~]# mkvirtualenv py1
[root@python ~]# mkvirtualenv --python=/usr/bin/python3 py2	#Specify python environment as version 3

Problem and Solution

(1). /Modules/_ssl.c:57:25: Fatal error: openssl/rsa.h: No file or directory

Error Reason The program you are trying to compile uses OpenSSL but is missing files linked to OpenSSL (libraries and headers)

Solution yum install openssl-devel

(2)pip install -i virtualenv DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7

Maintenance stopped with pip3 https://www.jianshu.com/p/7f1b9a203045

(3) Differences between pip pip3 pip3.7 after Python 3.7 installation https://blog.csdn.net/u013077984/article/details/85333713

(4) Execute virtualenv --version command prompt-bash: virtualenv: command not found

Find virtualenv and create a soft link as follows, then execute the command above to query version information

find / -name virtualenv #Global Find virtualenv
/usr/local/python3/bin/virtualenv #Find Path
ln -s /usr/local/python3.7.3/bin/virtualenv /usr/bin/virtualenv #Add Soft Link

(5) Install virtual environment/usr/bin/python: No module named virtualenvwrapper

Error Reason Two versions of python, version 2.7 and version 3.x, were installed using sudo pip3 install virtualenvwrapper, and python 2.x was used by default at run time, but there was no corresponding module in Python 2.x

Add the following commands to the bashrc file

IRTUALENVWRAPPER_PYTHON=/usr/bin/python3	#Select Python 3 Interpreter

(6)AttributeError: module 'enum' has no attribute 'IntFlag' Error Reason Domestic source updates are not timely, few packages installed

(7) Error in python installation virtual environment: Error while finding module specification for'virtualenvwrapper.hook_loader'

/usr/local/bin/python3: Error while finding module specification for 'virtualenvwrapper.hook_loader' (ModuleNotFoundError: No module named 'virtualenvwrapper')
virtualenvwrapper.sh: There was a problem running the initialization hooks.

If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3 and that PATH is
set properly.

Solution python3-m PIP install --user virtualenvwrapper --upgrade

Posted by Mathy on Thu, 16 Apr 2020 20:28:26 -0700