前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >linux上安装python3, 保留python2

linux上安装python3, 保留python2

原创
作者头像
cg错过
修改2020-11-25 18:13:28
2.1K0
修改2020-11-25 18:13:28
举报
文章被收录于专栏:程序笔记

首先, linux系统中自带python2, 所以可以直接使用python命令, 注意, 安装python2不能卸载

  1. 安装依赖

rpm

代码语言:javascript
复制
sudo yum install zlib-devel bzip2-devel sqlite sqlite-devel openssl-devel

deb

代码语言:javascript
复制
sudo apt-get install make build-essential libssl-dev zlib1g-dev libbz2-dev libsqlite3-dev

首先要先安装依赖, 不然后面安装python时将不会自动安装pip

  1. 下载python

ftp站点

代码语言:javascript
复制
https://www.python.org/ftp/python

在上面的页面中找到需要下载的包, 然后使用命令

代码语言:javascript
复制
wget https://www.python.org/ftp/python/3.5.4/Python-3.5.4.tar.xz

下载完成之后在当前运行wget命令的目录下就可以看到下载的文件

然后tar命令解压, 如下

代码语言:javascript
复制
tar -xf Python-3.5.4.tar.xz

就会得到Python-3.5.4名文件夹

创建一个需要存放安装文件的文件夹,例如我创建的路径/usr/local/python3/

代码语言:javascript
复制
mkdir -p /usr/local/python3

然后cd进入Python-3.5.4

执行编译安装

代码语言:javascript
复制
sudo ./configure --prefix=/usr/local/python3/

sudo make

sudo make install

执行完成make install后, 其输出如

代码语言:javascript
复制
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-9.0.1 setuptools-28.8.0
[root@10-9-183-29 Python-3.5.4]# 

即表示安装完成了, 并安装了pip

之后查看安装路径的结构信息如

代码语言:javascript
复制
[root@10-9-183-29 Python-3.5.4]# cd /usr/local/python3/
[root@10-9-183-29 python3]# ls
bin  include  lib  share
[root@10-9-183-29 python3]# 
[root@10-9-183-29 python3]# cd bin
[root@10-9-183-29 bin]# ls
2to3  2to3-3.5  easy_install-3.5  idle3  idle3.5  pip3  pip3.5  pydoc3  pydoc3.5  python3  python3.5  python3.5-config  python3.5m  python3.5m-config  python3-config  pyvenv  pyvenv-3.5
[root@10-9-183-29 bin]# ls -l
total 24128
lrwxrwxrwx 1 root root        8 Sep  3 10:25 2to3 -> 2to3-3.5
-rwxr-xr-x 1 root root      109 Sep  3 10:25 2to3-3.5
-rwxr-xr-x 1 root root      250 Sep  3 10:25 easy_install-3.5
lrwxrwxrwx 1 root root        7 Sep  3 10:25 idle3 -> idle3.5
-rwxr-xr-x 1 root root      107 Sep  3 10:25 idle3.5
-rwxr-xr-x 1 root root      222 Sep  3 10:25 pip3
-rwxr-xr-x 1 root root      222 Sep  3 10:25 pip3.5
lrwxrwxrwx 1 root root        8 Sep  3 10:25 pydoc3 -> pydoc3.5
-rwxr-xr-x 1 root root       92 Sep  3 10:25 pydoc3.5
lrwxrwxrwx 1 root root        9 Sep  3 10:25 python3 -> python3.5
-rwxr-xr-x 2 root root 12334016 Sep  3 10:25 python3.5
lrwxrwxrwx 1 root root       17 Sep  3 10:25 python3.5-config -> python3.5m-config
-rwxr-xr-x 2 root root 12334016 Sep  3 10:25 python3.5m
-rwxr-xr-x 1 root root     3088 Sep  3 10:25 python3.5m-config
lrwxrwxrwx 1 root root       16 Sep  3 10:25 python3-config -> python3.5-config
lrwxrwxrwx 1 root root       10 Sep  3 10:25 pyvenv -> pyvenv-3.5
-rwxr-xr-x 1 root root      244 Sep  3 10:25 pyvenv-3.5
[root@10-9-183-29 bin]# 

这里可以通过配置环境变量来使用python3命令, 当然也可以直接将python3命令通过软连接到/usr/bin目录下

