自动创建requirements.txt

Igo*_*nov 205 python dependencies requirements python-import

有时我从下载python源代码,github不知道如何安装所有依赖项.如果没有requirements.txt文件,我必须手工创建.问题是:鉴于python源代码目录是否可以requirements.txt从导入部分自动创建?

DJa*_*ens 282

您可以使用以下代码生成requirements.txt文件:

pip install pipreqs

pipreqs /path/to/project
Run Code Online (Sandbox Code Playgroud)

有关pipreqs的更多信息可以在这里找到.

有时您遇到过pip freeze,但这会保存环境中的所有包,包括您当前项目中未使用的包.

  • 工作就像一个魅力.它做了正确的事,没有大惊小怪. (9认同)
  • 重要提示:这个变体可能很糟糕。使用 virtualenv 和 pip3 freeze >requirements.txt。让我解释一下:pipreqs 只保存项目中导入的包,而不保存它们的依赖项。有时包维护者会写这样的需求规则:“Jinja2>=2.10”(这来自 Flask 1.02 包的 `setup.py`)。因此可以安装不兼容的主要版本(在我的示例中我安装了 Jinja 3.1.0)并破坏了一切。 (8认同)
  • 我使用了 pipreqs 并且需要在扫描的项目文件夹中指定使用的字符集 - 否则我收到 pipreqs 生成的错误:`pipreqs --encoding utf-8` (4认同)
  • 注意:这不包括您的设置中已安装的、未明确导入到您的视图中但仍在后台使用的应用程序。 (4认同)
  • @Shaikhul,如果您收到这些警告,您将不会获得完整的文件,您只会获得部分要求。您可以在 https://github.com/npow/MemNN/tree/hyperopt repo 上查看。我只有'hyperopt == 0.0.2 numpy == 1.9.2 scikit_learn == 0.16.1 theano == 0.7.0' (3认同)
  • 伟大的工具.谢谢!它的强大功能,它生成完全使用的包,而不是像pip冻结! (2认同)
  • 适用于 Windows 10 上的 Python 3.9 (2认同)

dam*_*ver 201

如果你使用虚拟环境,pip freeze > requirements.txt就好了.如果没有,pigar将是一个不错的选择.

顺便说一下,我不确定它是否适用于2.6.

更新:

