瞎说什么大实话 發表於 2020-5-16 16:42:00

centos 7 升级Python3.8

<h1 class="postTitle"><span style="font-size: 15px">&nbsp;自由自在 闲话少叙...</span></h1>
<p><strong>1.下载官方Python包</strong><br>使用wget下载Python-3.8.2包</p>
<div class="cnblogs_code">
<pre>wget https:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">www.python.org/ftp/python/3.8.2/Python-3.8.2.tar.xz</span></pre>
</div>
<p><strong>2.解压Python-3.8.2</strong><br>tar.xz压缩格式,需要用xz命令解压为.tar,用tar命令解包,目录权限不足时需使用:sudo</p>
<div class="cnblogs_code">
<pre>xz -d Python-3.8.2<span style="color: rgba(0, 0, 0, 1)">.tar.xz
tar xvf Python</span>-3.8.2.tar</pre>
</div>
<p><strong>另外一种格式</strong></p>
<div class="cnblogs_code">
<pre>wget --no-check-certificate https:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">www.python.org/ftp/python/3.8.2/Python-3.8.2.tgz<br></span></pre>
<pre>tar xzvf Python-3.8.2.tgz</pre>
</div>
<p>&nbsp;</p>
<p><strong>3.Python-3.8.2安装到指定目录</strong><br>编译安装时不要使用“make &amp;&amp; make install”<br>一定要分两步,先make(编译),然后再 make install(编译安装)。<br>非root用户需使用sudo</p>
<p>1. 创建安装目录</p>
<div class="cnblogs_code">
<pre>mkdir /usr/local/python3/</pre>
</div>
<p>2. 指定安装目录<br>不要在./configure 后添加–enable-optimizations参数,在低版本的gcc版本中带有–enable-optimizations参数时会出现“Could not import runpy module”安装错误,在下面会介绍</p>
<p># 进入Python-3.8.2目录</p>
<div class="cnblogs_code">
<pre>cd Python-3.8.2</pre>
</div>
<p># 配置(指定安装目录)</p>
<div class="cnblogs_code">
<pre>./configure --prefix=/usr/local/python3</pre>
</div>
<p>3. 编译</p>
<div class="cnblogs_code">
<pre>make</pre>
</div>
<p>4. 编译安装</p>
<div class="cnblogs_code">
<pre>make install</pre>
</div>
<p>5. 更换系统默认Python版本<br>(1).备份原系统旧版本python</p>
<div class="cnblogs_code">
<pre>mv /usr/bin/python /usr/bin/<span style="color: rgba(0, 0, 0, 1)">python.bak
mv </span>/usr/bin/pip /usr/bin/pip.bak</pre>
</div>
<p>(2).配置环境变量:创建新版本Python和pip的软链接</p>
<div class="cnblogs_code">
<pre>ln -s /usr/local/python3/bin/python3.8 /usr/bin/<span style="color: rgba(0, 0, 0, 1)">python
ln </span>-s /usr/local/python3/bin/pip3 /usr/bin/pip</pre>
</div>
<p>5. 查看版本</p>
<div class="cnblogs_code">
<pre>python -V</pre>
</div>
<p>6. 修改因更换Python版本影响的其它命令功能<br>(1).yum</p>
<p>vim /usr/bin/yum<br>把最顶部的#!/usr/bin/python改为:#!/usr/bin/python2.7<br>wq 保存,已经是的无须更改<br>(2).urlgrabber-ext-down</p>
<p>vim /usr/libexec/urlgrabber-ext-down<br>把最顶部的#!/usr/bin/python改为:#!/usr/bin/python2.7<br>wq 保存,已经是的无须更改<br>(2).firewalld</p>
<p>vim /usr/sbin/firewalld<br>把最顶部的#!/usr/bin/python改为:#!/usr/bin/python2.7<br>wq 保存,已经是的无须更改<br>(2).firewall-cm</p>
<p>vim /usr/bin/firewall-cm<br>把最顶部的#!/usr/bin/python改为:#!/usr/bin/python2.7<br>wq 保存,已经是的无须更改</p>
<p>7. 出现Could not import runpy module“”安装错误说明<br>错误示例:</p>
<p>make build_all CFLAGS_NODIST=" -fprofile-use -fprofile-correction" LDFLAGS_NODIST=""<br>make: Entering directory `/usr/local/src/Python-3.8.0'<br>./python -E -S -m sysconfig --generate-posix-vars ;\<br>if test $? -ne 0 ; then \<br>        echo "generate-posix-vars failed" ; \<br>        rm -f ./pybuilddir.txt ; \<br>        exit 1 ; \<br>fi<br>Could not import runpy module<br>Traceback (most recent call last):<br>File "/usr/local/src/Python-3.8.0/Lib/runpy.py", line 15, in &lt;module&gt;<br>    import importlib.util<br>File "/usr/local/src/Python-3.8.0/Lib/importlib/util.py", line 14, in &lt;module&gt;<br>    from contextlib import contextmanager<br>File "/usr/local/src/Python-3.8.0/Lib/contextlib.py", line 4, in &lt;module&gt;<br>    import _collections_abc<br>SystemError: &lt;built-in function compile&gt; returned NULL without setting an error<br>generate-posix-vars failed<br>make: *** Error 1<br>make: Leaving directory `/usr/local/src/Python-3.8.0'<br>make: *** Error 2</p>
<p><br>导致原因:<br>在低版本的gcc中带有–enable-optimizations参数<br>解决方法(不推荐使用方法1):<br>1、升级gcc至高版本,gcc 8.1.0已修复此问题<br>2、./configure参数中去掉–enable-optimizations</p>
<p><br>————————————————<br>原文链接:https://blog.csdn.net/IT_Migrant_worker/java/article/details/104725904</p><br><br>
来源:https://www.cnblogs.com/adao21/p/12901127.html
頁: [1]
查看完整版本: centos 7 升级Python3.8