ubuntu安装、切换、使用不同版本的python


本篇文章是在 win10系统下 ubuntu16.04虚拟机上进行配置的。ubuntu16.04自带2.7.2和3.5.2版本的python,但现在有项目需要3.7版本的python,但我的虚拟机磁盘大小有限没法安装anaconda,所以想安装个新版本的python,并随时能调换不同版本进行使用。

1. 下载python安装包和openssl

1.1 python安装包

方法一:终端命令安装

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

方法二:手动官网下载

python官网下载linux版本的压缩包

请添加图片描述

1.2 openssl安装包

因为python版本需要和openssl的版本匹配,在Python3.7之后的版本,依赖的openssl必须要是1.1或者1.0.2之后的版本,ubuntu系统自带的openssl版本过低了,不安装的话使用pip时会报错SSLError("Can't connect to HTTPS URL because the SSL module is not available.")

wget https://www.openssl.org/source/openssl-1.1.1l.tar.gz

2. 解压安装

我的两个安装包都放置在Downloads路径下

2.1 安装openssl
zhouying@ubuntu:~/Downloads# wget https://www.openssl.org/source/openssl-1.1.1l.tar.gz
zhouying@ubuntu:~/Downloads# tar xzvf openssl-1.1.1l.tar.gz
zhouying@ubuntu:~/Downloads# cd openssl-1.1.1l
zhouying@ubuntu:~/Downloads/openssl-1.1.1l# sudo -s # 输入密码,开启root权限
# prefix是指定安装路径,shared zlib库是在安装时寻找zlib库依赖的
root@ubuntu:~/Downloads/openssl-1.1.1l# ./config --prefix=/usr/local/openssl shared zlib
root@ubuntu:~/Downloads/openssl-1.1.1l# make && make install

备份原openssl

root@ubuntu:~/Downloads/openssl-1.1.1l# mv /usr/bin/openssl /usr/bin/openssl.bak
root@ubuntu:~/Downloads/openssl-1.1.1l# mv /usr/include/openssl /usr/include/openssl.bak

替换原来的openssl

root@ubuntu:~/Downloads/openssl-1.1.1l# ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
root@ubuntu:~/Downloads/openssl-1.1.1l# ln -s /usr/local/openssl/include/openssl/ /usr/include/openssl
root@ubuntu:~/Downloads/openssl-1.1.1l# echo "/usr/local/openssl/lib" >> /etc/ld.so.conf
root@ubuntu:~/Downloads/openssl-1.1.1l# ldconfig
root@ubuntu:~/Downloads/openssl-1.1.1l# openssl version #测试是否安装成功
OpenSSL 1.1.1l  24 Aug 2021
2.2 安装python3.7
zhouying@ubuntu:~/Downloads# tar zxvf Python-3.7.15.tgz
zhouying@ubuntu:~/Downloads# cd Python-3.7.15
zhouying@ubuntu:~/Downloads/Python-3.7.15# sudo -s # 输入密码,开启root权限
# prefix是指定安装路径,with-openssl是openssl的安装路径
root@ubuntu:~/Downloads/Python-3.7.15# ./configure --prefix=/python/python37 --with-openssl=/usr/local/openssl/
root@ubuntu:~/Downloads/Python-3.7.15# make && make install

运行结果会出现警告:

...
Processing /tmp/tmp0veeojry/pip-22.0.4-py3-none-any.whl
Installing collected packages: setuptools, pip
  WARNING: The script easy_install-3.7 is installed in '/python/python37/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.
  WARNING: The scripts pip3 and pip3.7 are installed in '/python/python37/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-22.0.4 setuptools-47.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@ubuntu:~/Downloads/Python-3.7.15# cd /usr/bin
root@ubuntu:/usr/bin# ls -l python* # 查看当前存在的python
lrwxrwxrwx 1 root root       9 May  6 02:54 python -> python2.7
lrwxrwxrwx 1 root root       9 May  6 02:54 python2 -> python2.7
-rwxr-xr-x 1 root root 3492624 Mar  1  2021 python2.7
lrwxrwxrwx 1 root root       9 May  6 02:54 python3 -> python3.5
-rwxr-xr-x 2 root root 4456208 Jan 26  2021 python3.5
-rwxr-xr-x 2 root root 4456208 Jan 26  2021 python3.5m
lrwxrwxrwx 1 root root      10 May  6 02:54 python3m -> python3.5m
-rwxr-xr-x 1 root root    2553 Feb 10  2016 python-argcomplete-check-easy-install-script3

