Python 需求文件是跟踪 Python 模块的好方法。它是一个简单的文本文件,保存了项目所需的模块和包的列表。通过创建 Python 文件 requirements.txt ,您可以省去手动跟踪和安装所有必需模块的麻烦。

在本文中,我们将学习如何创建 Python 需求文件以及最佳实践和使用它们的好处。它通常与 Python 项目中的虚拟环境一起使用,但这超出了本文的范围。

在我们详细介绍如何创建 Python 需求文件之前, 如果您认真 学习 Python, 此处的 。它使您的生活更轻松,并提高您的工作效率。

使用 Python 需求文件有很多好处。

首先,它允许您跟踪项目使用的 Python 模块和包。它简化了在任何计算机上安装所有必需模块的过程,而无需搜索在线文档或 Python 包档案。它用于在另一台计算机上安装所有依赖项,以便它们彼此兼容。

其次,它使你可以轻松地与他人共享你的项目。他们安装你在需求文件中列出的相同 Python 模块,并毫无问题地运行你的项目。

第三,如果您需要更新或向您的项目添加 Python 模块,您只需更新需求文件,而不必在所有代码中搜索对旧模块的每个引用。

接下来,让我们学习如何创建一个!

如何创建 Python 需求文件

它只是一个文本文件,其中包含 Python 项目所需的所有模块。首先导航到 Python 项目目录并创建一个新 .txt 文档。确保它被命名为 requirements.txt ,然后将其保存在与 .py 该项目文件相同的目录中。

此时,我们不需要做太多其他工作来创建 Python 需求文件,但我们将介绍如何在终端中手动安装特定的包。

命令直接从命令行 requirements.txt 生成 Python

pip freeze > requirements.txt

pip freeze 输出所有已安装的 Python 模块及其版本的列表。

将模块添加到 Python 需求文件

现在我们已经创建了一个 Python 需求文件,是时候开始添加一些模块了!第一步是打开文本文档并添加您想要安装的模块的名称。

例如,如果我想将 tensorflow 库安装到我的项目中,我会在其自己的行中输入 tensorflow 所需的版本。让我们在新创建的 Python requirements.txt 文件中输入一个示例:

tensorflow==2.3.1
uvicorn==0.12.2
fastapi==0.63.0

添加所需的所有模块后,保存文档并退出!

从需求文件安装 Python 包

现在我们的 Python 需求文件已经全部设置好了,让我们看看如何从中安装软件包。为此,我们将使用 pip 包管理器。

pip 实用程序用于安装、升级和卸载 Python 包。它还用于管理 Python 虚拟环境等。

首先,打开终端或命令提示符并导航到 Python 项目的目录。到达那里后,输入以下命令:

pip install -r requirements.txt

这会将 Python 需求文件中列出的所有模块安装到我们的项目环境中。

输出:

Successfully installed absl-py-1.0.0 astunparse-1.6.3 cachetools-4.2.4 certifi-2021.10.8 charset-normalizer-2.0.9 click-7.1.2 fastapi-0.63.0 gast-0.3.3 google-auth-2.3.3 google-auth-oauthlib-0.4.6 google-pasta-0.2.0 grpcio-1.42.0 h11-0.12.0 h5py-2.10.0 idna-3.3 importlib-metadata-4.8.2 keras-preprocessing-1.1.2 markdown-3.3.6 numpy-1.18.5 oauthlib-3.1.1 opt-einsum-3.3.0 protobuf-3.19.1 pyasn1-0.4.8 pyasn1-modules-0.2.8 pydantic-1.8.2 requests-2.26.0 requests-oauthlib-1.3.0 rsa-4.8 six-1.16.0 starlette-0.13.6 tensorboard-2.7.0 tensorboard-data-server-0.6.1 tensorboard-plugin-wit-1.8.0 tensorflow-2.3.1 tensorflow-estimator-2.3.0 termcolor-1.1.0 typing-extensions-4.0.1 urllib3-1.26.7 uvicorn-0.12.2 werkzeug-2.0.2 wheel-0.37.0 wrapt-1.13.3 zipp-3.6.0

感谢您的阅读!希望本文对您有所帮助并对您有所帮助。您可以在在使用 Python 需求文件安装软件包之前设置新环境是一种很好的做法。在我的下一篇文章中了解pyenvvenv以帮助您完成此过程, and 请点击此处.

If you ever need to remove a module for any reason, use the same command but with the uninstall keyword instead of install . Also, use upgrade instead of install to update previously installed Python packages.

