python项目环境迁移时如何生成第三方库文件requirements.txt并安装?

有很多的python爱好者应该都知道,requirements.txt文件一般是用来描述python使用了哪些相关的模块的。

经常在python项目的迁移时,可能需要将服务器开发环境的项目同步到本地电脑上面进行开发。

这个时候就需要对本地环境进行pip install xxxx许多的依赖库,在github上面已经有大佬解决了我们的困扰。

话不多说,接下来我们来说明一下如何在项目的环境迁移时轻松完成python依赖库的安装。

首先,需要在python环境安装pipreqs模块,这里我们使用pip的方式安装。

pip install pipreqs

安装完成之后在控制台可以切换到项目目录下面执行生成requirements.txt文件命令。

cd /local/python/proj

pipreqs . --encoding=utf8 --force

这样执行完成后就已经将项目或者该路径下的.py文件引用的python非标准库模块及版本信息都添加到requirements.txt文件中。

d3blocks==1.2.8
fitz==0.0.1.dev2
loguru==0.6.0
pandas==1.5.1
pdf2docx==0.5.6
polars==0.16.2
py7zr==0.20.2
PyPDF2==3.0.1
PyQt5==5.15.9
pywin32==305
rarfile==4.0

requirements.txt文件生成之后,我们只需要在将要迁移到的目标环境下进行安装即可。

pip install -r requirements.txt

PS G:\py-workspace\the-public\test020> pip install -r .\requirements.txt
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting d3blocks==1.2.8
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/90/42/aa3879357b0ca221293a7c0399eb37cbaeb2092753235c5ae91e41af8eb6/d3blocks-1.2.8-py3-none-any.whl (928 kB)
     ---------------------------------------- 928.8/928.8 kB 1.2 MB/s eta 0:00:00
Collecting fitz==0.0.1.dev2

Successfully installed PyMuPDF-1.21.1 PyPDF2-3.0.1 PyQt5-5.15.9 brotli-1.0.9 ci-info-0.3.0 clusteval-2.1.4 colourmap-1.1.10 configobj-5.0.8 configparser-5.3.0 d3blocks-1.2.8 d3graph-2.
3.10 elasticgraph-0.2.0 etelemetry-0.3.0 fire-0.5.0 fitz-0.0.1.dev2 future-0.18.3 httplib2-0.21.0 inflate64-0.3.1 ismember-1.0.2 isodate-0.6.1 loguru-0.6.0 looseversion-1.0.3 lxml-4.9.
2 markupsafe-2.0.1 multivolumefile-0.2.3 nibabel-5.0.0 nipype-1.8.5 numpy-1.22.4 opencv-python-4.7.0.68 pathlib-1.0.1 pdf2docx-0.5.6 polars-0.16.2 prov-2.0.0 psutil-5.9.4 py7zr-0.20.2
pybcj-1.0.1 pycryptodomex-3.17 pydot-1.4.2 pypickle-1.1.0 pyppmd-1.0.0 python-docx-0.8.11 python-louvain-0.16 pyxnat-1.5 pyzstd-0.15.3 rarfile-4.0 rdflib-6.2.0 seaborn-0.12.2 simplejso
n-3.18.3 termcolor-2.2.0 texttable-1.6.7 traits-6.3.2 wget-3.2 win32-setctime-1.1.0

最后,耐心等待requirements.txt文件中所有的模块安装完成就OK了。

猜你喜欢

转载自blog.csdn.net/chengxuyuan_110/article/details/129130272