这里能看到系统自带的2.7和3.5版本的python,并且现在默认的python是2.7版本的。

# 创建个软链接将新安装的python链接到当下路径
root@ubuntu:/usr/bin# ln -s /python/python37/bin/python3.7 python3.7
root@ubuntu:/usr/bin# ls -l python*
lrwxrwxrwx 1 root root       9 Oct 24 18:32 python -> python2.7
lrwxrwxrwx 1 root root       9 May  6 02:54 python2 -> python2.7
-rwxr-xr-x 1 root root 3492624 Mar  1  2021 python2.7
lrwxrwxrwx 1 root root       9 May  6 02:54 python3 -> python3.5
-rwxr-xr-x 2 root root 4456208 Jan 26  2021 python3.5
-rwxr-xr-x 2 root root 4456208 Jan 26  2021 python3.5m
lrwxrwxrwx 1 root root      29 Oct 24 18:26 python3.7 -> /python/python37/bin/python3.7
lrwxrwxrwx 1 root root      10 May  6 02:54 python3m -> python3.5m
-rwxr-xr-x 1 root root    2553 Feb 10  2016 python-argcomplete-check-easy-install-script3

3. 切换不同版本的python

一般能采用update-alternatives来切换版本,本篇使用另外一个很简单的方法,直接修改/usr/bin下面的python软链接的对象就可以切换不同版本的python。

root@ubuntu:/usr/bin# python --version 
Python 2.7.12
root@ubuntu:/usr/bin# ln -fs python3.7 python # 将python软链接的对象更改为3.7版本的
root@ubuntu:/usr/bin# python --version
Python 3.7.15

按照这样的方式就能随时切换不同版本的python了。

切换pip

相应地需要切换pip:

root@ubuntu:/usr/bin# ls -l pip*
-rwxr-xr-x 1 root root 292 Oct  5  2020 pip
-rwxr-xr-x 1 root root 283 Oct  5  2020 pip2

将pip3.7软链接到当前路径,名字可以自己取,我就直接取名pip3了

root@ubuntu:/usr/bin# ln -s /python/python37/bin/pip3.7 pip3
root@ubuntu:/usr/bin# ls -l pip*
-rwxr-xr-x 1 root root 292 Oct  5  2020 pip
-rwxr-xr-x 1 root root 283 Oct  5  2020 pip2
lrwxrwxrwx 1 root root  26 Oct 24 19:25 pip3 -> /python/pythonx/bin/pip3.7
root@ubuntu:/usr/bin# pip3 list
Package            Version
------------------ -------
pip                22.0.4
setuptools         47.1.0

# 安装个库试下
root@ubuntu:/usr/bin# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple onnx==1.12.0
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting onnx==1.12.0
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/bf/c5/e8edd9bc58192ef964270e2f4600a02cd5e5d0958b81f7abe2ee0a604478/onnx-1.12.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (13.1 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 13.1/13.1 MB 1.4 MB/s eta 0:00:00
Collecting numpy>=1.16.6
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/6d/ad/ff3b21ebfe79a4d25b4a4f8e5cf9fd44a204adb6b33c09010f566f51027a/numpy-1.21.6-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (15.7 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 15.7/15.7 MB 1.4 MB/s eta 0:00:00
Collecting typing-extensions>=3.6.2.1
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/ec/6b/63cc3df74987c36fe26157ee12e09e8f9db4de771e0f3404263117e75b95/typing_extensions-4.7.1-py3-none-any.whl (33 kB)
Collecting protobuf<=3.20.1,>=3.12.2
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/21/9b/258771d72fd2cf27eed3cfea1fc957a12666ccde394b294ac563fca23f2d/protobuf-3.20.1-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (1.0 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.0/1.0 MB 1.4 MB/s eta 0:00:00
Installing collected packages: typing-extensions, protobuf, numpy, onnx
Successfully installed numpy-1.21.6 onnx-1.12.0 protobuf-3.20.1 typing-extensions-4.7.1

经测试python和pip都能正常运行了。

参考:ubuntu篇:多版本python的安装配置,以及环境切换

pip is configured with locations that require TLS/SSL

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值