CentOS7中安装python3
<h2>一、安装前准备工作</h2>
<h3>1、检查系统是否有gcc</h3>
<p>gcc --version ——查询是否安装</p>
<pre><code class="language-bash">[root@localhost ~]# gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39)
Copyright (C) 2015 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.</code></pre>
<p>gcc -v ——查询版本</p>
<pre><code class="language-bash">[root@localhost ~]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)</code></pre>
<h3>2、注意:</h3>
<p>以下教程是让centOS7里面的Python2和Python3共存,并不是替换Python2。由于centos7原本就安装了Python2,而且这个Python2不能被删除,因为有很多系统命令,比如yum都要用到。
输入Python命令,查看可以得知是Python2.7.5版本</p>
<pre><code class="language-bash">[root@localhost ~]# python
Python 2.7.5 (default, Apr 9 2019, 14:30:50)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.</code></pre>
<p>可以查看位置,一般是位于/usr/bin/python目录下。</p>
<pre><code class="language-bash">[root@localhost ~]# which python
/usr/bin/python</code></pre>
<h2>二、下面介绍安装Python3的方法</h2>
<h3>1、Centos7下yum安装Python3.6环境</h3>
<p>目标:安装并配置好Python3.6和pip3</p>
<h4>(1)安装EPEL和IUS软件源</h4>
<pre><code class="language-bash">yum install -y epel-release
yum install -y https://centos7.iuscommunity.org/ius-release.rpm</code></pre>
<h4>(2)安装Python3.6</h4>
<pre><code class="language-bash">yum install -y python36u
yum install -y python36u-devel</code></pre>
<h4>(3)创建python3连接符</h4>
<pre><code class="language-bash">ln -s /bin/python3.6 /bin/python3 默认已建,检查未建的请创建</code></pre>
<h4>(4)安装pip3</h4>
<pre><code class="language-bash">yum install -y python36u-pip</code></pre>
<h4>(5)创建pip3链接符</h4>
<pre><code class="language-bash">ln -s /bin/pip3.6 /bin/pip3 默认已建,检查未建的请创建</code></pre>
<h3>2、以下是编译安装</h3>
<p>但编译还未成功有报错,下次找时间再安装。
来源:
<a href="https://www.cnblogs.com/xiujin/p/11477419.html">https://www.cnblogs.com/xiujin/p/11477419.html</a> 有排错和修改pip3源
<a href="https://www.cnblogs.com/s-seven/p/9105973.html">https://www.cnblogs.com/s-seven/p/9105973.html</a>
<a href="https://blog.csdn.net/qq_24890999/article/details/82780943">https://blog.csdn.net/qq_24890999/article/details/82780943</a></p>
<h4>(1)首先安装依赖包</h4>
<pre><code class="language-bash">yum -y groupinstall "Development tools"
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</code></pre>
<h4>(2)下载Python3</h4>
<p>(可到官网下载最新,wget后面加下载地址)当前最新版本是 Python-3.8.0
<code>wget https://www.python.org/ftp/python/3.8.0/Python-3.8.0.tar.xz</code></p>
<h4>(3)创建编译目录</h4>
<p><code>mkdir /usr/local/python3</code></p>
<h4>(4)解压</h4>
<p><code>[root@localhost ~]tar -xvJf Python-3.8.0.tar.xz</code></p>
<h4>(5)安装</h4>
<p>注意:编译目录不要跟安装的目标目录下相同,否则会报错。</p>
<pre><code class="language-bash">cd Python-3.8.0
./configure --prefix=/usr/local/python3 --enable-optimizations --with-ssl
#第一个指定安装的路径,不指定的话,安装过程中可能软件所需要的文件复制到其他不同目录,删除软件很不方便,复制软件也不方便.
#第二个可以提高python10%-20%代码运行速度.
#第三个是为了安装pip需要用到ssl,后面报错会有提到.
make && make install</code></pre>
<h4>(6)创建软链接</h4>
<pre><code class="language-bash">ln -s /usr/local/python3/bin/python3 /usr/local/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/local/bin/pip3</code></pre>
<h4>(7)验证是否成功</h4>
<pre><code class="language-bash">python3 -V
pip3 -V</code></pre>