使用virtualenvwrapper 管理多个共存的python包
sudo pip3 install virtualenvwrapper
find /home/fang/ -name virtualenvwrapper.sh
/home/fang/.local/bin/virtualenvwrapper.sh
vim ~/.bashrc
export WORKON_HOME=/home/fang/.virtualenvs
#export VIRTUALENVWRAPPER_SCRIPT=/home/fang/.local/bin/virtualenvwrapper.sh
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /home/fang/.local/bin/virtualenvwrapper.sh
#export PROJECT_HOME=/home/fang/workspace
source ~/.bashrc
virtualenvwrapper
mkvirtualenv -p /usr/bin/python3.6 venv_py36 #这里可以指定虚拟环境路径,就是说源码下载的话,指定--prefix比较好!
mkvirtualenv -p /usr/bin/python3.8 venv_py38
lsvirtualenv
workon venv_py38
# pip3 freeze > requirements.txt #事先导出外部python已经安装的软件
pip install -r requirements.txt
pip install -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple numpy
deactivate #退出当前venv
rmvirtualenv myenv01 #删除某个虚拟环境
环境就绪后,我们 workon venv_py38 就使用某个虚拟环境了。在虚拟环境里可以正常使用python和pip。试试测试文件:
## https://blog.csdn.net/Varalpha/article/details/104908046
import numpy as np
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import scipy.signal as sg
a = 0.8
b = 5
t = np.linspace(0, 5, 100)
y = b * np.exp(-a * t) * np.sin(np.pi * t)
plt.xlabel('time')
plt.ylabel('yvalue')
plt.plot(t, y)
plt.grid(True)
plt.savefig('test.png')
plt.show()
缺少一些库,
pip install -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple numpy
pip install -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple matplotlib
pip install -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple scipy
运行提示plt.show没法显示图片,修改源码 matplotlib.use('TkAgg') (已经更新到.py了),结果是报告缺少tkinter:
sudo apt-get install python3-tk #缺少tkinter,这个是python自带的库,需要apt安装
然后可以正常运行。
手动编译安装其他版本的python:
搜索 python for linux,下载压缩包 Python-3.12.7.tgz
tar vxf Python-3.12.7.tgz
cd Python-3.12.7/
编译前先安装依赖
sudo apt-get install zlib1g-dev libbz2-dev libssl-dev libncurses5-dev libsqlite3-dev libreadline-dev tk-dev libgbm-dev libdb-dev libpcap-dev xz-utils libexpat1-dev liblzma-dev libffi-dev libc6-dev
sudo ./configure
sudo make
sudo make install
mkvirtualenv -p /usr/local/bin/python3.12 venv_py312
然后发现python3.12 pip有问题....想卸载,源码安装的怎么卸载??
sudo ./configure --prefix=/home/fang/Python-3.12.7/tmp_install #指定一个非系统路径
sudo make
sudo make install ,假装安装,然后挨个删除系统中对应的文件。删掉系统里面的python3.12后,本地这个可以保留,万一还想用呢,,删除也方便
mkvirtualenv -p /home/fang/Python-3.12.7/tmp_install/bin/python3.12 venv_py312
#so,下次注意了 就这么安装,和 virtualenvwrapper 配合,太方便了
再试一个,去镜像站 https://www.cnblogs.com/wutou/p/17709685.html 下载3.13
cd Python-3.13.0
mkdir tmp_install
sudo ./configure --prefix=/home/fang/Python-3.13.0/tmp_install/
sudo make
sudo make install
mkvirtualenv -p /home/fang/Python-3.13.0/tmp_install/bin/python3.13 venv_py313
workon venv_py313
结果发现3.13的pip和3.12一样有问题,需要升级:
python -m ensurepip --upgrade #升级后 pip不知为何没了,要用 pip3
pip3 install -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple numpy
安装完numpy matplotlib scipy,发现还是没法显示,
不知为何 /usr/lib/python3.8/ 和/usr/lib/python3.6/下 find . -name *tkinter*.so 都可以找到对应的库,
但是 /home/fang/Python-3.13.0/tmp_install 找不到,即便 python3-tk 和 tk-dev 已经安装。(猜测是ubuntu18.04的apt库导致的,python3-tk只有3.8和3.6能用的版本)
所以在ubuntu18.04的python3.12和3.13上,matplotlib的plot.show画不出来,只能savefig保存图片,或者用python3.6或3.8显示
windows上安装:
pip install virtualenvwrapper-win,完成后添加 C:\Users\fang\AppData\Roaming\Python\Python313\Scripts到Path环境变量
mkvirtualenv -p "C:\Program Files\Python313\python.exe" venv_py313
maplotlib库也有问题,需要把C:\Program Files\Python313\tcl下的tcl8.6和tk8.6拷贝到C:\Program Files\Python313\Lib下