有时我从github下载python源代码,不知道如何安装所有的依赖项。如果没有requirements.txt文件,我必须手工创建它。 问题是: 给定python源代码目录,是否有可能从导入部分自动创建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
其他回答
如果你在你的系统中安装了很多依赖项,并且你需要一个特定项目的requirements.txt,你可以安装第一个pipreqs:
$ pip install pipreqs
并在项目文件夹下执行以下命令。
$ pipreqs
该命令将为特定项目生成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))
附注:它可能有一些额外的库,因为它检查模糊逻辑。
建议使用Pipenv或其他工具来改进开发流程。
pip3 freeze > requirements.txt # Python3
pip freeze > requirements.txt # Python2
如果您不使用虚拟环境,pigar将是一个很好的选择。
你可以用命令来做。它将创建requirements .txt并自动添加相关模块。
For Unix : pip3 freeze > requirements.txt
For Windos: pip freeze > requirements.txt
对于python3:(我的机器上有python2和python3,其中python2是默认值)
# install
pip3 install pipreqs
# Run in current directory
python3 -m pipreqs.pipreqs .
python2:
pip install pipreqs
python -m pipreqs.pipreqs .
检查你的python版本:
python --version
推荐文章
- 如何在Flask-SQLAlchemy中按id删除记录
- 在Python中插入列表的第一个位置
- Python Pandas只合并某些列
- 如何在一行中连接两个集而不使用“|”
- 从字符串中移除前缀
- 代码结束时发出警报
- 如何在Python中按字母顺序排序字符串中的字母
- 在matplotlib中将y轴标签添加到次要y轴
- 如何消除数独方块的凹凸缺陷?
- 为什么出现这个UnboundLocalError(闭包)?
- 使用Python请求的异步请求
- 如何检查一个对象是否是python中的生成器对象?
- 如何从Python包内读取(静态)文件?
- 如何计算一个逻辑sigmoid函数在Python?
- python: SyntaxError: EOL扫描字符串文字