[实践OK]在CentOS7上安装并配置Python3.8.1环境,新版本Python3.6.2支持Ansible的,和pip3 pip 安装 pdb调试程序如 python -m pdb mail.py,涉及到yum里的python本维持旧版本也需要修改其指向旧版本。
背景:Python很火,且连一些如ESP8266的硬件都支持Python了。
目前Python存在两个版本Python2和Python3系列,且这两个版本同时在更新与维护。到底是选择Python2还是选择Python3,取决于当前要使用的库、框架支持哪个版本。因此一台电脑上可能需要同时安装Python2和Python3两个版本。本文主要讲述的是如何在centos7系统中存在python2系列的同时安装python3.8.1(目前python3系列的最新版本)
1.安装升级相应的软件包
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make
2.下载python 3.8.1 的源码包
wget http://mirrors.sohu.com/python/3.8.1/Python-3.8.1.tgz
3.安装python3.8.1
tar zxvf Python-3.8.1.tgz -C /opt/
cd /opt/Python-3.8.1/
./configure --prefix=/usr/local/python3
make && make install
若遇到编译问题:
zipimport.ZipImportError: can't decompress data; zlib not available #make && make install
yum install zlib zlib-devel -y
4.配置python,配置python3 pip3 软链接,设置软连接:目的是让我们直接在任何地方输入python3命令都可以得到python解释器的运行。
更改/usr/bin/python链接:
ln -s /usr/local/python3/bin/python3.8.1 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
mv /usr/bin/pip{,.backup}
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip #python2-pip没有,就没有这个pip.
查看是否安装成功
python3 -V
pip3 -V
更改/usr/bin/python默认链接,实现升级:
/usr/bin/python --version
Python 2.7.5
mv /usr/bin/python{,.old.backup}
ln -sf /usr/local/python3/bin/python3.8 /usr/bin/python
5.查看当python的版本,出现版本信息即为正确安装
python3 --version
安装python3.7出现ModuleNotFoundError: No module named '_ctypes'报错(重新编译一次就OK了)
来自:https://mhl.xyz/Python/python37_ctypes.html
6.更改yum脚本的python依赖
yum localinstall mysql-5.7.12-171123111505.el7.centos.x86_64.rpm
File "/usr/libexec/urlgrabber-ext-down", line 28
except OSError, e:
^
# ls /usr/bin/yum*
# ls yum*
yum yum-config-manager yum-debug-restore yum-groups-manager
yum-builddep yum-debug-dump yumdownloader
更改以上文件头为
#!/usr/bin/python 改为 #!/usr/bin/python2
用Sed 一行搞定,参考自[实践OK]经常用到之linux sed 批量替换字符串,及模糊替换和模糊拼接的例子。http://jackxiang.com/post/2324/:
/usr/bin/yum-groups-manager #!/usr/bin/python -tt
/usr/bin/yumdownloader #直接python后面有一个空格
/usr/bin/yum-debug-restore #!/usr/bin/python -tt
/usr/bin/yum-debug-dump #!/usr/bin/python -tt
/usr/bin/yum-config-manager #!/usr/bin/python -tt
/usr/bin/yum-builddep #!/usr/bin/python -tt
/usr/bin/yum #python后面无空格
势必要有两种替换:
sed -i 's/#!\/usr\/bin\/python /#!\/usr\/bin\/python2.7 /' /usr/bin/yum-builddep
sed -i 's/#!\/usr\/bin\/python /#!\/usr\/bin\/python2.7 /' /usr/bin/yum-config-manager
sed -i 's/#!\/usr\/bin\/python /#!\/usr\/bin\/python2.7 /' /usr/bin/yum-debug-dump
sed -i 's/#!\/usr\/bin\/python /#!\/usr\/bin\/python2.7 /' /usr/bin/yum-debug-restore
sed -i 's/#!\/usr\/bin\/python /#!\/usr\/bin\/python2.7 /' /usr/bin/yumdownloader
sed -i 's/#!\/usr\/bin\/python /#!\/usr\/bin\/python2.7 /' /usr/bin/yum-groups-manager
sed -i 's/#!\/usr\/bin\/python$/#!\/usr\/bin\/python2.7/' /usr/bin/yum
sed -i 's/#!\/usr\/bin\/python$/#!\/usr\/bin\/python2.7/' /usr/libexec/urlgrabber-ext-down
这个-tt的没有啥用,只是起到警告作用,完全在替换时不要:
-t Issue a warning when a source file mixes tabs and spaces for indentation in a way that makes it
depend on the worth of a tab expressed in spaces. Issue an error when the option is given twice.
-t 当源文件以制作缩进的方式混合制表符和空格时发出警告
取决于用空格表示的选项卡的价值。 当该选项被赋予两次时发出一个错误。
7.测试用Python的Pip3安装Python扩展:
rpm -qf /bin/pip
python2-pip-8.1.2-5.el7.noarch
/bin/pip --version
pip 9.0.1 from /usr/lib/python2.7/site-packages (python 2.7)
pip3 --version
pip 9.0.1 from /usr/local/python3/lib/python3.8.1/site-packages (python 3.8.1)
pip3 install PyEmail
Collecting PyEmail
Downloading https://files.pythonhosted.org/packages/ca/39/caf0436670b6fa9f072e322fab1a2b04d5631bdbdc8ea9857230aae02dd5/PyEmail-0.0.1.zip
Installing collected packages: PyEmail
Running setup.py install for PyEmail ... done
Successfully installed PyEmail-0.0.1
You are using pip version 9.0.1, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
pip3 install ipdb
Collecting ipdb
Downloading https://files.pythonhosted.org/packages/80/fe/4564de08f174f3846364b3add8426d14cebee228f741c27e702b2877e85b/ipdb-0.11.tar.gz
Requirement already satisfied: setuptools in /usr/local/python3/lib/python3.8.1/site-packages (from ipdb)
Collecting ipython>=5.0.0 (from ipdb)
Downloading https://files.pythonhosted.org/packages/b1/7f/91d50f28af3e3a24342561983a7857e399ce24093876e6970b986a0b6677/ipython-6.4.0-py3-none-any.whl (750kB)
62% |████████████████████ | 471kB 54kB/s eta 0:00:06
好一会下载安装就能装上这个,但是提示:You are using pip version 9.0.1, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip
pip install --upgrade pip
pip install ipdb #再一次安装这个Python的调试扩展
python -m pdb mail.py
最后,这些脚本总结起来放一块就这些,方便后期再安装之用:
来自:https://blog.csdn.net/hobohero/article/details/54381475
来自:https://blog.csdn.net/wjqwinn/article/details/75633714
目前Python存在两个版本Python2和Python3系列,且这两个版本同时在更新与维护。到底是选择Python2还是选择Python3,取决于当前要使用的库、框架支持哪个版本。因此一台电脑上可能需要同时安装Python2和Python3两个版本。本文主要讲述的是如何在centos7系统中存在python2系列的同时安装python3.8.1(目前python3系列的最新版本)
1.安装升级相应的软件包
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make
2.下载python 3.8.1 的源码包
wget http://mirrors.sohu.com/python/3.8.1/Python-3.8.1.tgz
3.安装python3.8.1
tar zxvf Python-3.8.1.tgz -C /opt/
cd /opt/Python-3.8.1/
./configure --prefix=/usr/local/python3
make && make install
若遇到编译问题:
zipimport.ZipImportError: can't decompress data; zlib not available #make && make install
yum install zlib zlib-devel -y
4.配置python,配置python3 pip3 软链接,设置软连接:目的是让我们直接在任何地方输入python3命令都可以得到python解释器的运行。
更改/usr/bin/python链接:
ln -s /usr/local/python3/bin/python3.8.1 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
mv /usr/bin/pip{,.backup}
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip #python2-pip没有,就没有这个pip.
查看是否安装成功
python3 -V
pip3 -V
更改/usr/bin/python默认链接,实现升级:
/usr/bin/python --version
Python 2.7.5
mv /usr/bin/python{,.old.backup}
ln -sf /usr/local/python3/bin/python3.8 /usr/bin/python
5.查看当python的版本,出现版本信息即为正确安装
python3 --version
安装python3.7出现ModuleNotFoundError: No module named '_ctypes'报错(重新编译一次就OK了)
来自:https://mhl.xyz/Python/python37_ctypes.html
6.更改yum脚本的python依赖
yum localinstall mysql-5.7.12-171123111505.el7.centos.x86_64.rpm
File "/usr/libexec/urlgrabber-ext-down", line 28
except OSError, e:
^
# ls /usr/bin/yum*
# ls yum*
yum yum-config-manager yum-debug-restore yum-groups-manager
yum-builddep yum-debug-dump yumdownloader
更改以上文件头为
#!/usr/bin/python 改为 #!/usr/bin/python2
用Sed 一行搞定,参考自[实践OK]经常用到之linux sed 批量替换字符串,及模糊替换和模糊拼接的例子。http://jackxiang.com/post/2324/:
/usr/bin/yum-groups-manager #!/usr/bin/python -tt
/usr/bin/yumdownloader #直接python后面有一个空格
/usr/bin/yum-debug-restore #!/usr/bin/python -tt
/usr/bin/yum-debug-dump #!/usr/bin/python -tt
/usr/bin/yum-config-manager #!/usr/bin/python -tt
/usr/bin/yum-builddep #!/usr/bin/python -tt
/usr/bin/yum #python后面无空格
势必要有两种替换:
sed -i 's/#!\/usr\/bin\/python /#!\/usr\/bin\/python2.7 /' /usr/bin/yum-builddep
sed -i 's/#!\/usr\/bin\/python /#!\/usr\/bin\/python2.7 /' /usr/bin/yum-config-manager
sed -i 's/#!\/usr\/bin\/python /#!\/usr\/bin\/python2.7 /' /usr/bin/yum-debug-dump
sed -i 's/#!\/usr\/bin\/python /#!\/usr\/bin\/python2.7 /' /usr/bin/yum-debug-restore
sed -i 's/#!\/usr\/bin\/python /#!\/usr\/bin\/python2.7 /' /usr/bin/yumdownloader
sed -i 's/#!\/usr\/bin\/python /#!\/usr\/bin\/python2.7 /' /usr/bin/yum-groups-manager
sed -i 's/#!\/usr\/bin\/python$/#!\/usr\/bin\/python2.7/' /usr/bin/yum
sed -i 's/#!\/usr\/bin\/python$/#!\/usr\/bin\/python2.7/' /usr/libexec/urlgrabber-ext-down
这个-tt的没有啥用,只是起到警告作用,完全在替换时不要:
-t Issue a warning when a source file mixes tabs and spaces for indentation in a way that makes it
depend on the worth of a tab expressed in spaces. Issue an error when the option is given twice.
-t 当源文件以制作缩进的方式混合制表符和空格时发出警告
取决于用空格表示的选项卡的价值。 当该选项被赋予两次时发出一个错误。
7.测试用Python的Pip3安装Python扩展:
rpm -qf /bin/pip
python2-pip-8.1.2-5.el7.noarch
/bin/pip --version
pip 9.0.1 from /usr/lib/python2.7/site-packages (python 2.7)
pip3 --version
pip 9.0.1 from /usr/local/python3/lib/python3.8.1/site-packages (python 3.8.1)
pip3 install PyEmail
Collecting PyEmail
Downloading https://files.pythonhosted.org/packages/ca/39/caf0436670b6fa9f072e322fab1a2b04d5631bdbdc8ea9857230aae02dd5/PyEmail-0.0.1.zip
Installing collected packages: PyEmail
Running setup.py install for PyEmail ... done
Successfully installed PyEmail-0.0.1
You are using pip version 9.0.1, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
pip3 install ipdb
Collecting ipdb
Downloading https://files.pythonhosted.org/packages/80/fe/4564de08f174f3846364b3add8426d14cebee228f741c27e702b2877e85b/ipdb-0.11.tar.gz
Requirement already satisfied: setuptools in /usr/local/python3/lib/python3.8.1/site-packages (from ipdb)
Collecting ipython>=5.0.0 (from ipdb)
Downloading https://files.pythonhosted.org/packages/b1/7f/91d50f28af3e3a24342561983a7857e399ce24093876e6970b986a0b6677/ipython-6.4.0-py3-none-any.whl (750kB)
62% |████████████████████ | 471kB 54kB/s eta 0:00:06
好一会下载安装就能装上这个,但是提示:You are using pip version 9.0.1, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip
pip install --upgrade pip
pip install ipdb #再一次安装这个Python的调试扩展
python -m pdb mail.py
最后,这些脚本总结起来放一块就这些,方便后期再安装之用:
来自:https://blog.csdn.net/hobohero/article/details/54381475
来自:https://blog.csdn.net/wjqwinn/article/details/75633714
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/9795/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
最后编辑: jackxiang 编辑于2019-12-30 14:54
评论列表