多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
### 1.查看是否已经安装Python CentOS 7.2 默认安装了python2.7.5 因为一些命令要用它比如yum 它使用的是python2.7.5。 使用 python -V 命令查看一下是否安装Python 然后使用命令 which python 查看一下Python可执行文件的位置 ```bash # 查看一下Python可执行文件的位置 which python ``` ```bash # 进入 /usr/bin/ 目录 cd /usr/bin/ # 查看与Python相关的文件 ll |grep python ``` ### 2.安装编译依赖环境 ```bash yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel gcc ``` ### 3.下载二进制安装包 Linux中下载Python-3.9.4.tgz安装包比较慢,需要等待几分钟。当然,你也可以在浏览器中下载安装包,然后再上传Linux的目录中。 ```javascript # 文件上传的位置 /data/software # 下载一个Python-3.9.4.tgz安装包 wget https://www.python.org/ftp/python/3.9.4/Python-3.9.4.tgz ``` ### 4.安装Python3.9 本次python3.9.4安装在/data/environment/(具体安装位置根据自己的需求) ```bash #1.解压安装包 tar xzvf Python-3.9.4.tgz #2.python3.7版本之后需要一个新的包libffi-devel yum install libffi-devel -y #3.进入python文件夹,生成编译脚本(指定安装目录) cd /data/software/Python-3.9.4 ./configure --prefix=/data/environment/Python-3.9.4 #4.编译: make #5.编译成功后,编译安装: make install #6.检查python3.9的编译器: cd /data/environment/Python-3.9.4/bin/python3.9 ./python3.9 然后就出现这样的界面 [root@localhost bin]# ./python3.9 Python 3.9.4 (default, Jan 11 2022, 10:42:56) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> ``` ### 5.建立Python3和pip3的软链: ```bash ln -s /data/environment/Python-3.9.4/bin/python3.9 /usr/bin/python3 ln -s /data/environment/Python-3.9.4/bin/pip3 /usr/bin/pip3 ``` ### 6.将/data/environment/Python-3.9.4/bin加入PATH ```bash 1.编辑/etc/profile vim /etc/profile 2.按“I”,然后贴上下面内容: # User specific environment and startup programs PATH=$PATH:$HOME/bin:/data/environment/Python-3.9.4/bin export PATH 3.按ESC,输入:wq回车退出 4.修改完记得执行行下面的命令,让上一步的修改生效: source ~/.bash_profile ``` ### 7.检查Python3及pip3是否正常可用: ```bash [root@localhost data]# pip3 -V pip 20.2.3 from /data/environment/Python-3.9.4/lib/python3.9/site-packages/pip (python 3.9) [root@localhost data]# python3 -V Python 3.9.4 [root@localhost data]# python3 Python 3.9.4 (default, Jan 11 2022, 10:42:56) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> ``` `使用python3和pip3的名称,是为了不影响Linux内置的python2.7的环境,不影响yum命令的正常使用。`