简体   繁体   English

创建 requirements.txt 文件

[英]Creating a requirements.txt file

When uploading to GitHub one would want to include a requirements.txt file.上传到 GitHub 时,需要包含一个 requirements.txt 文件。 I have created a virtual environment and so pip3 freeze lists only the packages I installed during the project development.我已经创建了一个虚拟环境,所以 pip3 freeze 只列出了我在项目开发过程中安装的包。 However I had also installed pylint (suggested by VS Code) which I wouldn't want on the requirements file.但是,我还安装了 pylint(由 VS Code 建议),我不想在需求文件中安装它。 Pylint isn't listed in a single entry when I use pip3 freeze.当我使用 pip3 freeze 时,Pylint 没有在单个条目中列出。 So is there any way to remove pylint and related stuff from the requirements?那么有没有办法从需求中删除 pylint 和相关的东西? Worst case can anyone list out all the pylint stuff so I can manually remove them from requirements file?最坏的情况是任何人都可以列出所有 pylint 的东西,以便我可以从需求文件中手动删除它们吗?

Assuming you're using pip (and not other manager like Poetry), you can use pip-tools to handle this scenario.假设您使用的是pip (而不是像 Poetry 这样的其他管理器),您可以使用pip-tools来处理这种情况。 First you write your requirements manually into a file:首先,您将需求手动写入文件:

$ cat requirements.in

# assuming your project uses only following dependencies
django
gunicorn

then you can generate the whole dependency graph:然后你可以生成整个依赖图:

$ pip-compile requirements.in

#
# This file is autogenerated by pip-compile
# To update, run:
#
#    pip-compile requirements.in
#
asgiref==3.2.10           # via django
django==3.1               # via -r requirements.in
gunicorn==20.0.4          # via -r requirements.in
pytz==2020.1              # via django
sqlparse==0.3.1           # via django

^ this is your requirements.txt for production. ^ 这是您的生产requirements.txt .txt。 For the development dependencies you can do the same, but save them into a separate file, so you can install it when needed, but it stays separated from runtime dependencies:对于开发依赖项,您可以这样做,但将它们保存到单独的文件中,以便在需要时安装它,但它与运行时依赖项保持分离:

$ cat requirements-dev.in

# development requirements
pylint
mypy
pytest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM