详解Linux Centos7下安装Python的方法
<p><strong>1.查看是否已经安装Python</strong></p>
<p>
Centos7默认安装了python2.7.5 因为一些命令要用它比如yum 它使用的是python2.7.5。</p>
<p>
使用python -V命令查看一下是否安装Python:</p>
<p>
<img style="max-width:100%!important;height:auto!important;"title="详解Linux Centos7下安装Python的方法" alt="详解Linux Centos7下安装Python的方法" src="https://zhuji.jb51.net/uploads/img/202305/cd057d71d7f193cb8107584b82e766da.jpg"></p>
<p>
然后查看一下Python可执行文件的位置: </p>
<p>
<img style="max-width:100%!important;height:auto!important;"title="详解Linux Centos7下安装Python的方法" alt="详解Linux Centos7下安装Python的方法" src="https://zhuji.jb51.net/uploads/img/202305/4dbe0f07b92c433109a9fa0c2dc7cb7a.jpg"></p>
<p>
进入到bin目录:</p>
<p>
<img style="max-width:100%!important;height:auto!important;"title="详解Linux Centos7下安装Python的方法" alt="详解Linux Centos7下安装Python的方法" src="https://zhuji.jb51.net/uploads/img/202305/4fe799fa22c8069ce48caee10a638f4a.jpg"></p>
<p>
可以看到可执行文件python指向python2,python2又指向python2.7也就是说Python命令执行的系统预装的Python2.7。 </p>
<p>
要安装Python的其他版本先执行mv python python.bak命令备份python文件。</p>
<p>
<img style="max-width:100%!important;height:auto!important;"title="详解Linux Centos7下安装Python的方法" alt="详解Linux Centos7下安装Python的方法" src="https://zhuji.jb51.net/uploads/img/202305/a2ef9d2cf17f9309e015398272134e2b.jpg"></p>
<p>
<strong>2.安装新版本的Python</strong></p>
<p>
下载Python新版本https://www.python.org/downloads/source/</p>
<p>
下载tgz包就可以了。其实下面的2个包其一都可以使用</p>
<p>
Python-3.5.1.tgz (这个不是编译过的东西,不能解压之后直接使用)</p>
<p>
Python-3.5.1.tar.xz (这个是pthon的源码)</p>
<p>
讲下载的tgz包上传到Linux服务器。</p>
<p>
或者执行wget命令直接下载到服务器:</p>
<div class="jb51code">
<div>
<div class="syntaxhighlighterbash" id="highlighter_45964">
<div class="toolbar">
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td class="gutter">
<div class="line number1 index0 alt2">
1</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2">
<code class="bash plain">wget https:</code><code class="bash plain">//www</code><code class="bash plain">.python.org</code><code class="bash plain">/ftp/python/3</code><code class="bash plain">.5.1</code><code class="bash plain">/Python-3</code><code class="bash plain">.5.1.tgz</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div class="codetool" id="codetool">
<div class="code_n">
<textarea></textarea>
</div>
</div>
</div>
<p>
解压缩:</p>
<div class="jb51code">
<div>
<div class="syntaxhighlighterbash" id="highlighter_255021">
<div class="toolbar">
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td class="gutter">
<div class="line number1 index0 alt2">
1</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2">
<code class="bash functions">tar</code> <code class="bash plain">-zxvf Python-3.5.1.tgz</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div class="codetool" id="codetool">
<div class="code_n">
<textarea></textarea>
</div>
</div>
</div>
<p>
加压缩之后该目录下出现Python-3.5.1文件夹,进入到文件夹下执行编译安装。</p>
<p>
<img style="max-width:100%!important;height:auto!important;"title="详解Linux Centos7下安装Python的方法" alt="详解Linux Centos7下安装Python的方法" src="https://zhuji.jb51.net/uploads/img/202305/fff00089ecba70e2fb592a1e43e5ef7a.jpg"></p>
<p>
执行 ./configure --prefix=/usr/local/python/python3 命令。./configure命令执行完毕之后创建一个文件creating Makefile,供下面的make命令使用 执行make install之后就会把程序安装到我们指定的目录中去。</p>
<p>
Configure是一个可执行脚本,它有很多选项,在待安装的源码路径下使用命令./configure –help输出详细的选项列表。</p>
<p>
其中--prefix选项是配置安装的路径,如果不配置该选项,安装后可执行文件默认放在/usr /local/bin,库文件默认放</p>
<p>
在/usr/local/lib,配置文件默认放</p>
<p>
在/usr/local/etc,其它的资源文件放</p>
<p>
在/usr /local/share,比较凌乱。</p>
<p>
如果配置--prefix,如:</p>
<p>
./configure --prefix=/usr/local/test</p>
<p>
可以把所有资源文件放在/usr/local/test的路径中,不会杂乱。</p>
<p>
用了—prefix选项的另一个好处是卸载软件或移植软件。当某个安装的软件不再需要时,只须简单的删除该安装目录,就可以把软件卸载得干干净净;移植软件只需拷贝整个目录到另外一个机器即可(相同的操作系统)。</p>
<p>
当然要卸载程序,也可以在原来的make目录下用一次make uninstall,但前提是make文件指定过uninstall。</p>
<p>
然后执行make、make install命令。</p>
<p>
安装成功之后进入到python3文件夹可以看到Python的可执行文件目录、lib目录等相关目录。</p>
<p>
<img style="max-width:100%!important;height:auto!important;"title="详解Linux Centos7下安装Python的方法" alt="详解Linux Centos7下安装Python的方法" src="https://zhuji.jb51.net/uploads/img/202305/7eb99ded00edd94ec0808441b636458a.jpg"></p>
<p>
执行ln -s /usr/local/python/python3/bin/python3 /usr/bin/python命令创建软连接。执行python -V查看Python是否安装成功。</p>
<p>
<img style="max-width:100%!important;height:auto!important;"title="详解Linux Centos7下安装Python的方法" alt="详解Linux Centos7下安装Python的方法" src="https://zhuji.jb51.net/uploads/img/202305/036087beacad015ba36d8ae15d378a16.jpg"></p>
<p>
安装成功:</p>
<p>
<img style="max-width:100%!important;height:auto!important;"title="详解Linux Centos7下安装Python的方法" alt="详解Linux Centos7下安装Python的方法" src="https://zhuji.jb51.net/uploads/img/202305/0fec2ec69ae8a72a80dd89e1e099cfd2.jpg"></p>
<p>
<strong>3.修改yum配置文件</strong></p>
<p>
因为yum使用python2,因此替换为python3后可能无法正常工作,继续使用这个python2.7.5</p>
<p>
因此修改yum配置文件(vi /usr/bin/yum)。</p>
<p>
把文件头部的#!/usr/bin/python改成#!/usr/bin/python2.7保存退出即可。</p>
<p>
因为yum使用python2,因此替换为python3后可能无法正常工作,继续使用这个python2.7.5因此修改yum配置文件(vi /usr/bin/yum)。把文件头部的#!/usr/bin/python改成#!/usr/bin/python2.7保存退出即可</p>
<p>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。</p>
<p>
原文链接:http://www.cnblogs.com/lclq/p/5620196.html</p>
頁:
[1]