centos7搭建python3的开发环境(python3,pip3)

本章内容:

1.构建python3及pip3开发环境

2.yum相关(替换源,误删yum恢复方案)

查看Python的位置

[root@centos bin]# whereis python

python2: /usr/bin/python2 /usr/bin/python2.7 /usr/lib/python2.7 /usr/lib64/python2.7 /usr/include/python2.7 /usr/share/man/man1/python2.1.gz

可以知道我们的python在 /usr/bin目录中

[root@centos ~]# cd /usr/bin/
[root@centos bin]# ll python*
lrwxrwxrwx. 1 root root    7 2月   7 09:30 python -> python2
lrwxrwxrwx. 1 root root    9 2月   7 09:30 python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 8月   4 2017 python2.7

python指向的是python2,python2指向的是python2.7,

因此我们可以装个python3,然后将python指向python3,然后python2指向python2.7,那么两个版本的python就能共存了。

先安装相关依赖

[root@pr-dr-13-47 Python-3.7.3]# yum -y install 
gcc make patch libffi-devel python-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel mysql-devel gcc-devel

安装wget

yum install wget

以python3.7.3为例

在线下载地址

wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz

解压

tar -zxvf Python-3.7.3.tgz

创建存放python3的目录

mkdir /usr/local/python3

指定编译和安装的目录,进入Python-3.7.3目录,执行

./configure --prefix=/usr/local/python3 --enable-optimizations --with-ssl

编译,安装

make && make install

查看编译安装后的python执行程序文件夹

[root@pr-dr-13-47 bin]# cd /usr/local/python3
[root@pr-dr-13-47 python3]# ls
bin  include  lib  share

创建软连接

ln -s 源文件或目录 目标文件或目录

[root@kangpc bin]# ln -s /usr/local/python3/bin/bin/python3.7 /usr/bin/python3

[root@kangpc bin]# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

验证python3,python,pip3

[root@pr-dr-13-47 bin]# python3
Python 3.7.3 (default, Jan 18 2021, 14:27:23) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

[root@pr-dr-13-47 bin]# python2
Python 2.7.5 (default, Nov 16 2020, 22:23:17) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

[root@pr-dr-13-47 bin]# pip3 install requests
Collecting requests
  Downloading https://files.pythonhosted.org/packages/29/c1/24814557f1d22c56d50280771a17307e6bf87b70727d975fd6b2ce6b014a/requests-2.25.1-py2.py3-none-any.whl (61kB)
    100% |████████████████████████████████| 61kB 13kB/s 
Collecting idna<3,>=2.5 (from requests)
  Downloading https://files.pythonhosted.org/packages/a2/38/928ddce2273eaa564f6f50de919327bf3a00f091b5baba8dfa9460f3a8a8/idna-2.10-py2.py3-none-any.whl (58kB)
    100% |████████████████████████████████| 61kB 13kB/s 
Collecting urllib3<1.27,>=1.21.1 (from requests)
  Downloading https://files.pythonhosted.org/packages/f5/71/45d36a8df68f3ebb098d6861b2c017f3d094538c0fb98fa61d4dc43e69b9/urllib3-1.26.2-py2.py3-none-any.whl (136kB)
    100% |████████████████████████████████| 143kB 9.8kB/s 
Collecting certifi>=2017.4.17 (from requests)
  Downloading https://files.pythonhosted.org/packages/5e/a0/5f06e1e1d463903cf0c0eebeb751791119ed7a4b3737fdc9a77f1cdfb51f/certifi-2020.12.5-py2.py3-none-any.whl (147kB)
    100% |████████████████████████████████| 153kB 14kB/s 
Collecting chardet<5,>=3.0.2 (from requests)
  Downloading https://files.pythonhosted.org/packages/19/c7/fa589626997dd07bd87d9269342ccb74b1720384a4d739a1872bd84fbe68/chardet-4.0.0-py2.py3-none-any.whl (178kB)
    100% |████████████████████████████████| 184kB 16kB/s 
