问题:对于需要含有很多指定依赖或python 包的环境下才能运行的代码,每次转移到新环境都需要依次进行配置,易错(比如有些包需要特定版本,但pip install 默认安装最新版本),易遗漏(有时遗漏的包的提示并不是直接的 “cann't import ...”,而是一些奇怪的错误)
解决:在编写代码时,就有意识地记录需要特定安装的模块,将每一个模块(可包含版本)写入一个txt文件(比如:requirements.txt),每个占一行,最后在终端输入指令:
pip install -r requirements.txt
系统变化自动进行安装,你只需要等待即可。
示例:(requirements.txt中的内容,用于配置mockingbird源码需要的环境,可参考)
umap-learn
visdom
librosa==0.8.1
matplotlib>=3.3.0
numpy==1.19.3; platform_system == "Windows"
numpy==1.19.4; platform_system != "Windows"
scipy>=1.0.0
tqdm
sounddevice
SoundFile
Unidecode
inflect
PyQt5
multiprocess
numba
webrtcvad; platform_system != "Windows"
pypinyin
flask
flask_wtf
flask_cors==3.0.10
gevent==21.8.0
flask_restx
tensorboard
streamlit==1.8.0
PyYAML==5.4.1
torch_complex
espnet
PyWavelets
如遇以下报错,可以将执行的指令修改为:
pip install --user -r requirements.txt