[英]Requirements.txt from import statements?
I have cloned in a Pycharm project, a Github repo that does not have a requirements.txt
file.我已经克隆了一个 Pycharm 项目,一个没有
requirements.txt
文件的 Github 存储库。
Rather than manually identifying all modules to pip install, is it possible to create a requirements.txt
automatically, based on all import
statements across the entire project?与其手动识别 pip 安装的所有模块,是否可以根据整个项目的所有
import
语句自动创建requirements.txt
?
I only know how to create one when I build the project myself, and the modules are already installed, using pip3 freeze > requirements.txt
当我自己构建项目时,我只知道如何创建一个,并且模块已经安装,使用
pip3 freeze > requirements.txt
You could try pipreqs (Generate pip requirements.txt file based on imports of any project): https://github.com/bndr/pipreqs您可以尝试 pipreqs(根据任何项目的导入生成 pip requirements.txt 文件): https://github.com/bndr/pipreqs
pip install pipreqs
You could try modulefinder You can use it as a script: python -m modulefinder myscript.py.您可以尝试modulefinder您可以将其用作脚本:python -m modulefinder myscript.py。 This way you will be able to get all the modules required.
这样,您将能够获得所需的所有模块。 They have a great example on the page:
他们在页面上有一个很好的例子:
from modulefinder import ModuleFinder
finder = ModuleFinder()
finder.run_script('bacon.py')
print('Loaded modules:')
for name, mod in finder.modules.items():
print('%s: ' % name, end='')
print(','.join(list(mod.globalnames.keys())[:3]))
print('-'*50)
print('Modules not imported:')
print('\n'.join(finder.badmodules.keys()))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.