• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

基于linux安装python

开发技术 开发技术 4小时前 3次浏览

linux安装python3

默认情况下,linux会自带python

1、查看默认版本

通过命令来查看自带python的版本,python –version命令查看

[root@python ~]# python --version
Python 2.7.5
[root@python ~]# python   //使用python命令窗口
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.
>>> a=1;b=2;c=a+b
>>> print(c)
3
2、查看默认安装位置

可以看出,python是python2的软连接,python2是python2.7的软连接,所以运行他们三个是一样的。

[root@python ~]# whereis python
python: /usr/bin/python /usr/bin/python2.7 /usr/lib/python2.7 /usr/lib64/python2.7 /etc/python /usr/include/python2.7 /usr/share/man/man1/python.1.gz
[root@python ~]# ll /usr/bin/python*
lrwxrwxrwx. 1 root root    7 Dec 17  2020 /usr/bin/python -> python2
lrwxrwxrwx. 1 root root    9 Dec 17  2020 /usr/bin/python2 -> python2.7
-rwxr-xr-x. 1 root root 7216 Apr 11  2018 /usr/bin/python2.7

安装python3

1、在官网下载对应版本的python

登录https://www.python.org/downloads/source/,选择对应版本(以Python3.9.7为例)

基于linux安装python

2、上传到/usr/local/src/下并解压
[root@python src]# pwd
/usr/local/src
[root@python src]# ls
Python-3.9.7.tgz
[root@python src]# tar -zxvf Python-3.9.7.tgz
3、准备依赖环境
yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make libffi-devel
4、执行configure
[root@python src]# cd Python-3.9.7/
[root@python Python-3.9.7]# ./configure --prefix /usr/local/
[root@python Python-3.9.7]# make && make install
[root@python Python-3.9.7]# ls /usr/local/
bin  etc  games  include  lib  lib64  libexec  python3  sbin  share  src
5、添加软连接
[root@python Python-3.9.7]# ln -s /usr/local/python3/bin/python3.9 /usr/bin/python3
[root@python Python-3.9.7]# ln -s /usr/local/python3/bin/pip3.9 /usr/bin/pip3
6、查看版本
[root@python Python-3.9.7]# python3 --version
Python 3.9.7
[root@python Python-3.9.7]# pip3 --version       
pip 21.2.3 from /usr/local/python3/lib/python3.9/site-packages/pip (python 3.9)
7、更改yum配置
[root@python ~]# vim /usr/bin/yum
#! /usr/bin/python 修改为 #! /usr/bin/python2
[root@python ~]# vi /usr/libexec/urlgrabber-ext-down
#! /usr/bin/python 修改为 #! /usr/bin/python2

程序员灯塔
转载请注明原文链接:基于linux安装python
喜欢 (0)