注意:先安装依赖,否则后边编译会报错。
sudo apt-get install libffi-dev uuid-dev lzma-dev liblzma-dev libncurses5-dev libgdbm-dev sqlite3 libsqlite3-dev openssl tcl8.6-dev tk8.6-dev libreadline-dev zlib1g-dev build-essential bzip2 libbz2-1.0 libbz2-dev libc6-dev libdb-dev libexpat1 libexpat1-dev libgdbm3 libncursesw5-dev libpcap-dev libreadline5 libreadline6 libreadline6-dev libsqlite0 libsqlite0-dev libsqlite3-0 libssl-dev libssl1.0.0 libxml2-dev libxslt1-dev sqlite tcl tk tk-dev xz-utils zlib1g zlib1g-dev make
Ubuntu18.04.5系统自带有python2.7 和python3.6。
最后的序号表示优先级,数字越大,优先级越高,会自动选取优先级高的作为默认指向,可手动更改默认指向,这里将系统默认的指向的优先级设置为100
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 100
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 99
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 100
sudo update-alternatives --list python
sudo update-alternatives --list python3
cd /usr/local
sudo mkdir -p /usr/local/python
sudo chmod -R 777 ./python
下面将进行各个版本的安装,至于版本号的选择
参考地址:https://www.python.org/ftp/python/
wget https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tgz
在上述前期准备中,已经在/usr/local目录下创建了一个python目录,实际上安装时如果没有创建会自动创建。
cd /usr/local/python
mkdir ./python3.7
cd ~
tar -xf ~/Python-3.7.9.tgz
cd ~/Python-3.7.9
./configure prefix=/usr/local/python/python3.7 --enable-optimizations
3.7的编译时间相比其他版本会更长,耐心等待。。。。
cd ~/Python-3.7.9
make -j 2
一定要使用altinstall,看过的教程有说明过使用install会覆盖安装,可能会导致Ubuntu崩溃的情况。
cd ~/Python-3.7.9
make altinstall >&1|tee make.log
安装gedit
apt install gedit
gedit ~/.bashrc或者 vim ~/.bashrc
添加以下内容,其中第一行注释属于个人喜好,可不添加
#[Python3.7]
export PATH=$PATH:/usr/local/python/python3.7/bin
让环境变量生效
source ~/.bashrc
添加命令指向【数值比前面设置的默认指向的优先级100小即可】
sudo update-alternatives --install /usr/bin/python python /usr/local/python/python3.7/bin/python3.7 37
sudo update-alternatives --install /usr/bin/python3 python3 /usr/local/python/python3.7/bin/python3.7 37
选择python命令的默认版本【这里只是查看,按回车即可】
*号指向应当是Ubuntu系统默认的指向(python2.7)
sudo update-alternatives --config python
选择python3命令的默认版本【这里只是查看,按回车即可】
*号指向应当是Ubuntu系统默认的指向(python3.7)
sudo update-alternatives --config python3
python3.7的启动命令
python3.7 ==> python3.7
这样之后,下面是各命令的启动版本
python2 ==> python2.7
python ==> python2.7
python3 ==> python3.5
python3.6 ==> python3.6
python3.7 ==> python3.7
对于python3.7,安装后使用pip请以python3.7 -m pip install xxx的形式使用,同时,对于python3.7的使用,建议明确指明版本来使用,而不建议更改python3的指向,因为Ubuntu的图形界面一定程度上是依赖自带的两个版本的python,更改指向可能会带来意想不到的问题。
可更新python3.7自带的pip的版本,也可不更新,一般原版的更配,只是新版的进度条更好看,也没有版本更新警告。
python3.7 -m pip install --upgrade pip
如果想切换回原版的
python3.7 -m pip install --upgrade pip==20.1.1
python3.7 -m pip install flask-socketio
python3.7 -m pip install numpy -i https://pypi.douban.com/simple/
python3.7 -m pip install pandas -i https://pypi.douban.com/simple/
python3.7 -m pip install kafka
python3.7 -m pip install kafka-python
cd ~
sudo rm -r ./Python-3.7.9
rm -r ./Python-3.7.9.tgz
最好进行一次系统重启
这里给出卸载的命令,如果不需要了,可以进行卸载。
sudo update-alternatives --remove python /usr/local/python/python3.7/bin/python3.7
sudo update-alternatives --remove python3 /usr/local/python/python3.7/bin/python3.7
rm -r /usr/local/python/python3.7
gedit ~/.bashrc
把上面添加的环境变量内容删除即可
source ~/.bashrc
apt install gcc
现象:
Creating directory /usr/local/python/python3.7/share/man/man1
/usr/bin/install -c -m 644 ./Misc/python.man \
/usr/local/python/python3.7/share/man/man1/python3.7.1
if test "xupgrade" != "xno" ; then \
case upgrade in \
upgrade) ensurepip="--altinstall --upgrade" ;; \
install|*) ensurepip="--altinstall" ;; \
esac; \
./python -E -m ensurepip \
$ensurepip --root=/ ; \
fi
Traceback (most recent call last):
File "/root/Python-3.7.9/Lib/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/root/Python-3.7.9/Lib/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/root/Python-3.7.9/Lib/ensurepip/__main__.py", line 5, in <module>
sys.exit(ensurepip._main())
File "/root/Python-3.7.9/Lib/ensurepip/__init__.py", line 214, in _main
default_pip=args.default_pip,
File "/root/Python-3.7.9/Lib/ensurepip/__init__.py", line 127, in _bootstrap
return _run_pip(args + [p[0] for p in _PROJECTS], additional_paths)
File "/root/Python-3.7.9/Lib/ensurepip/__init__.py", line 32, in _run_pip
runpy.run_module("pip", run_name="__main__", alter_sys=True)
File "/root/Python-3.7.9/Lib/runpy.py", line 201, in run_module
mod_name, mod_spec, code = _get_module_details(mod_name)
File "/root/Python-3.7.9/Lib/runpy.py", line 142, in _get_module_details
return _get_module_details(pkg_main_name, error)
File "/root/Python-3.7.9/Lib/runpy.py", line 109, in _get_module_details
__import__(pkg_name)
zipimport.ZipImportError: can't decompress data; zlib not available
make: *** [altinstall] Error 1
Makefile:1151: recipe for target 'altinstall' failed
解决方法:
apt install -y zlib*
现象:
root@prd-devel-00:~/Python-3.7.9# sudo apt-get install libffi-dev uuid-dev lzma-dev liblzma-dev libncurses5-dev libgdbm-dev sqlite3 libsqlite3-dev openssl tcl8.6-dev tk8.6-dev libreadline-dev zlib1g-dev build-essential bzip2 libbz2-1.0 libbz2-dev libc6-dev libdb-dev libexpat1 libexpat1-dev libgdbm3 libncursesw5-dev libpcap-dev libreadline5 libreadline6 libreadline6-dev libsqlite0 libsqlite0-dev libsqlite3-0 libssl-dev libssl1.0.0 libxml2-dev libxslt1-dev sqlite tcl tk tk-dev xz-utils zlib1g zlib1g-dev make
Reading package lists... Done
Building dependency tree
Reading state information... Done
Note, selecting 'libreadline-dev' instead of 'libreadline6-dev'
Package libreadline6 is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
E: Unable to locate package libgdbm3
E: Package 'libreadline6' has no installation candidate
解决方法:
待补充
解决方法:
待补充
现象:
Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
Requirement already up-to-date: pip==20.1.1 in /usr/local/python/python3.7/lib/python3.7/site-packages (20.1.1)
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
解决方法: