简体   繁体   English

有没有办法在安装时自动将依赖项添加到 requirements.txt 中?

[英]Is there a way to automatically add dependencies to requirements.txt as they are installed?

Similar to how Node.js automatically adds dependencies to package-lock.json , is there a way I can automatically add requirements to my requirements.txt file for Python?类似于 Node.js 自动将依赖项添加到package-lock.json ,有没有一种方法可以自动将要求添加到我的requirements.txt文件中以获取 Python?

Since you mentioned Node.js specifically, the Python project that comes closest to what you're looking for is probably Pipenv .由于您特别提到了 Node.js ,因此最接近您正在寻找的 Python 项目可能是Pipenv

Blurb from the Pipenv documentation : Pipenv 文档中的模糊信息

Pipenv is a dependency manager for Python projects. Pipenv 是 Python 项目的依赖管理器。 If you're familiar with Node.js's npm or Ruby's bundler , it is similar in spirit to those tools.如果您熟悉 Node.js 的npm或 Ruby 的bundler ,它们在本质上与这些工具相似。 While pip can install Python packages, Pipenv is recommended as it's a higher-level tool that simplifies dependency management for common use cases.虽然 pip 可以安装 Python 包,但建议使用 Pipenv,因为它是一种高级工具,可以简化常见用例的依赖管理。

It's quite a popular package among developers as the many stars on GitHub attest.正如 GitHub 上的许多明星所证明的那样,它在开发人员中非常流行 package。

Alternatively, you can use a "virtual environment" in which you only install the external dependencies that your project needs.或者,您可以使用“虚拟环境”,仅安装项目所需的外部依赖项。 You can either use the venv module from the standard library or the Virtualenv package from PyPI , which offers certain additional features (that you may or may not need).您可以使用标准库中的venv模块或PyPI中的Virtualenv package,它提供了某些附加功能(您可能需要也可能不需要)。 With either of those, you can then use Python's (standard) package manager Pip to update the requirements file :使用其中任何一个,您都可以使用 Python 的(标准)package 管理器 Pip 来更新需求文件

pip freeze >requirements.txt

This is the "semi-automatic" way, so to speak.可以这么说,这是“半自动”方式。 Personally, I prefer to do this manually.就个人而言,我更喜欢手动执行此操作。 That's because in a typical development environment ("virtual" or not), you also install packages that are only required for development tasks, such as running tests or building the documentation.这是因为在典型的开发环境(“虚拟”与否)中,您还安装了仅用于开发任务的软件包,例如运行测试或构建文档。 They don't need to be installed along with your package on end-user machines, so shouldn't be in requirements.txt .它们不需要与您的 package 一起安装在最终用户机器上,因此不应该在requirements.txt中。 Popular packaging tools such as Flit and Poetry manage these "extra dependencies" separately, as does Pip . FlitPoetry等流行的打包工具分别管理这些“额外依赖项”Pip 也是如此

If you are using Linux you can create an alias like this:如果您使用的是 Linux 您可以创建这样的别名:

alias req='pip3 freeze > ~/requerments.txt'

And then when you want to install new package use this command:然后当你想安装新的 package 使用这个命令:

pip3 install <package> | req

I think, to-requirements.txt is what you need:我认为, to-requirements.txt是您需要的:

pip install to-requirements.txt
requirements-txt setup

After that installed packages will be appended to requirements.txt .之后安装的软件包将附加到requirements.txt And uninstalled packages will be removed.并且卸载的软件包将被删除。 It might require root access if you install it on system-wide Python interpreter.如果您将它安装在系统范围的 Python 解释器上,它可能需要 root 访问权限。 Add sudo if it failes.如果失败,请添加sudo

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

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