您的位置:首页 > 技术中心 > 其他 >

Python怎么快速生成本项目的requeirments.txt

时间:2023-05-13 18:48

在Python项目中,我们通常需要使用许多第三方库来提供额外的功能和工具。但是,直接将这些库上传到Git仓库并不是一种好的做法,因为这会使得代码库变得过于臃肿,并且很难管理。此外,有时候在部署应用程序时也需要安装特定版本的依赖项。

这时候,就可以使用requirements.txt文件来管理项目所需的依赖项。该文件列出了项目所需的所有依赖项及其版本号,使得其他人可以轻松地安装和运行该项目所需的所有依赖项。使用pip命令读取该文件,可以自动下载并安装所有列出的依赖项,这大大简化了项目启动/部署的流程。

因此,生成requirements.txt文件对于管理Python项目的依赖项非常重要,它能够确保项目的可重复性、可移植性和可维护性。

Python怎么快速生成本项目的requeirments.txt

1.使用pipreqs生成requeirments.txt

在项目根目录下打开终端,运行以下命令安装pipreqs:

pip install pipreqs

运行以下命令生成requirements.txt文件:

pipreqs . --encoding=utf8 --force

其中,.表示当前目录,–encoding=utf8指定编码为UTF-8,–force选项强制覆盖已存在的requirements.txt文件。

等待执行完毕,即可在项目根目录下看到生成的requirements.txt文件。

Python怎么快速生成本项目的requeirments.txt

2.使用pip

要使用pip生成当前Python项目的requirements.txt文件,请按照以下步骤操作:

1.确保你已经安装了pip和虚拟环境。

2.在虚拟环境中打开终端,并进入到项目的根目录下。

3.运行以下命令,生成包含所有依赖项的requirements.txt文件:

pip freeze > requirements.txt

执行完毕后,你可以在项目的根目录下看到一个名为requirements.txt的文本文件,其中包含了所有依赖项及其版本号。

Python怎么快速生成本项目的requeirments.txt

需要注意的是,pip freeze命令会将所有安装的包及其版本信息输出到控制台。通过重定向符号>将输出结果写入到文件中,就能够生成requirements.txt文件。但是,该文件中可能包含一些不必要的依赖项,如系统自带的库、测试工具等。因此,在使用生成的requirements.txt文件时,建议手动检查并删除不必要的依赖项,以减小项目体积。
以下是生成的requirements.txt文件,可以看到,有好多不必要的依赖项被生成