Installing collected packages: idna, urllib3, certifi, chardet, requests
Successfully installed certifi-2020.12.5 chardet-4.0.0 idna-2.10 requests-2.25.1 urllib3-1.26.2
You are using pip version 19.0.3, however version 20.3.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

到这里python,pip都安装好了,下面的内容是注意的点

以后要进入python2,就敲python,要进入python3,就敲python3,要用pip3,就敲pip3。

这里要格外注意:尽量不要去改原来的python软链,否则python没有指向的python程序,原来写死用python执行的程序就要报错了

假如你把python改为python2,那所有引用到python的地方都要改,比如/usr/bin/yum如果不修改,yum就不能用,会报错

[root@pr-dr-13-47 yum.repos.d]# yum
-bash: /usr/bin/yum: /usr/bin/python: bad interpreter: No such file or directory

下面修改/usr/bin/yum,把#!/usr/bin/python 修改为 #!/usr/bin/python2,修改后如下:

vim /usr/bin/yum
#!/usr/bin/python2
import sys
try:
    import yum
except ImportError:
    print >> sys.stderr, """\
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:

   %s

Please install a package which provides this module, or
verify that the module is installed correctly.

It's possible that the above module doesn't match the
current version of Python, which is:
%s

If you cannot solve this problem yourself, please go to 
the yum faq at:
  http://yum.baseurl.org/wiki/Faq
  
""" % (sys.exc_value, sys.version)
    sys.exit(1)

sys.path.insert(0, '/usr/share/yum-cli')
try:
    import yummain
    yummain.user_main(sys.argv[1:], exit_code=True)
except KeyboardInterrupt, e:
    print >> sys.stderr, "\n\nExiting on user cancel."
    sys.exit(1)

更换yum源,这里用163

curl -O http://mirrors.163.com/.help/CentOS7-Base-163.repo
mv CentOS7-Base-163.repo CentOS-Base.repo

误删了yum,下面是修复方案

下载python2及yum相关组件,为什么要python2,是因为yum的脚本其实是用python2写的,

下载的包一共有5个,如下图,先找到,然后wget http://mirrors.163.com/centos/7/os/x86_64/Packages/yum-plugin-fastestmirror-1.1.31-54.el7_8.noarch.rpm

[root@pr-dr-13-47 yum.repos.d]# pwd
/etc/yum.repos.d
[root@pr-dr-13-47 yum.repos.d]# ls *.rpm -alh
-rw-r--r--. 1 root root 109K Apr  4  2020 python-urlgrabber-3.10-10.el7.noarch.rpm
-rw-r--r--. 1 root root 1.3M Oct 15 03:21 yum-3.4.3-168.el7.centos.noarch.rpm
-rw-r--r--. 1 root root  66K Oct 15 03:21 yum-cron-3.4.3-168.el7.centos.noarch.rpm
-rw-r--r--. 1 root root  28K Jul  4  2014 yum-metadata-parser-1.1.4-10.el7.x86_64.rpm
-rw-r--r--. 1 root root  35K May 14  2020 yum-plugin-fastestmirror-1.1.31-54.el7_8.noarch.rpm

安装

[root@pr-dr-13-47 yum.repos.d]# rpm -ivh *rpm --nodeps --force
Preparing...                          ################################# [100%]
Updating / installing...
   1:yum-metadata-parser-1.1.4-10.el7 ################################# [ 20%]
   2:python-urlgrabber-3.10-10.el7    ################################# [ 40%]
   3:yum-plugin-fastestmirror-1.1.31-5################################# [ 60%]
   4:yum-3.4.3-168.el7.centos         ################################# [ 80%]
   5:yum-cron-3.4.3-168.el7.centos    ################################# [100%]
posted @ 2022-04-05 19:07  我是一言  阅读(218)  评论(0编辑  收藏  举报