建议使用Pipenv或其他工具来改善您的开发流程.

  • @damnever好的谢谢!`pip3 freeze`给了我在我的环境中安装的所有Python包的列表,而`pipreqs`给了我在源代码中实际使用的所有我正在寻找的东西.除此之外,它没有任何问题. (6认同)
  • 我执行了 `pip3 freeze > requirements.txt`,但我很惊讶没有找到我实际使用的一些包,比如 `argparse` 和 `configparser`。是因为它们是 Python 核心的一部分吗?我相信更好的答案是使用下面的“pipreqs”,因为它只会列出您的项目正在使用的要求。 (4认同)
  • 进入`requirements.txt` 可能是[一个很糟糕的主意。](https://medium.com/@tomagee/pip-freeze-requirements-txt-thinked-harmful-f0bce66cf895) (4认同)
  • 对于任何错误的人,我建议在提问之前尝试`pip3 freeze> requirements.txt`. (2认同)
  • pipenv 有严重问题,请参阅此讨论:https://news.ycombinator.com/item?id=18612590 (2认同)

Sne*_*osh 61

令人难以置信的是,这个简单的任务在 Python 中却如此复杂。这是我认为自动执行此操作的最佳方法。

你需要两个工具:

1. pipreqs

pip3 install pipreqs

pipreqs 将遍历您的项目并仅安装您的项目使用的软件包。而不是像你的 python 环境中的所有包那样pip freeze

但这种方法有一个问题。它不安装子包。

例如,您的项目使用pandas==1.3.2. 本身与其他包一起pandas使用。numpy==1.21.2pipreqs本身并没有将子包(即numpy)写入requirments.txt

这是您需要pipreqs与第二个工具结合使用的地方。

  1. pip工具

pip3 install pip-tools

pip-tools将接收包requirements.in并生成requirements.txt包含所有子包的包。例如,如果您有 pandas==1.3.2in requirements.inpip-tools将生成

numpy==1.21.2 # via pandasrequirements.txt

但需要手动添加包requirements.in。这很容易出错,您可能偶尔会忘记这样做。

这是您可以使用第一个工具的地方。

但这两个工具都写入到requirements.txt. 那么如何解决呢?

使用 --savepathfor代替默认的pipreqs进行写入。requirements.inrequirements.txt

用一个命令来完成它;做就是了

pipreqs --savepath=requirements.in && pip-compile

就这样吧。现在,您无需担心手动维护包,并且您将requirements.txt拥有所有子包,以便您的构建具有确定性。

长话短说

  1. pip3 install pipreqs
  2. pip3 install pip-tools

使用以下内容构建确定性requirements.txt

pipreqs --savepath=requirements.in && pip-compile

  • 它应该是“pip install pip-tools”而不是“pip install pip-tool” (6认同)

Mos*_*ael 29

因为大多数答案pipreqs对我来说不起作用。这是我的答案。

生成requirements.txt文件:

pip install pipreqs

python -m  pipreqs.pipreqs --encoding utf-8  /path/to/project
Run Code Online (Sandbox Code Playgroud)

我更喜欢使用pipreqsmore pip freeze,因为pip freeze可以保存环境中的所有包,包括您当前项目中未使用的包。但是,pipreqs仅保存您在项目中使用的那些。

要安装要求,请使用:

pip3 install -r requirements.txt
Run Code Online (Sandbox Code Playgroud)


cha*_*dot 15

您只需使用以下命令即可完成此操作。它将创建文件requirement.txt并自动添加相关模块。

对于 Unix:

pip3 freeze > requirements.txt
Run Code Online (Sandbox Code Playgroud)

对于 Windows:

pip freeze > requirements.txt
Run Code Online (Sandbox Code Playgroud)


小智 13

首先,你的项目文件必须是一个py文件,它是直接的python文件。如果您的文件是 ipynb 格式,您可以使用以下代码行将其转换为 py 类型:

jupyter nbconvert --to=python
Run Code Online (Sandbox Code Playgroud)

然后,您需要从 cmd(mac 终端)安装 pipreqs 库。

pip install pipreqs
Run Code Online (Sandbox Code Playgroud)

现在我们可以使用下面的代码创建txt文件。如果您与文件位于同一路径,则只需编写 ./ 。否则,您需要提供文件的路径。

pipreqs ./
Run Code Online (Sandbox Code Playgroud)

或者

pipreqs /home/project/location
Run Code Online (Sandbox Code Playgroud)

这将为您的项目创建一个 requirements.txt 文件。


小智 13

创建文件requirement.txt

对于Python 3版本,命令是:

pip3 freeze > requirements.txt
Run Code Online (Sandbox Code Playgroud)

对于Python 2版本,命令是:

pip freeze > requirements.txt
Run Code Online (Sandbox Code Playgroud)

安装需求.txt

对于Python 3版本,命令是:

pip3 install -r requirements.txt
Run Code Online (Sandbox Code Playgroud)

对于Python 2版本,命令是:

pip install -r requirements.txt
Run Code Online (Sandbox Code Playgroud)


voi*_*lex 12

自动更新requirements.txt的方法

在使用requirements.txt开发Python应用程序时,我们有多种选择:

  1. 开发完成后,当我们想要部署它时,生成requirements.txt 。它是由pip freeze > requirements.txtpipreqs为了减少混乱的结果而执行的。
  2. 每次安装后手动将每个模块添加到文件requirements.txt
  3. 安装将为我们处理requirements.txt更新的管理器。

第一个选项有很多答案,第二个选项是不言自明的,所以我想描述第三种方法。有一个名为to-requirements.txt 的库。要安装它,请输入:

pip install to-requirements.txt  # Pip install to requirements.txt
Run Code Online (Sandbox Code Playgroud)

如果您立即阅读整个命令,您就会看到它的作用。安装后您应该进行设置。跑步:

requirements-txt init
Run Code Online (Sandbox Code Playgroud)

它会覆盖 pip 脚本,以便使用pip install所需版本的包自动pip uninstall更新项目的requirements.txt文件。覆盖是安全进行的,因此卸载此软件包后,pip 将表现正常。

您还可以自定义它的工作方式。例如,全局禁用它并仅针对所需目录激活它,仅针对 git 存储库激活它,或者允许/禁止创建requirements.txt 文件(如果它不存在)。

参考:

  1. 文档
  2. GitHub
  3. 皮伊


小智 11

确保为 python3.7 运行 pip3。

pip3 freeze >> yourfile.txt
Run Code Online (Sandbox Code Playgroud)

在执行上述命令之前,请确保您已经创建了一个虚拟环境。

蟒蛇3:

pip3 install virtualenv
python3 -m venv <myenvname> 
Run Code Online (Sandbox Code Playgroud)

蟒蛇2

pip install virtualenv
virtualenv <myenvname>
Run Code Online (Sandbox Code Playgroud)

之后,将您的源代码放在目录中。如果您现在运行 python 文件,如果您使用的是非本机模块,它可能不会启动。您可以通过运行pip3 install <module>或来安装这些模块pip install <module>

除了您所在的环境之外,这不会影响您的整个模块列表。

现在您可以在顶部执行命令,现在您有一个需求文件,其中仅包含您在虚拟环境中安装的模块。现在您可以运行顶部的命令。

我建议每个人都使用环境,因为当涉及到这样的事情时,它会让事情变得更容易。


The*_*hal 7

如果面临与我相同的问题,即不在虚拟环境中,并且需要针对特定项目或来自所选文件夹(包括子项)的requirements.txt,则 pipreqs 不支持。

您可以使用 :

import os
import sys
from fuzzywuzzy import fuzz
import subprocess

path = "C:/Users/Username/Desktop/DjangoProjects/restAPItest"


files = os.listdir(path)
pyfiles = []
for root, dirs, files in os.walk(path):
      for file in files:
        if file.endswith('.py'):
              pyfiles.append(os.path.join(root, file))

stopWords = ['from', 'import',',','.']

importables = []

for file in pyfiles:
    with open(file) as f:
        content = f.readlines()

        for line in content:
            if "import" in line:
                for sw in stopWords:
                    line = ' '.join(line.split(sw))

                importables.append(line.strip().split(' ')[0])

importables = set(importables)

subprocess.call(f"pip freeze > {path}/requirements.txt", shell=True)

with open(path+'/requirements.txt') as req:
    modules = req.readlines()
    modules = {m.split('=')[0].lower() : m for m in modules}


notList = [''.join(i.split('_')) for i in sys.builtin_module_names]+['os']

new_requirements = []
for req_module in importables:
    try :
        new_requirements.append(modules[req_module])

    except KeyError:
        for k,v in modules.items():
            if len(req_module)>1 and req_module not in notList:
                if fuzz.partial_ratio(req_module,k) > 90:
                    new_requirements.append(modules[k])

new_requirements = [i for i in set(new_requirements)]

new_requirements

with open(path+'/requirements.txt','w') as req:
    req.write(''.join(new_requirements))
Run Code Online (Sandbox Code Playgroud)

PS:它可能有一些额外的库,因为它检查了模糊逻辑。


Moh*_*hod 7

Python 3 的最佳方法是:

pip3 freeze > requirements.txt
Run Code Online (Sandbox Code Playgroud)

它对我有用...

  • 打扰一下,您的答案与 /sf/answers/2342829541/ 有什么不同? (4认同)

Fra*_*cis 7

我盲目地遵循使用 pip3 freeze > requirements.txt 的公认答案

它生成了一个巨大的文件,列出了整个解决方案的所有依赖项,这不是我想要的。

所以你需要弄清楚你要生成什么样的requirements.txt。

如果您需要一个包含所有依赖项的requirements.txt 文件,请使用 pip3

pip3 freeze > requirements.txt
Run Code Online (Sandbox Code Playgroud)

但是,如果您想生成一个仅列出您需要的依赖项的最小 requirements.txt,则使用 pipreqs 包。如果您在项目中的每个组件级别有大量的 requirements.txt 文件而不是解决方案范围级别的单个文件,则特别有用

pip install pipreqs
pipreqs [path to folder]
e.g. pipreqs .
     pipreqs . --force --ignore=tests (Overwrites exisiting requirements.txt, ignores the tests directory)
Run Code Online (Sandbox Code Playgroud)


小智 6

就我而言,我使用Anaconda,因此从我的环境中的conda终端运行以下命令即可解决该问题,并自动为我创建了此需求txt文件:

conda list -e > requirements.txt
Run Code Online (Sandbox Code Playgroud)

这是从Github链接pratos / condaenv.txt中获取的

  • 这真的有效吗?每当我尝试此操作时,都会出现“pip installrequirements.txt”拒绝的依赖项和语法细节。 (3认同)

Mun*_*ram 6

Simple Pythonic Way

To get a list of all the REQUIREMENTS in a standard requirements.txt file, you can use the following command.

pip freeze > requirements.txt
Run Code Online (Sandbox Code Playgroud)

Now, this should automatically create a standard requirements file with all of the packages installed alongside their corresponding versions.

Pretty Print on Terminal

If you just want to get a pretty print on the terminal you can use the following approach.

pip list
Run Code Online (Sandbox Code Playgroud)

This lists all of the installed packages, in a pretty print format.

Custom Dependency

如果你有一个像 Github Repo 这样的项目文件夹,并且你想为项目获得一个自定义的 requirements.txt你可以使用下面的包。 https://pypi.org/project/pipreqs/ pipreqs

用法

$ pipreqs /home/project/location
Successfully saved requirements file in /home/project/location/requirements.txt
Run Code Online (Sandbox Code Playgroud)

requirements.txt 的内容

wheel==0.23.0
Yarg==0.1.9
docopt==0.6.2
Run Code Online (Sandbox Code Playgroud)


jtl*_*lz2 6

@Francis 说得对 - /sf/answers/4600992301/

但只是补充一下:

通过对 Jupyter 笔记本(即.ipynb文件)的额外支持,您现在可以使用https://pypi.org/project/pipreqsnb(与 pipreqs 的语法相同):

pip install pipreqsnb
pipreqsnb .
Run Code Online (Sandbox Code Playgroud)

[我不是作者]


Rex*_*rus 5

如果您只想列出 virtualenv 中使用的包,请使用:

pip freeze -l > requirements.txt 
Run Code Online (Sandbox Code Playgroud)


小智 5

如果你的系统中已经安装了很多依赖,并且你需要一个特定项目的requirements.txt,你可以先安装pipreqs:

$ pip install pipreqs
Run Code Online (Sandbox Code Playgroud)

并在项目文件夹下执行以下命令。

$ pipreqs
Run Code Online (Sandbox Code Playgroud)

此命令将为特定项目生成 requirements.txt 文件。


Rea*_*aas 5

对于python3:(我的机器上有python 2和3,其中python2是默认值)

# install
pip3 install pipreqs

# Run in current directory
python3 -m  pipreqs.pipreqs .
Run Code Online (Sandbox Code Playgroud)

  • 对我进行了一些修改:“pip install pipreqs”,然后“python -m pipreqs.pipreqs”。 (5认同)