不过看上面的输出可以知道

python3python3.5的软连接

pip3则没有软连接

则要使用python3命令即可以直接将这个python3文件复制到/usr/bin目录下

要使用pip3命令则可以使用创建软连接的方式来使用

如 在当前目录(安装目录的bin)下执行命令

代码语言:javascript
复制
cp python3 /usr/bin/

ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

这样就完成了

/usr/bin目录下查看

代码语言:javascript
复制
[root@10-9-183-29 bin]# ls -l | grep pip
-rwxr-xr-x. 1 root root       2291 Jul 31  2015 lesspipe.sh
lrwxrwxrwx  1 root root         27 Sep  3 10:39 pip3 -> /usr/local/python3/bin/pip3
[root@10-9-183-29 bin]# ls -l | grep python
lrwxrwxrwx  1 root root         27 Sep  3 10:39 pip3 -> /usr/local/python3/bin/pip3
lrwxrwxrwx  1 root root          7 Sep  2 21:42 python -> python2
lrwxrwxrwx  1 root root          9 Sep  2 21:42 python2 -> python2.7
-rwxr-xr-x  1 root root       7216 Jul 13 21:07 python2.7
-rwxr-xr-x  1 root root   12334016 Sep  3 10:37 python3
[root@10-9-183-29 bin]# 

output:

代码语言:javascript
复制
[root@10-9-183-29 automatic_monitor]# yum install openssl-devel
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
10gen                                                                                                                                                                                                                                                  | 2.5 kB  00:00:00     
base                                                                                                                                                                                                                                                   | 3.6 kB  00:00:00     
centosplus                                                                                                                                                                                                                                             | 3.4 kB  00:00:00     
epel                                                                                                                                                                                                                                                   | 3.2 kB  00:00:00     
extras                                                                                                                                                                                                                                                 | 3.4 kB  00:00:00     
updates                                                                                                                                                                                                                                                | 3.4 kB  00:00:00     
(1/2): epel/x86_64/updateinfo                                                                                                                                                                                                                          | 939 kB  00:00:00     
(2/2): epel/x86_64/primary                                                                                                                                                                                                                             | 3.6 MB  00:00:01     
epel                                                                                                                                                                                                                                                              12663/12663
Resolving Dependencies
--> Running transaction check
---> Package openssl-devel.x86_64 1:1.0.2k-12.el7 will be installed
--> Processing Dependency: zlib-devel(x86-64) for package: 1:openssl-devel-1.0.2k-12.el7.x86_64
--> Processing Dependency: krb5-devel(x86-64) for package: 1:openssl-devel-1.0.2k-12.el7.x86_64
--> Running transaction check
---> Package krb5-devel.x86_64 0:1.15.1-19.el7 will be installed
--> Processing Dependency: libkadm5(x86-64) = 1.15.1-19.el7 for package: krb5-devel-1.15.1-19.el7.x86_64
--> Processing Dependency: libverto-devel for package: krb5-devel-1.15.1-19.el7.x86_64
--> Processing Dependency: libselinux-devel for package: krb5-devel-1.15.1-19.el7.x86_64
--> Processing Dependency: libcom_err-devel for package: krb5-devel-1.15.1-19.el7.x86_64
--> Processing Dependency: keyutils-libs-devel for package: krb5-devel-1.15.1-19.el7.x86_64
---> Package zlib-devel.x86_64 0:1.2.7-17.el7 will be installed
--> Running transaction check
---> Package keyutils-libs-devel.x86_64 0:1.5.8-3.el7 will be installed
---> Package libcom_err-devel.x86_64 0:1.42.9-12.el7_5 will be installed
---> Package libkadm5.x86_64 0:1.15.1-19.el7 will be installed
---> Package libselinux-devel.x86_64 0:2.5-12.el7 will be installed
--> Processing Dependency: libsepol-devel(x86-64) >= 2.5-6 for package: libselinux-devel-2.5-12.el7.x86_64
--> Processing Dependency: pkgconfig(libsepol) for package: libselinux-devel-2.5-12.el7.x86_64
--> Processing Dependency: pkgconfig(libpcre) for package: libselinux-devel-2.5-12.el7.x86_64
---> Package libverto-devel.x86_64 0:0.2.5-4.el7 will be installed
--> Running transaction check
---> Package libsepol-devel.x86_64 0:2.5-8.1.el7 will be installed
---> Package pcre-devel.x86_64 0:8.32-17.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================================================================================================================================================================================================
 Package                                                                  Arch                                                        Version                                                              Repository                                                    Size
