预留linux自带的python2.7

  • yum工具的依赖

修改/usr/bin/yum和/usr/bin/urlgrabber-ext-down两个文件的头文件,即将第一行#!/usr/bin/python 改为 #!/usr/bin/python2时/usr/bin/python但是等会我们需要装两个python,为了避免冲突我们先将/usr/bin/python和/usr/bin/pip修改成/usr/bin/python2和/usr/bin/pip2. - mv /usr/bin/pip /usr/bin/pip2&& mv /usr/bin/python /usr/bin/python2 - python2 和pip2 运行一下确定python2.7的软连接还在。

[[email protected]_101_141_centos home]# python2
Python 2.7.5 (default, Apr 11 2018, 07:36:10) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
[[email protected]_101_141_centos home]# pip2 --version
pip 19.1.1 from /usr/lib/python2.7/site-packages/pip (python 2.7)

修改yum工具的依赖

  • 修改/usr/bin/yum和/usr/bin/urlgrabber-ext-down两个文件的头文件,即将第一行#!/usr/bin/python 改为 #!/usr/bin/python2

安装python

安装之前记得下载依赖: yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel libffi-devel

下载python指定包再安装

两种方法,一个是在官网下载指定版本的python包再安装 我们按照python3.6.2为例子:

# wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz
# yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel //安装可能的依赖库
# tar -zxvf Python-3.6.2.tgz
# cd Python-3.6.2/
# ./configure --prefix=/usr/local/ //安装到/usr/local目录
# make
# make altinstall //此处不能用install安装,因为install不区分版本,会出现多版本混乱的问题

python3.6程序的执行文件:/usr/local/bin/python3.6   python3.6应用程序目录:/usr/local/lib/python3.6   pip3的执行文件:/usr/local/bin/pip3.6   pyenv3的执行文件:/usr/local/bin/pyenv-3.6   更改python默认链接,更改为我们当时留出来的/usr/bin/python

  # cd/usr/bin
# mv  python python.backup
# ln -s /usr/local/bin/python3.6 /usr/bin/python
# ln -s /usr/local/bin/python3.6 /usr/bin/python

pip的位置:

[[email protected]_101_141_centos site-packages]# pwd
/usr/local/lib/python3.6/site-packages
[[email protected]_101_141_centos site-packages]# ls
easy_install.py  pip  pip-9.0.1.dist-info  pkg_resources  __pycache__  README.txt  setuptools  setuptools-28.8.0.dist-info

/usr/local/lib/python3.6/site-packages/目录下我们可以看到pip在这里,然后通过设置软链设置为快捷键:ln -s /usr/local/lib/python3.6/site-packages/pip /usr/bin/pip pip更新后再运行pip缺查无此command,删除/usr/bin/pip设置的软链,运行/usr/local/lib/python3.6/site-packages/pip instll upgrade pip然后再设置软链

yum直接下载安装

# yum search python //先查下yum源支持的python安装版本
# yum install python36 //我的yum源最高支持到python3.6

强烈推荐这个下载方法,因为yum支持的安装版本一般比较稳定。当然如果想尝试新版本可以了,都是慢慢试过来的emmm。这个下载也比较 全,不用担心找不到pip。

找不到pip怎么办

报错:ModuleNotFoundError: No module named '_ctypes' 缺少依赖,解决方法: yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel libffi-devel 额外获取pip的方法: curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py" python get-pip.py.

END,要注意的是python2.7到python3的的时候yu预留得保留python2.7版本, 修改/usr/bin/yum和/usr/bin/urlgrabber-ext-down两个文件的头文件,即将第一行#!/usr/bin/python 改为 #!/usr/bin/python2。

原创文章,转载请注明出处:https://boywithacoin.cn/article/centosan-zhuang-python3-6-pip-sheng-ji-python/