As mentioned before, use the pip freeze command to output a list of the Python modules installed in your environment.

How to Maintain a Python Requirements File

If you created a Python requirements.txt file at one point but have failed to maintain it for some reason, fear not! You can do it as follows.

Step 1: Output a list of outdated packages with pip list --outdated .

Output:

Package              Version Latest Type                                                                                                                                                                     -------------------- ------- ------ -----                                                                                                                                                                    click                7.1.2   8.0.3  wheel                                                                                                                                                                    fastapi              0.63.0  0.70.0 wheel                                                                                                                                                                    gast                 0.3.3   0.5.3  wheel                                                                                                                                                                    h5py                 2.10.0  3.6.0  wheel                                                                                                                                                                    numpy                1.18.5  1.21.4 wheel                                                                                                                                                                    pip                  20.0.2  21.3.1 wheel                                                                                                                                                                    setuptools           44.0.0  59.5.0 wheel                                                                                                                                                                    starlette            0.13.6  0.17.1 wheel                                                                                                                                                                    tensorflow           2.3.1   2.7.0  wheel                                                                                                                                                                    tensorflow-estimator 2.3.0   2.7.0  wheel                                                                                                                                                                    uvicorn              0.12.2  0.15.0 wheel

Step 2: Upgrade the required package with pip install -U PackageName .

As an example, let’s update fastapi :

pip install -U fastapi

Output:

Successfully installed anyio-3.4.0 fastapi-0.70.0 sniffio-1.2.0 starlette-0.16.0

It is also possible to upgrade everything with pip install -U -r requirements.txt .

Step 3: Check to see if all of the tests pass.

Step 4: Run pip freeze > requirements.txt to update the Python requirements file.

Step 5: Run git commit and git push to the production branch.

Freezing all your dependencies helps you have predictable builds.

If you need to check for missing dependencies, you can do so with the following command:

python -m pip check

Output:

No broken requirements found.

In our case, we are good to go!

How to Create Python Requirements File After Development

While it is possible to create it manually, it is a good practice to use the pipreqs module. It is used to scan your imports and build a Python requirements file for you.

According to the documentation , once installed with the following command:

pip install pipreqs

running pipreqs in the command line generates a requirements.txt file automatically:

$ pipreqs /home/project/location
Successfully saved requirements file in   /home/project/location/requirements.txt

Why You Should Use a Python Requirements File

Create a Python requirements.txt file when starting a new data science project. It is always a good idea to include one in your project, particularly in the context of version control.

If you are unsure about version control, read more about it

如果您不确定版本控制,请在此处 . And if you are interested in writing better Python code, you can find more information here .

Using Python requirements files is among Python development best practices. It dramatically reduces the need for managing and supervising different libraries. Managing your library dependencies from one place makes it easier, more convenient, and faster. It helps keep everything organized and easy for everyone involved.

Compared to pasting a list of dependency paths into the command line every time you want to install or update them, it makes installing your Python applications on another system easier. It is a great way to ensure you have all the necessary dependencies installed for your project.

Also, GitHub provides automated vulnerability alerts for dependencies in your repository. By uploading a requirements.txt with your code, GitHub checks for any conflict and sends an alert to the administrator if it detects any. It can even resolve the vulnerabilities automatically!

Best Practices for Using a Python Requirements File

There are several best practices to follow in using a Python requirements.txt file:

  • Always use the pip freeze command to generate a list of Python modules and packages installed in the virtual environment of your project. This ensures the list is up to date and accurate.
  • Only list the Python modules and packages your project needs. Do not include unnecessary modules or packages, as this makes the txt file bloated and difficult to read. It is also a waste of resources.
  • Save the Python requirements.txt file in the project source code repository so that other developers can easily install all of the Python modules and packages when they clone or check out your project.
  • Use the pip install -r requirements.txt command to install all of the Python modules and packages listed in your requirements.txt file. This saves time and effort.
  • Keep your Python requirements.txt files up to date and accurate. This ensures your project always uses the latest versions of the Python modules and packages.

Looking for data science project ideas to experiment with creating and maintaining a Python requirements file? Feel free to check this article to find some inspiration!

Closing Thoughts on the Python Requirements File

In this article, we've learned what a Python requirements.txt file is and how to create it. We've also learned about its benefits and the Python community best practices for using a requirements file.

Thank you for reading! I hope this article was helpful and informative. You can find out more on .

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部