centos7 安裝 python3.9


centos7 安裝 python3.9

本節來自:Josphat Mutai

Step 1: Install Python Dependencies

登陸賬戶(root賬戶或者擁有 sudo 權限的賬戶)

$ ssh username@serveripaddress

更新系統

sudo yum -y install epel-release
sudo yum -y update

重啟系統

sudo reboot

安裝開發者工具

sudo yum groupinstall "Development Tools" -y
sudo yum install openssl-devel libffi-devel bzip2-devel -y

確認 gcc 可用

$ gcc --version
gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-4)
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Step 2: Download latest Python 3.9 Archive

安裝 wget

sudo yum install wget -y

使用 wget 下載 python3.9

wget https://www.python.org/ftp/python/3.9.10/Python-3.9.10.tgz

使用 tar 解壓壓縮包

tar xvf Python-3.9.10.tgz

進入到解壓的文件夾內:

cd Python-3.9*/

Step 2: Install Python 3.9 on CentOS 8 / CentOS 7

配置 python 安裝

./configure --enable-optimizations

上面命令可能產生的輸出如下:

....
checking for the Linux getrandom() syscall... yes
checking for the getrandom() function... yes
checking for library containing shm_open... -lrt
checking for sys/mman.h... (cached) yes
checking for shm_open... yes
checking for shm_unlink... yes
checking for pkg-config... /usr/bin/pkg-config
checking whether compiling and linking against OpenSSL works... yes
checking for X509_VERIFY_PARAM_set1_host in libssl... yes
checking for --with-ssl-default-suites... python
checking for --with-builtin-hashlib-hashes... md5,sha1,sha256,sha512,sha3,blake2
configure: creating ./config.status
config.status: creating Makefile.pre
config.status: creating Misc/python.pc
config.status: creating Misc/python-embed.pc
config.status: creating Misc/python-config.sh
config.status: creating Modules/ld_so_aix
config.status: creating pyconfig.h
creating Modules/Setup.local
creating Makefile

Build Python 3.9 on CentOS 8 / CentOS 7:

sudo make altinstall

耐心等待,如果成功的話,可能會產生如下信息:

....
running install_scripts
copying build/scripts-3.9/pydoc3.9 -> /usr/local/bin
copying build/scripts-3.9/idle3.9 -> /usr/local/bin
copying build/scripts-3.9/2to3-3.9 -> /usr/local/bin
changing mode of /usr/local/bin/pydoc3.9 to 755
changing mode of /usr/local/bin/idle3.9 to 755
changing mode of /usr/local/bin/2to3-3.9 to 755
rm /usr/local/lib/python3.9/lib-dynload/_sysconfigdata__linux_x86_64-linux-gnu.py
rm -r /usr/local/lib/python3.9/lib-dynload/__pycache__
/usr/bin/install -c -m 644 ./Misc/python.man \
	/usr/local/share/man/man1/python3.9.10
if test "xupgrade" != "xno"  ; then \
	case upgrade in \
		upgrade) ensurepip="--altinstall --upgrade" ;; \
		install|*) ensurepip="--altinstall" ;; \
	esac; \
	 ./python -E -m ensurepip \
		$ensurepip --root=/ ; \
fi
Looking in links: /tmp/tmpxqejw3c3
Processing /tmp/tmpxqejw3c3/setuptools-49.2.1-py3-none-any.whl
Processing /tmp/tmpxqejw3c3/pip-20.2.3-py2.py3-none-any.whl
Installing collected packages: setuptools, pip
  WARNING: The script easy_install-3.9 is installed in '/usr/local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  WARNING: The script pip3.9 is installed in '/usr/local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed pip-20.2.3 setuptools-49.2.1

Check Python 3.9 installation on CentOS 8 / CentOS 7

檢查是否成功安裝

$ python3.9 --version
Python 3.9.10

Pip3.9 也會被安裝:

$ pip3.9 --version
pip 21.2.4 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)

升級 pip

$ /usr/local/bin/python3.9 -m pip install --upgrade pip
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: pip in /usr/local/lib/python3.9/site-packages (21.2.4)
Collecting pip
  Downloading pip-21.3.1-py3-none-any.whl (1.7 MB)
     |████████████████████████████████| 1.7 MB 11.1 MB/s
Installing collected packages: pip
Successfully installed pip-21.3.1

這樣安裝第三方庫

$ pip3.9 install virtualenv

查看安裝的包的信息:

$ pip3.9 show virtualenv
Name: virtualenv
Version: 20.14.1
Summary: Virtual Python Environment builder
Home-page: https://virtualenv.pypa.io/
Author: Bernat Gabor
Author-email: gaborjbernat@gmail.com
License: MIT
Location: /usr/local/lib/python3.9/site-packages
Requires: distlib, filelock, platformdirs, six
Required-by: 

虛擬環境

下面部分來自:QYGQH

virtualenv

安裝 virtualenv

$ pip3.9 install virtualenv

創建虛擬環境:

$ virtualenv venv  # 會在當前目錄下創建一個 venv 文件夾

$ virtualenv -p /usr/bin/python2.7 venv    # -p 參數可以指定 Python 版本

激活虛擬環境:

[root@rachel ~]# source venv/bin/activate
(venv) [root@rachel ~]# 

第二行的 (venv) 代表此時已經在虛擬環境中了

激活后,你就可以在虛擬環境安裝第三庫:

$ pip install ***

又或者在虛擬環境中運行python:

(venv) [root@rachel ~]# python
Python 2.7.5 (default, Nov 16 2020, 22:23:17) 

退出虛擬環境:

cd venv
cd bin
deactivate

進入虛擬環境的目錄,執行 deactivate 腳本

刪除虛擬環境

rm -rf venv  # 直接刪除目錄

virtualenvwrapper

可以集中管理虛擬環境的庫。

安裝:

# linux
pip3.9 install virtualenvwrapper
# windows
pip3.9 install virtualenvwrapper-win

~/.bashrc 文件末尾追加兩行:

# 存放虛擬環境的目錄,這個目錄會自動創建
export WORKON_HOME=~/Envs
# 導入腳本,讓腳本可以在任意 shell 窗口中執行。virtrualenvwrapper會安裝到python的bin目錄下,所以該路徑是python安裝目錄下bin/virtualenvwrapper.sh
source /usr/local/bin/virtualenvwrapper.sh

.bashrc 文件是一個用戶配置文件,你可以將一些加載項放到這個文件里,當用戶登陸 shell 時會自動執行這個腳本下的內容,因此我們可以將一些經常使用的腳本,函數,環境變量等寫入這個文件中,從而在登陸 shell 后,可以在任意子 shell 使用這些腳本,函數,環境變量。

export 的作用是聲明全局變量

source 會在當前 shell 上下文中執行命令,而不是創建一個新的 shell 來執行命令,因此需要使用它來導入其他庫

~/.bashrc 文件立即生效:

$ source ~/.bashrc    # 讀入配置文件,立即生效

創建虛擬環境:

mkvirtualenv venv  # 會在 $WORKON_HOME 下創建一個 venv 的虛擬環境

mkvirtualenv --python=/usr/local/python3.5.3/bin/python venv2  # 指定 python 版本

mkvirtualenv -p /usr/bin/python2 venv3  # 指定python版本

上面創建的三個虛擬環境,都會位於 $WORKON_HOME 變量代表的文件夾下

列出所有的虛擬環境

[root@rachel ~]# workon
venv2
venv3
venv 

激活某個虛擬環境:

[root@rachel ~]# workon venv

退出虛擬環境:

[root@rachel ~]# deactivate

刪除虛擬環境

[root@rachel ~]# rmvirtualenv venv


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM