此为记录我重装Ubuntu后安装Python的过程
安装Python3.8
目前Ubuntu自带的Python版本为3.6,但官方都出3.8了,能不试一下新的功能吗?
第一步:下载Python3.8的源码
打开https://www.python.org/downloads/source/
选择下载tgz包(“Download Gzipped source tarball ”),解压
第二步:更新包列表并安装构建Python源所需的包
输入这三个命令
sudo apt update
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget
sudo apt-get install make
第三步:安装Python3.8
cd 到Python解压目录,然后输入以下命令
sudo ./configure --enable-optimizations
sudo make altinstall
到这里Python3.8已经安装完成,但是只能输入python3.8打开,这里等到下面一并解决
安装pip和pip3
直接输入,安装
sudo apt install python-pip
sudo apt install python3-pip
此时,ubuntu给我安装了Python2.7、pip、pip2、pip3
解决兼容问题
我的想法是:Python2.7、Python3.6、Python3.8之间可以随便切换,而且pip和pip3会动态指向我切换的Python版本上(pip2就让他指向Python2.7吧)
第一步:解决Python版本切换问题
目前网上已经有很好的教程了,使用update-alternatives 来为整个系统更改Python 版本的方法更好。
找到各个Python的路径,使用which命令
which python2.7
which python3.6
which python3.8
记录好他们所在路径,我这里是
/usr/bin/python2.7
/usr/bin/python3.6
/usr/local/bin/python3.8
接下来就是重点了,使用update-alternatives命令:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 2
sudo update-alternatives --install /usr/bin/python python /usr/local/bin/python3.8 3
这里的最后一个参数是优先级,数越大越高,倒数第二个参数是各个版本Python所在位置,即我们上一步记录的东西
输入
sudo update-alternatives --config python
选择并切换Python版本,至此,Python版本兼容性问题已经解决
第二步:解决pip、pip2、pip3指向问题
当我切换到Python3.8输入pip -V,查看pip指向哪里之时,会报错
“ from pip import main ImportError: cannot import name 'main' ”
听网上说,主要是由于ubuntu升级pip的版本后导致的
我的做法是修改pip文件
注:一定要备份pip文件,包括pip、pip2、pip3、pip3.8(Python3.8的)
输入echo $PATH,可以看到环境变量
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
不巧的是pip和pip3在/usr/local/bin目录下,而/usr/bin目录下也有pip和pip3,由优先级知道输入pip命令会优先执行/usr/local/bin目录下的。
所以我这里修改的是/usr/local/bin下的pip、pip3、pip3.8和/usr/bin下的pip2,假如想改/usr/bin下的pip和pip3的话可以直接cp过去(注意先备份)
下面是修改的代码,可以直接复制粘贴过去
pip
#!/usr/bin/python # GENERATED BY DEBIAN import sys # Run the main entry point, similarly to how setuptools does it, but because # we didn't install the actual entry point from setup.py, don't use the # pkg_resources API. if sys.version_info.major == 3 and sys.version_info.minor == 8: from pip import __main__ if __name__ == '__main__': sys.exit(__main__._main()) else: from pip import main if __name__ == '__main__': sys.exit(main())
pip2
#!/usr/bin/python2.7 # GENERATED BY DEBIAN import sys # Run the main entry point, similarly to how setuptools does it, but because # we didn't install the actual entry point from setup.py, don't use the # pkg_resources API. from pip import main if __name__ == '__main__': sys.exit(main())
pip3和pip3.8
#!/usr/bin/python # -*- coding:utf-8 -*- # GENERATED BY DEBIAN import sys import os # Run the main entry point, similarly to how setuptools does it, but because # we didn't install the actual entry point from setup.py, don't use the # pkg_resources API. if sys.version_info.major == 3: if sys.version_info.minor == 8: from pip import __main__ if __name__ == '__main__': sys.exit(__main__._main()) else: from pip import main if __name__ == '__main__': sys.exit(main()) else: msg = "当前Python版本为Python%s.%s,不能使用pip3!" % (sys.version_info.major, sys.version_info.minor) os.system("echo %s" % msg) os.system("""echo "请使用'update-alternatives --config python' 切换Python版本" """)
参考资料:
https://www.jb51.net/article/163112.htm
https://blog.csdn.net/qq_38522539/article/details/80678412