==============================================================================================================================================================================================================================================================================
Installing:
 openssl-devel                                                            x86_64                                                      1:1.0.2k-12.el7                                                      base                                                         1.5 M
Installing for dependencies:
 keyutils-libs-devel                                                      x86_64                                                      1.5.8-3.el7                                                          base                                                          37 k
 krb5-devel                                                               x86_64                                                      1.15.1-19.el7                                                        updates                                                      269 k
 libcom_err-devel                                                         x86_64                                                      1.42.9-12.el7_5                                                      updates                                                       31 k
 libkadm5                                                                 x86_64                                                      1.15.1-19.el7                                                        updates                                                      175 k
 libselinux-devel                                                         x86_64                                                      2.5-12.el7                                                           base                                                         186 k
 libsepol-devel                                                           x86_64                                                      2.5-8.1.el7                                                          base                                                          77 k
 libverto-devel                                                           x86_64                                                      0.2.5-4.el7                                                          base                                                          12 k
 pcre-devel                                                               x86_64                                                      8.32-17.el7                                                          base                                                         480 k
 zlib-devel                                                               x86_64                                                      1.2.7-17.el7                                                         base                                                          50 k

Transaction Summary
==============================================================================================================================================================================================================================================================================
Install  1 Package (+9 Dependent packages)

Total download size: 2.8 M
Installed size: 6.0 M
Is this ok [y/d/N]: y
Downloading packages:
(1/10): keyutils-libs-devel-1.5.8-3.el7.x86_64.rpm                                                                                                                                                                                                     |  37 kB  00:00:00     
(2/10): libcom_err-devel-1.42.9-12.el7_5.x86_64.rpm                                                                                                                                                                                                    |  31 kB  00:00:00     
(3/10): krb5-devel-1.15.1-19.el7.x86_64.rpm                                                                                                                                                                                                            | 269 kB  00:00:00     
(4/10): libkadm5-1.15.1-19.el7.x86_64.rpm                                                                                                                                                                                                              | 175 kB  00:00:00     
(5/10): libselinux-devel-2.5-12.el7.x86_64.rpm                                                                                                                                                                                                         | 186 kB  00:00:00     
(6/10): libverto-devel-0.2.5-4.el7.x86_64.rpm                                                                                                                                                                                                          |  12 kB  00:00:00     
(7/10): libsepol-devel-2.5-8.1.el7.x86_64.rpm                                                                                                                                                                                                          |  77 kB  00:00:00     
(8/10): openssl-devel-1.0.2k-12.el7.x86_64.rpm                                                                                                                                                                                                         | 1.5 MB  00:00:00     
(9/10): zlib-devel-1.2.7-17.el7.x86_64.rpm                                                                                                                                                                                                             |  50 kB  00:00:00     
(10/10): pcre-devel-8.32-17.el7.x86_64.rpm                                                                                                                                                                                                             | 480 kB  00:00:00     
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                                                                                          11 MB/s | 2.8 MB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : keyutils-libs-devel-1.5.8-3.el7.x86_64                                                                                                                                                                                                                    1/10 
  Installing : libcom_err-devel-1.42.9-12.el7_5.x86_64                                                                                                                                                                                                                   2/10 
  Installing : libkadm5-1.15.1-19.el7.x86_64                                                                                                                                                                                                                             3/10 
  Installing : libsepol-devel-2.5-8.1.el7.x86_64                                                                                                                                                                                                                         4/10 
  Installing : pcre-devel-8.32-17.el7.x86_64                                                                                                                                                                                                                             5/10 
  Installing : libselinux-devel-2.5-12.el7.x86_64                                                                                                                                                                                                                        6/10 
  Installing : libverto-devel-0.2.5-4.el7.x86_64                                                                                                                                                                                                                         7/10 
  Installing : krb5-devel-1.15.1-19.el7.x86_64                                                                                                                                                                                                                           8/10 
  Installing : zlib-devel-1.2.7-17.el7.x86_64                                                                                                                                                                                                                            9/10 
  Installing : 1:openssl-devel-1.0.2k-12.el7.x86_64                                                                                                                                                                                                                     10/10 
  Verifying  : krb5-devel-1.15.1-19.el7.x86_64                                                                                                                                                                                                                           1/10 
  Verifying  : zlib-devel-1.2.7-17.el7.x86_64                                                                                                                                                                                                                            2/10 
  Verifying  : 1:openssl-devel-1.0.2k-12.el7.x86_64                                                                                                                                                                                                                      3/10 
  Verifying  : libverto-devel-0.2.5-4.el7.x86_64                                                                                                                                                                                                                         4/10 
  Verifying  : libselinux-devel-2.5-12.el7.x86_64                                                                                                                                                                                                                        5/10 
  Verifying  : pcre-devel-8.32-17.el7.x86_64                                                                                                                                                                                                                             6/10 
  Verifying  : libsepol-devel-2.5-8.1.el7.x86_64                                                                                                                                                                                                                         7/10 
  Verifying  : libkadm5-1.15.1-19.el7.x86_64                                                                                                                                                                                                                             8/10 
  Verifying  : libcom_err-devel-1.42.9-12.el7_5.x86_64                                                                                                                                                                                                                   9/10 
  Verifying  : keyutils-libs-devel-1.5.8-3.el7.x86_64                                                                                                                                                                                                                   10/10 