absl-py==1.0.0addict==2.4.0aiohttp==3.7.4.post0alembic==1.8.1argon2-cffi @ file:///opt/conda/conda-bld/argon2-cffi_1645000214183/workargon2-cffi-bindings @ file:///C:/ci/argon2-cffi-bindings_1644569848815/workastunparse==1.6.3async-timeout==3.0.1attrs @ file:///opt/conda/conda-bld/attrs_1642510447205/workbackcall @ file:///home/ktietz/src/ci/backcall_1611930011877/workbeautifulsoup4 @ file:///tmp/build/80754af9/beautifulsoup4_1631874778482/workbilibili-api==5.1.2bleach @ file:///opt/conda/conda-bld/bleach_1641577558959/workblinker==1.5cachetools==5.0.0certifi @ file:///C:/b/abs_85o_6fm0se/croot/certifi_1671487778835/work/certificffi @ file:///C:/ci_310/cffi_1642682485096/workchardet==4.0.0charset-normalizer==2.0.12click @ file:///C:/ci/click_1646038601470/workcloudpickle @ file:///tmp/build/80754af9/cloudpickle_1632508026186/workcolorama @ file:///tmp/build/80754af9/colorama_1607707115595/workcryptography @ file:///C:/ci/cryptography_1652101770956/workcycler==0.11.0cytoolz==0.11.0dask==1.1.4debugpy @ file:///C:/ci/debugpy_1637091911212/workdecorator @ file:///opt/conda/conda-bld/decorator_1643638310831/workdefusedxml @ file:///tmp/build/80754af9/defusedxml_1615228127516/workdnspython==2.3.0docopt==0.6.2einops==0.4.1email-validator==1.3.1entrypoints==0.3fastjsonschema @ file:///tmp/build/80754af9/python-fastjsonschema_1620414857593/work/distFlask==2.2.3Flask-Email==1.4.4Flask-Mail==0.9.1Flask-Migrate==3.1.0Flask-Script==2.0.6Flask-SQLAlchemy @ file:///tmp/build/80754af9/flask-sqlalchemy_1616180561581/workFlask-WTF==1.1.1flatbuffers==23.1.21fonttools==4.30.0fvcore==0.1.5.post20220305gast==0.4.0google-auth==2.6.5google-auth-oauthlib==0.4.6google-pasta==0.2.0greenlet @ file:///C:/ci/greenlet_1628888257991/workgrpcio==1.45.0grpcio-tools==1.45.0h6py @ file:///C:/ci/h6py_1659089886851/workidna==3.3imagecodecs @ file:///C:/ci/imagecodecs_1635529223557/workimageio @ file:///tmp/build/80754af9/imageio_1617700267927/workimportlib-metadata @ file:///C:/ci/importlib-metadata_1648562631189/workimportlib-resources==5.9.0iopath==0.1.9ipykernel @ file:///C:/ci/ipykernel_1647000985174/work/dist/ipykernel-6.9.1-py3-none-any.whlipython @ file:///C:/ci/ipython_1643800131373/workipython-genutils @ file:///tmp/build/80754af9/ipython_genutils_1606773439826/workipywidgets @ file:///tmp/build/80754af9/ipywidgets_1634143127070/workitsdangerous @ file:///tmp/build/80754af9/itsdangerous_1621432558163/workjedi @ file:///C:/ci/jedi_1644297241925/workJinja2 @ file:///C:/b/abs_7cdis66kl9/croot/jinja2_1666908141852/workjoblib @ file:///C:/b/abs_e60_bwl1v6/croot/joblib_1666298845728/workjsonschema @ file:///Users/ktietz/demo/mc3/conda-bld/jsonschema_1630511932244/workjupyter==1.0.0jupyter-client @ file:///opt/conda/conda-bld/jupyter_client_1643638337975/workjupyter-console @ file:///opt/conda/conda-bld/jupyter_console_1647002188872/workjupyter-core @ file:///C:/ci/jupyter_core_1646976467633/workjupyterlab-pygments @ file:///tmp/build/80754af9/jupyterlab_pygments_1601490720602/workjupyterlab-widgets @ file:///tmp/build/80754af9/jupyterlab_widgets_1609884341231/workkeras==2.11.0kiwisolver @ file:///C:/ci/kiwisolver_1653274189334/worklabelme==3.16.7libclang==15.0.6.1loguru @ file:///C:/ci/loguru_1643616607274/worklxml==4.6.5Mako==1.2.2Markdown==3.3.6MarkupSafe @ file:///C:/ci/markupsafe_1654508076077/workmatplotlib==3.5.1matplotlib-inline @ file:///tmp/build/80754af9/matplotlib-inline_1628242447089/workmistune @ file:///C:/ci/mistune_1594373272338/workmkl-fft==1.3.1mkl-random @ file:///C:/ci/mkl_random_1626186163140/workmkl-service==2.4.0mmcv==1.6.2multidict==6.0.2nbclient @ file:///tmp/build/80754af9/nbclient_1645431659072/worknbconvert @ file:///C:/ci/nbconvert_1649759177374/worknbformat @ file:///C:/ci/nbformat_1649845122517/worknest-asyncio @ file:///C:/ci/nest-asyncio_1649848126026/worknetworkx==2.2notebook @ file:///C:/ci/notebook_1645002740769/worknumpy @ file:///C:/ci/numpy_and_numpy_base_1649782933444/workoauthlib==3.2.0opencv-python==4.5.5.64openslide-python==1.2.0opt-einsum==3.3.0packaging @ file:///tmp/build/80754af9/packaging_1637314298585/workpandas==1.3.5pandocfilters @ file:///opt/conda/conda-bld/pandocfilters_1643405455980/workparso @ file:///opt/conda/conda-bld/parso_1641458642106/workpickleshare @ file:///tmp/build/80754af9/pickleshare_1606932040724/workPillow==9.0.1pipreqs==0.4.11portalocker==2.4.0prettytable==3.3.0prometheus-client @ file:///opt/conda/conda-bld/prometheus_client_1643788673601/workprompt-toolkit @ file:///tmp/build/80754af9/prompt-toolkit_1633440160888/workprotobuf==3.19.6pyasn1==0.4.8pyasn1-modules==0.2.8pycparser @ file:///tmp/build/80754af9/pycparser_1636541352034/workpyecharts==1.9.1pygame==2.2.0Pygments @ file:///opt/conda/conda-bld/pygments_1644249106324/workPyMySQL @ file:///C:/ci/pymysql_1610464946597/workpyparsing==3.0.7PyQt5-Qt5==5.15.2PyQt5-sip==12.9.1pyrsistent @ file:///C:/ci/pyrsistent_1636093257833/workpytesseract==0.3.10python-dateutil @ file:///tmp/build/80754af9/python-dateutil_1626374649649/workpytz @ file:///C:/Windows/TEMP/abs_90eacd4e-8eff-491e-b26e-f707eba2cbe1ujvbhqz1/croots/recipe/pytz_1654762631027/workPyWavelets @ file:///C:/ci/pywavelets_1648728036674/workpywin32==302pywinpty @ file:///C:/ci_310/pywinpty_1644230983541/work/target/wheels/pywinpty-2.0.2-cp37-none-win_amd64.whlPyYAML==6.0pyzmq @ file:///C:/ci/pyzmq_1638435182681/workqtconsole @ file:///opt/conda/conda-bld/qtconsole_1649078897110/workQtPy @ file:///opt/conda/conda-bld/qtpy_1649073884068/workregex==2022.10.31requests==2.27.1requests-oauthlib==1.3.1rsa==4.8scikit-image @ file:///C:/ci/scikit-image_1648196140109/workscikit-learn @ file:///C:/ci/scikit-learn_1642599122269/workscipy @ file:///C:/ci/scipy_1641555141383/workseaborn==0.11.2Send2Trash @ file:///tmp/build/80754af9/send2trash_1632406701022/worksip==4.19.13six @ file:///tmp/build/80754af9/six_1644875935023/worksoupsieve @ file:///tmp/build/80754af9/soupsieve_1636706018808/workSQLAlchemy @ file:///C:/Windows/Temp/abs_f8661157-660b-49bb-a790-69ab9f3b8f7c8a8s2psb/croots/recipe/sqlalchemy_1657867864564/worktabulate==0.8.9tensorboard==2.11.2tensorboard-data-server==0.6.1tensorboard-plugin-wit==1.8.1tensorflow==2.11.0tensorflow-estimator==2.11.0tensorflow-intel==2.11.0tensorflow-io-gcs-filesystem==0.31.0termcolor==1.1.0terminado @ file:///C:/ci/terminado_1644322782754/worktestpath @ file:///tmp/build/80754af9/testpath_1624638946665/workthop==0.0.31.post2005241907threadpoolctl @ file:///Users/ktietz/demo/mc3/conda-bld/threadpoolctl_1629802263681/worktifffile @ file:///tmp/build/80754af9/tifffile_1627275862826/worktimm==0.6.7toolz @ file:///tmp/build/80754af9/toolz_1636545406491/worktorch==1.9.1+cu102torchaudio==0.9.1torchmetrics==0.9.3torchstat==0.0.7torchvision==0.10.1+cu102tornado @ file:///C:/ci/tornado_1606935947090/worktqdm==4.63.0traitlets @ file:///tmp/build/80754af9/traitlets_1636710298902/worktyping_extensions @ file:///opt/conda/conda-bld/typing_extensions_1647553014482/workurllib3==1.26.9wcwidth @ file:///Users/ktietz/demo/mc3/conda-bld/wcwidth_1629357192024/workwebencodings==0.5.1Werkzeug==2.2.3widgetsnbextension @ file:///C:/ci/widgetsnbextension_1645009553925/workwin32-setctime @ file:///home/tkoch/Workspace/win32_setctime/win32_setctime_1643630045199/workwincertstore==0.2wrapt==1.15.0WTForms==3.0.1xlwt==1.3.0yacs==0.1.8yapf==0.32.0yarg==0.1.9yarl==1.7.2zipp @ file:///C:/ci/zipp_1652274072582/work

以上就是Python怎么快速生成本项目的requeirments.txt的详细内容,更多请关注Gxl网其它相关文章!

热门排行

今日推荐

热门手游