centos7 自带有 python,版本是python2.7

接下来我们手动安装python3,并且配置后可以并存使用。

1.首先,你要知道系统现在的python的位置在哪儿:
[root@root ~]# whereis python
python: /usr/bin/python2.7 /usr/bin/python /usr/lib/python2.7 /usr/lib64/python2.7 /etc/python /usr/include/python2.7 /usr/share/man/man1/python.1.gz
可以知道我们的python在 /usr/bin目录中
[root@root ~]# cd /usr/bin/
[root@root 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就能共存了。

2.因为我们要安装python3,所以要先安装相关包,用于下载编译python3:
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make
运行了以上命令以后,就安装了编译python3所用到的相关依赖

3.默认的,centos7也没有安装pip,不知道是不是因为我安装软件的时候选择的是最小安装的模式。
#运行这个命令添加epel扩展源
yum -y install epel-release

#安装pip
yum install python-pip

4.用pip装wget
pip install wget

5.用wget下载python3的源码包(评论区网友提供了淘宝镜像,替换了官网下载python3,提高下载速度)
wget http://npm.taobao.org/mirrors/python/3.7.5/Python-3.7.5.tar.xz

6.编译python3源码包
#解压
xz -d Python-3.7.5.tar.xz
tar -xf Python-3.7.5.tar

#进入解压后的目录,依次执行下面命令进行手动编译
cd Python-3.7.5
./configure prefix=/usr/local/python3
make && make install

# 如果出现can’t decompress data; zlib not available这个错误,则需要安装相关库
#安装依赖zlib、zlib-devel
yum install zlib zlib
yum install zlib zlib-devel
如果最后没提示出错,就代表正确安装了,在/usr/local/目录下就会有python3目录

7.添加软链接
#将原来的链接备份
mv /usr/bin/python /usr/bin/python.bak

#添加python3的软链接
ln -s /usr/local/python3/bin/python3.7 /usr/bin/python

#测试是否安装成功了
python -V

8.更改yum配置,因为其要用到python2才能执行,否则会导致yum不能正常使用
vi /usr/bin/yum
把#! /usr/bin/python修改为#! /usr/bin/python2

vi /usr/libexec/urlgrabber-ext-down
把#! /usr/bin/python 修改为#! /usr/bin/python2
启动python2:

 

启动python3:

 

附录:

关于,如何下载python3的pip模块,以及pip2和pip3的如何兼容使用,可以看我的另一篇文章:

下载pip模块以及pip2和pip3共存问题
————————————————
版权声明:本文为CSDN博主「大蛇王」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/t8116189520/article/details/81976755

 

====================================================================

 

文章目录
安装Python3和pip3
更换pip为阿里源
常用源
临时更改
永久更改

当前CentOS7系统自带了python2.7.5,因为yum会用到python2,所以不能删除,此次安装了python3之后就保持两个版本长期共存吧
本次安装采用的是下载python源码再编译的方式

下面的操作,按照步骤来就可以了,不要在中途cd 到别的文件目录下,要想查看效果可以用 ls加上对应的目录,不需要切换进去。

安装Python3和pip3
首先进行yum更新

yum update -y
1
接着安装必要的软件

yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel libffi-devel sqlite-devel readline-devel tk-devel libffi-devel gcc make
1
查看现有python命令,这3个文件依赖关系很明显就可以看到

ls -l /usr/bin/python*

# 结果如下
lrwxrwxrwx. 1 root root 7 1月 22 18:57 /usr/bin/python -> python2
lrwxrwxrwx. 1 root root 9 1月 22 18:57 /usr/bin/python2 -> python2.7
-rwxr-xr-x. 1 root root 7144 11月 17 06:23 /usr/bin/python2.7
1
2
3
4
5
6
下载python源码压缩包,需提前安装wget工具

yum install -y wget

wget https://www.python.org/ftp/python/3.8.7/Python-3.8.7.tgz
1
2
3
等待下载完成之后会在当前目录下出现一个tgz包,命令解压这个包到当前目录就可以

tar -xvzf Python-3.8.7.tgz
1
进入解压后的目录,执行编译前的configure操作,提前安装gcc编译器

yum install gcc

cd Python-3.8.7 && ./configure –with-ssl –prefix=/usr/local/python3
1
2
3
稍微解释执行./configure 的命令,目的就是把python的安装目录指定一下,这样的话,里面的一些bin目录、lib目录就都会存放在这个目录下面。如果不指定这个安装目录的话,最后python的安装文件将分散到linux的默认目录,不在一块。我们指定安装目录,以后卸载的话直接删除目录就可以干净卸载了。

编译源码,在Python-3.8.7目录执行以下命令

make && make install
1
编译成功后提示如下信息,setuptools和pip都已经部署成功

Processing /tmp/tmpl3pm5lx_/setuptools-49.2.1-py3-none-any.whl
Processing /tmp/tmpl3pm5lx_/pip-20.2.3-py2.py3-none-any.whl
Installing collected packages: setuptools, pip
Successfully installed pip-20.2.3 setuptools-49.2.1
1
2
3
4
又是一大堆代码,执行完毕之后,我们就可以查看/usr/local/python3/目录了

ls -l /usr/local/python3

# 结果如下
drwxr-xr-x. 2 root root 220 1月 22 19:36 bin
drwxr-xr-x. 3 root root 23 1月 22 19:30 include
drwxr-xr-x. 4 root root 62 1月 22 19:36 lib
drwxr-xr-x. 3 root root 17 1月 22 19:30 share
1
2
3
4
5
6
7
接下来我们还有一点善后工作——创建python3和pip3的软连接

ln -s /usr/local/python3/bin/python3.8 /usr/bin/python3

ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
1
2
3
此时在/usr/bin/目录下,查看文件关系如下

ls -l /usr/bin/python*

# 结果如下
lrwxrwxrwx. 1 root root 7 1月 22 18:57 /usr/bin/python -> python2
lrwxrwxrwx. 1 root root 9 1月 22 18:57 /usr/bin/python2 -> python2.7
-rwxr-xr-x. 1 root root 7144 11月 17 06:23 /usr/bin/python2.7
lrwxrwxrwx. 1 root root 32 1月 22 20:42 /usr/bin/python3 -> /usr/local/python3/bin/python3.8
1
2
3
4
5
6
7
升级pip3

pip3 install –upgrade pip
1
更换pip为阿里源
常用源
清华源
https://pypi.tuna.tsinghua.edu.cn/simple
pypi.tuna.tsinghua.edu.cn
阿里源
https://mirrors.aliyun.com/pypi/simple/
mirrors.aliyun.com
豆瓣源
http://pypi.douban.com/simple
pypi.douban.com
我在这里以阿里源为例

临时更改
pip install <package> -i https://mirrors.aliyun.com/pypi/simple
pip install <package> -i https://mirrors.aliyun.com/pypi/simple –trusted-host mirrors.aliyun.com
1
2
永久更改
首先创建.pip文件夹

mkdir ~/.pip
1
创建pip.conf配置文件

touch ~/.pip/pip.conf
1
修改pip.conf配置文件

vim ~/.pip/pip.conf

# 文件内容如下
[global]
index-url=https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
1
2
3
4
5
6
7
至此,大功告成了!!!

文章知识点与官方知识档案匹配,可进一步学习相关知识
Python入门技能树预备知识安装Python58126 人正在系统学习中
————————————————
版权声明:本文为CSDN博主「Amosヾ」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_44924544/article/details/112993067

 

==========================================================

yum install zlib-devel bzip2-devel openssl-devel ncurses-devel libffi-devel sqlite-devel readline-devel tk-devel libffi-devel gcc make

因为我们要安装python3,所以要先安装相关包,用于下载编译python3:
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make
运行了以上命令以后,就安装了编译python3所用到的相关依赖

————————————————

 

WARNING: Value for scheme.headers does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
distutils: /usr/local/python3.10.1/include/python3.10/UNKNOWN
sysconfig: /usr/local/python3/Python-3.10.1/Include/UNKNOWN
WARNING: Additional context:
user = False
home = None
root = ‘/’
prefix = None
Looking in links: /tmp/tmph2hzr8z0
Processing /tmp/tmph2hzr8z0/setuptools-58.1.0-py3-none-any.whl
Processing /tmp/tmph2hzr8z0/pip-21.2.4-py3-none-any.whl
Installing collected packages: setuptools, pip
WARNING: Value for scheme.headers does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
distutils: /usr/local/python3.10.1/include/python3.10/setuptools
sysconfig: /usr/local/python3/Python-3.10.1/Include/setuptools
WARNING: Value for scheme.headers does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
distutils: /usr/local/python3.10.1/include/python3.10/pip
sysconfig: /usr/local/python3/Python-3.10.1/Include/pip
WARNING: The scripts pip3 and pip3.10 are installed in ‘/usr/local/python3.10.1/bin’ which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use –no-warn-script-location.
Successfully installed pip-21.2.4 setuptools-58.1.0
WARNING: Running pip as the ‘root’ user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
[root@localhost Python-3.10.1]# ping localhost

 

============================================================================

 

 

## 离线安装Python3

1.安装依赖环境

“`sh

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

“`

2.下载安装包

可以选择官网直接下载或者wget命令下载

* 官网(<https://www.python.org/)下载,点击downloads>

* wget:

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

3.解压

“`sh

mkdir -p /usr/local/python3.10

tar -zxvf Python-3.9.0.tgz

“`

4.编译安装

“`sh

cd Python-3.10.1

./configure –prefix=/usr/local/python3.10

make && make install

“`

5.配置环境变量

“`sh

vi /etc/profile

export PYTHON_HOME=/usr/local/python3.10

export PATH=${PYTHON_HOME}/bin:$PATH

source /etc/profile

“`

创建软连接

查看python指向:

“`sh

ls -l /usr/bin | grep python

“`

创建/usr/bin/python3

“`sh

ln -s /usr/local/bin/python3.10/bin/python3 /usr/bin/python3 # 创建python3软连接

rm /usr/bin/python

ln -s /usr/bin/python3 /usr/bin/python

“`

## 离线安装、迁移第三方模块

1.第三方模块下载

* 第三方模块下载可以直接下载whl文件,直接使用pip3 install

* 可在联网机器下载,迁移到离线服务器安装

下面主要介绍在在联网机器下载,迁移到离线服务器安装方式

如果新安装某一个模块,可以直接下载下来

“`sh

pip3 download xxx -d /tmp/packages/

“`

默认情况download是最新版本模块,但需要下载指定模块版本,例如:

“`sh

pip3 download kafka-python==2.0.2 -d /tmp/packages/

“`

* 查看已安装模块

“`sh

pip3 list

Package Version

—————— ———

certifi 2021.10.8

charset-normalizer 2.0.9

idna 3.3

kafka-python 2.0.2

pip 21.2.4

pydruid 0.6.2

requests 2.26.0

setuptools 58.1.0

urllib3 1.26.7

“`

* 将pip3 list的信息生成文档

“`sh

pip3 freeze > requirements.txt

“`

* 将requirement.txt文档中列出的模块信息下载到指定目录

“`sh

# 第一种方式,推荐使用

pip3 download -r requirements.txt -d /tmp/packages/

# 第二种方式

pip3 install –download /tmp/packages -r requirements.txt

“`

* 将下载好的模块copy到离线服务器

“`sh

#是什么格式就安装什么格式的文件

pip3 install xxx.tar.gz

pip3 install xxx.whl

pip3 install xxx.xx

“`

批量安装多个包和依赖包,所有文件放到一个目录中,进入这个目录执行命令

“`sh

pip3 install ./*

“`

issues:

issue1:pip3 安装依赖 ModuleNotFoundError: No module named ‘_ctypes’

解决方案

1.安装libffi-devel

“`sh

yum install libffi-devel

“`

2.重新编译安装

“`sh

./configure –prefix=/usr/local/python3 –enable-optimizations

make && make install

“`

issue2:No matching distribution found for request

“`sh

Collecting request

Could not fetch URL https://pypi.python.org/simple/request/: There was a problem confirming the ssl certificate: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:719) – skipping

Could not find a version that satisfies the requirement request (from versions: )

No matching distribution found for request

“`

分析:网络原因导致

解决方案

“`sh

pip3 install requests -i http://pypi.douban.com/simple/ –trusted-host pypi.douban.com

“`

issue3:pip3 install xxx.tar.gz 报连接错误

“`sh

tar -zxvf xxx.tar.gz

cd xxx

python3 setup.py install

“`
————————————————
版权声明:本文为CSDN博主「import80」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_32613771/article/details/122160440

版权声明:本文为朝闻道,夕死可矣。原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/wendaozhe/p/16396575.html