Installed:
  openssl-devel.x86_64 1:1.0.2k-12.el7                                                                                                                                                                                                                                        

Dependency Installed:
  keyutils-libs-devel.x86_64 0:1.5.8-3.el7  krb5-devel.x86_64 0:1.15.1-19.el7  libcom_err-devel.x86_64 0:1.42.9-12.el7_5  libkadm5.x86_64 0:1.15.1-19.el7  libselinux-devel.x86_64 0:2.5-12.el7  libsepol-devel.x86_64 0:2.5-8.1.el7  libverto-devel.x86_64 0:0.2.5-4.el7 
  pcre-devel.x86_64 0:8.32-17.el7           zlib-devel.x86_64 0:1.2.7-17.el7  

Complete!
[root@10-9-183-29 automatic_monitor]# yum install zlib-devel bzip2-devel sqlite sqlite-devel openssl-devel
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Package zlib-devel-1.2.7-17.el7.x86_64 already installed and latest version
Package sqlite-3.7.17-8.el7.x86_64 already installed and latest version
Package 1:openssl-devel-1.0.2k-12.el7.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package bzip2-devel.x86_64 0:1.0.6-13.el7 will be installed
---> Package sqlite-devel.x86_64 0:3.7.17-8.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================================================================================================================================================================================================
 Package                                                              Arch                                                           Version                                                               Repository                                                    Size
==============================================================================================================================================================================================================================================================================
Installing:
 bzip2-devel                                                          x86_64                                                         1.0.6-13.el7                                                          base                                                         218 k
 sqlite-devel                                                         x86_64                                                         3.7.17-8.el7                                                          base                                                         104 k

Transaction Summary
==============================================================================================================================================================================================================================================================================
Install  2 Packages

Total download size: 322 k
Installed size: 748 k
Is this ok [y/d/N]: y
Downloading packages:
(1/2): bzip2-devel-1.0.6-13.el7.x86_64.rpm                                                                                                                                                                                                             | 218 kB  00:00:00     
(2/2): sqlite-devel-3.7.17-8.el7.x86_64.rpm                                                                                                                                                                                                            | 104 kB  00:00:00     
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                                                                                         2.3 MB/s | 322 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : sqlite-devel-3.7.17-8.el7.x86_64                                                                                                                                                                                                                           1/2 
  Installing : bzip2-devel-1.0.6-13.el7.x86_64                                                                                                                                                                                                                            2/2 
  Verifying  : bzip2-devel-1.0.6-13.el7.x86_64                                                                                                                                                                                                                            1/2 
  Verifying  : sqlite-devel-3.7.17-8.el7.x86_64                                                                                                                                                                                                                           2/2 

Installed:
  bzip2-devel.x86_64 0:1.0.6-13.el7                                                                                                     sqlite-devel.x86_64 0:3.7.17-8.el7                                                                                                    

Complete!
[root@10-9-183-29 automatic_monitor]# 


文章来自微信公众号: 程序员品

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档