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


当前回答

如果遇到和我一样的问题,即不在虚拟环境中,并且想要特定项目的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))

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

其他回答

@Francis说得对- https://stackoverflow.com/a/65728461/1021819

但我要补充一点:

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

pip install pipreqsnb
pipreqsnb .

[我不是作家]

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

jupyter nbconvert --to=python

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

pip install pipreqs

现在我们可以使用下面的代码创建txt文件。如果你和你的文件在同一路径,你可以写。/。否则,你需要给你的文件路径。

pipreqs ./

or

pipreqs /home/project/location

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

你可以用命令来做。它将创建requirements .txt并自动添加相关模块。

For Unix : pip3 freeze > requirements.txt
For Windos: pip freeze > requirements.txt

简单的python方式

要获得标准REQUIREMENTS .txt文件中所有REQUIREMENTS的列表,您可以使用以下命令。

pip freeze > requirements.txt

现在,这将自动创建一个标准需求文件,其中包含安装在相应版本旁边的所有包。

终端打印精美

如果你只是想在终端上得到一个漂亮的打印,你可以使用下面的方法。

pip list

它以漂亮的打印格式列出了所有已安装的包。

自定义的依赖

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

使用

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

requirements.txt的内容

wheel==0.23.0
Yarg==0.1.9
docopt==0.6.2

我创建了这个bash命令。

for l in $(pip freeze); do p=$(echo "$l" | cut -d'=' -f1); f=$(find . -type f -exec grep "$p" {} \; | grep 'import'); [[ ! -z "$f" ]] && echo "$l" ; done;