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