Python 自动创建requirements.txt
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31684375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Automatically create requirements.txt
提问by Igor Barinov
Sometimes I download the python source code from github
and don't know how to install all the dependencies. If there is no requirements.txt
file I have to create it by hands.
The question is:
Given the python source code directory is it possible to create requirements.txt
automatically from the import section?
有时我下载了 python 源代码github
,但不知道如何安装所有依赖项。如果没有requirements.txt
文件,我必须手动创建它。问题是:鉴于python源代码目录是否可以requirements.txt
从导入部分自动创建?
采纳答案by damnever
If you use virtual environment, pip freeze > requirements.txt
just fine. IF NOT, pigarwill be a good choice for you.
如果你使用虚拟环境,pip freeze > requirements.txt
就好了。如果没有,pigar将是您的不错选择。
By the way, I do not ensure it will work with 2.6.
顺便说一句,我不确保它适用于 2.6。
UPDATE:
更新:
Pipenvor other tools is recommended for improving your development flow.
建议使用Pipenv或其他工具来改进您的开发流程。
For Python 3use below
对于Python 3使用下面
pip3 freeze > requirements.txt
回答by DJanssens
You can use the following code to generate a requirements.txt file:
您可以使用以下代码生成一个 requirements.txt 文件:
pip install pipreqs
pipreqs /path/to/project
more info related to pipreqs can be found here.
可以在此处找到与 pipreqs 相关的更多信息。
Sometimes you come across pip freeze
, but this saves all packages in the environment including those that you don't use in your current project.
有时您会遇到pip freeze
,但这会保存环境中的所有包,包括您当前项目中未使用的包。
回答by HassanSh__3571619
In my case, I use Anaconda, so running the following command from conda terminal inside my environment solved it, and created this requirements txt file for me automatically:
就我而言,我使用 Anaconda,因此在我的环境中从 conda 终端运行以下命令解决了它,并自动为我创建了这个需求 txt 文件:
conda list -e > requirements.txt
This was taken from this Github link pratos/condaenv.txt
这是从这个 Github 链接pratos/condaenv.txt 中获取的
If an error been seen, and you are using anaconda, try to use the .yml option:
如果看到错误,并且您正在使用 anaconda,请尝试使用 .yml 选项:
conda env export > <environment-name>.yml
For other person to use the environment...Or if you are creating a new enviroment on other machine: conda env create -f .yml
对于其他人使用环境...或者如果您正在其他机器上创建新环境: conda env create -f .yml
回答by johnyjohnny
Make sure to run pip3 for python3.7.
确保为 python3.7 运行 pip3。
pip3 freeze >> yourfile.txt
Before executing the above command make sure you have created a virtual environment.
在执行上述命令之前,请确保您已经创建了一个虚拟环境。
python3:
蟒蛇3:
pip3 install virtualenv
python3 -m venv <myenvname>
python2:
蟒蛇2:
pip install virtualenv
virtualenv <myenvname>
After that put your source code in the directory. If you run the python file now, probably It won't launch If you are using non-native modules. You can install those modules runing
之后,将您的源代码放在目录中。如果你现在运行 python 文件,可能它不会启动如果你使用的是非本地模块。您可以安装这些模块运行
pip3 install <module> or pip install <module>
This will not affect you entire module list except the environment you are In.
除了您所处的环境之外,这不会影响您的整个模块列表。
Now you can execute the command at the top and now you have a requirements file which contains only the modules you installed in the virtual environment. Now you can run the command at the top.
现在您可以在顶部执行命令,现在您有一个需求文件,其中仅包含您在虚拟环境中安装的模块。现在您可以运行顶部的命令。
I advise everyone to use environments as It makes things easier when It comes to stuff like this.
我建议每个人都使用环境,因为当涉及到这样的事情时,它会让事情变得更容易。
Hope this helped.
希望这有帮助。
回答by DARK_C0D3R
If Facing the same issue as mine i.e. not on the virtual environmentand wants requirements.txt for a specific projector from the selected folder(includes children)and pipreqs is not supporting.
如果面临与我相同的问题,即不在虚拟环境中,并且需要针对特定项目或来自所选文件夹(包括子项)的requirements.txt,则 pipreqs 不支持。
You can use :
您可以使用 :
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))
P.S: It may have a few additional libraries as it checks on fuzzylogic.
PS:它可能有一些额外的库,因为它检查了模糊逻辑。
回答by Mac Rathod
best way for Python 3 is:
Python 3 的最佳方法是:
pip3 freeze > requirements.txt
it worked for me...
它对我有用...
回答by Ahmet
if you are using PyCharm, when you open or clone the project into the PyCharm it shows an alert and ask you for installing all necessary packages.
如果您使用的是 PyCharm,当您打开项目或将项目克隆到 PyCharm 时,它会显示警告并要求您安装所有必要的包。