第一节 安装python3.7.4
1. 安装python3的依赖
sudo apt-get install build-essential checkinstall
sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev
libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev
2. 下载python3的包
sudo su # 切到管理员
cd /usr/loacl # 进入指定目录
mkdir python3 # 创建python3的目录
cd python3 # 进入python3的目录
wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz # 下载python3.7.4的包
tar xzf Python-3.7.4.tgz # 解压包
3. 编译python3.7
cd Python-3.7.4
sudo ./configure --enable-optimizations
sudo make altinstall
4. 添加环境软连接
sudo ln -s /usr/local/bin/python3.7 /usr/bin/python3
sudo mv /usr/bin/lsb_release /usr/bin/lsb_release.bak
第二节 安装Python3.7.4虚拟环境
1. 首先是安装两个包
sudo pip3 install virtualenv # python虚拟环境
sudo pip3 install virtualenvwrapper # 封装了虚拟环境,支持一些简化命令
# 然后通过pip3 list 查看是否安装成功
2. 添加环境变量(注意点!!!)
sudo apt-get install vim
sudo mkdir $HOME/.virtualenvs
vim ~/.bashrc
# 在末尾添上(去掉注释)
# export WORKON_HOME=$HOME/.virtualenvs
# source /usr/local/bin/virtualenvwrapper.sh
3. 执行一下~/.bashrc
source ~/.bashrc
4. 安装虚拟环境
mkvirtualenv -p python3 py3_flask
5. 会出现"ModuleNotFoundError: No module named 'CommandNotFound'" 报错
解决方案:找到lsb_release.py文件和CommandNotFound目录,把它们拷贝到报的错误中subprocess.py所在文件夹
sudo find / -name 'lsb_release.py'
# /usr/share/pyshared/lsb_release.py
# /usr/lib/python2.7/dist-packages/lsb_release.py
# /usr/lib/python3/dist-packages/lsb_release.py
# /usr/local/lib/python3.7/lsb_release.py
sudo cp /usr/lib/python3/dist-packages/lsb_release.py /usr/local/lib/python3.7/
6. 创建虚拟环境
root@ubuntu:~# mkvirtualenv -p python3 py3_flask
created virtual environment CPython3.7.4.final.0-64 in 130ms
creator CPython3Posix(dest=/root/.virtualenvs/py3_flask, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
added seed packages: pip==21.1.2, setuptools==57.0.0, wheel==0.36.2
activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
(py3_flask) root@ubuntu:~#
# 查看虚拟环境,并切入到某一个虚拟环境
workon
workon '虚拟环境名称'
# 退出虚拟环境
deactivate
参考网址
Ubuntu18.04下安装Python虚拟环境
https://www.huaweicloud.com/articles/81f31dfa2ac27da176d853a3a25be68b.html