Amosplanet

Stay Hungry, Stay Foolish

Install Python 3.9.9 on Centos7

1. Download necessary dependency package
yum install -y libffi-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel zlib gcc make libpcap-devel xz-devel gdbm-devel libxslt-devel libxml2-devel
yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel zlib* libffi-devel readline-devel tk-devel
2. Download python3.9.9
wget https://www.python.org/ftp/python/3.9.9/Python-3.9.9.tgz

If it posted error about wget, most probably wget has not been installed yet.

yum install wget
3. Uncompressed python3.9.9 and make install
tar -zxvf Python-3.9.9.tgz
cd Python-3.9.9
./configure
make&&make install
4. configure Env Varables so that python2/python3, pip can be distinguished
mv /usr/bin/python /usr/bin/python.bak   #删除以前python2.7的软连接
ln -s /usr/local/bin/python3 /usr/bin/python
mv /usr/bin/pip /usr/bin/pip.bak (如果报错说没有pip直接跳过)
以下为没有此文件或目录报错
ln -s /usr/local/bin/pip3 /usr/bin/pip
5. Confirm if Python and Pip have been installed successfully
python
exit()
pip -V
6. Install virtualenv and virtualenvwrapper

Installation Env.: Centos7

Reference: https://www.cnblogs.com/hszstudypy/p/11510933.html

a. Install virtualenv
pip3 install virtualenv
b. Install virtualenvwrapper
pip3 install virtualenvwrapper
c. Configure global varables

(1) find / -name virtualenvwrapper.sh

WORKON_HOME=~/Envs # Set virtualenv management directory, all virtual env. you will download in futre, will be stored here

(2) vi ~/.bashrc (Add contents below)

VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3 #Assign python 解释器的本体(注意此路径随不同的linux环境改变而改变)
export WORKON_HOME=~/Envs
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
source /usr/local/bin/virtualenvwrapper.sh
These shoulde be added at the end of text

(3) execute source ~/.bashrc and make it work

source ~/.bashrc
reboot 

(4) virtualenvwrapper instructions

1) Create virtual Environment

mkvirtualenv demo3

2) Look up virtual env.s

lsvirtualenv or workon

3) Start or switch virtual env.s

workon [env. name]

4) Exit out virtual env.s

deactivate

5) Delete virtual env.s

rmvirtualenv [Virtual env. name]

6) Look up virtual env.s packages

pip list

Troubleshooting

[root@growatt bin]# pip3.12 install numpy
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/numpy/
WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/numpy/
WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/numpy/
WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/numpy/
WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/numpy/
Could not fetch URL https://pypi.org/simple/numpy/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/numpy/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
ERROR: Could not find a version that satisfies the requirement numpy (from versions: none)
ERROR: No matching distribution found for numpy
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
wget https://www.openssl.org/source/openssl-1.1.1g.tar.gz

./config -prefix=/usr/local/openssl no-zlib

如果报错:
gcc: error: unrecognized command line option '-prefix=/usr/local/openssl'
make[1]: *** [apps/app_rand.o] Error 1
make[1]: Leaving directory `/root/openssl-1.1.1g'
make: *** [all] Error 2

执行

./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl


重新安装Python3.12后报错

python3: error while loading shared libraries: libpython3.12.so.1.0: cannot open shared object file: No such file or directory

执行

cp /usr/local/lib/libpython3.12.so.1.0 /usr/lib64/

Leave a Comment

Your email address will not be published. Required fields are marked *