问题描述
我想生成 requirements.txt
文件.在 MacOS 上使用 pip freeze
时,我得到一长串模块:
I want to generate the requirements.txt
file. When using pip freeze
on MacOS, I get a long list of modules:
altgraph==0.10.2
ansible==2.2.1.0
bdist-mpkg==0.5.0
bonjour-py==0.3
certifi==2017.7.27.1
cffi==1.9.1
chardet==3.0.4
cryptography==1.7.1
enum34==1.1.6
idna==2.6
ipaddress==1.0.18
Jinja2==2.8.1
macholib==1.5.1
MarkupSafe==0.23
matplotlib==1.3.1
modulegraph==0.10.4
numpy==1.8.0rc1
paramiko==2.1.1
py2app==0.7.3
pyasn1==0.1.9
pycparser==2.17
pycrypto==2.6.1
pyobjc-core==2.5.1
pyobjc-framework-Accounts==2.5.1
pyobjc-framework-AddressBook==2.5.1
pyobjc-framework-AppleScriptKit==2.5.1
pyobjc-framework-AppleScriptObjC==2.5.1
pyobjc-framework-Automator==2.5.1
pyobjc-framework-CFNetwork==2.5.1
pyobjc-framework-Cocoa==2.5.1
pyobjc-framework-Collaboration==2.5.1
pyobjc-framework-CoreData==2.5.1
pyobjc-framework-CoreLocation==2.5.1
pyobjc-framework-CoreText==2.5.1
pyobjc-framework-DictionaryServices==2.5.1
pyobjc-framework-EventKit==2.5.1
pyobjc-framework-ExceptionHandling==2.5.1
pyobjc-framework-FSEvents==2.5.1
pyobjc-framework-InputMethodKit==2.5.1
pyobjc-framework-InstallerPlugins==2.5.1
pyobjc-framework-InstantMessage==2.5.1
pyobjc-framework-LatentSemanticMapping==2.5.1
pyobjc-framework-LaunchServices==2.5.1
pyobjc-framework-Message==2.5.1
pyobjc-framework-OpenDirectory==2.5.1
pyobjc-framework-PreferencePanes==2.5.1
pyobjc-framework-PubSub==2.5.1
pyobjc-framework-QTKit==2.5.1
pyobjc-framework-Quartz==2.5.1
pyobjc-framework-ScreenSaver==2.5.1
pyobjc-framework-ScriptingBridge==2.5.1
pyobjc-framework-SearchKit==2.5.1
pyobjc-framework-ServiceManagement==2.5.1
pyobjc-framework-Social==2.5.1
pyobjc-framework-SyncServices==2.5.1
pyobjc-framework-SystemConfiguration==2.5.1
pyobjc-framework-WebKit==2.5.1
pyOpenSSL==0.13.1
pyparsing==2.0.1
python-dateutil==1.5
pytz==2013.7
PyYAML==3.12
requests==2.18.4
scipy==0.13.0b1
six==1.4.1
urllib3==1.22
vboxapi==1.0
xattr==0.6.4
zope.interface==4.1.1
实际上,我只知道我使用的 yaml
、requests
和 json
导入(除了 os
和 sys
).
Actually, I am only aware of the yaml
, requests
and json
imports I use (besides os
and sys
).
如果有一个命令只列出我的项目/文件正在导入的第一级模块(没有子依赖项或系统库)
If there a command to list me only the first level of modules that my project/file is importing (without sub-dependencies or system libraries)
================
==============
更新
我尝试了 virtualenv
和 pip freeze
现在报告:
I tried virtualenv
and pip freeze
now reports:
certifi==2017.7.27.1
chardet==3.0.4
idna==2.6
PyYAML==3.12
requests==2.18.4
urllib3==1.22
好的,这更接近于我仅有的两个非内置导入(PyYAML
和 requests
).
Ok, this is closer to my only two non-builtin imports (PyYAML
and requests
).
并且 virtualenv
确认我的 requirements.txt
中应该只有这两个模块:
And virtualenv
confirms that I should only have the two modules in my requirements.txt
:
(MYENV)$ python ./script.py
...
import yaml
ImportError: No module named yaml
(MYENV)$ pip install PyYAML
Collecting PyYAML
Installing collected packages: PyYAML
Successfully installed PyYAML-3.12
(MYENV)$ python ./script.py
...
import requests
ImportError: No module named requests
(MYENV)$ pip install requests
Collecting requests
Using cached requests-2.18.4-py2.py3-none-any.whl
Collecting urllib3<1.23,>=1.21.1 (from requests)
Using cached urllib3-1.22-py2.py3-none-any.whl
Collecting idna<2.7,>=2.5 (from requests)
Using cached idna-2.6-py2.py3-none-any.whl
Collecting chardet<3.1.0,>=3.0.2 (from requests)
Using cached chardet-3.0.4-py2.py3-none-any.whl
Collecting certifi>=2017.4.17 (from requests)
Using cached certifi-2017.7.27.1-py2.py3-none-any.whl
Installing collected packages: urllib3, idna, chardet, certifi, requests
Successfully installed certifi-2017.7.27.1 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22
(MYENV)$ python ./script.py
It works!
所有其他要求(certifi
、chardet
、idna
、urllib3
)实际上都是请求.
All the other requirements (certifi
, chardet
, idna
, urllib3
) are actually requirements of requests
.
那么,有没有办法(除了手动清理)只获取导入的顶级依赖项?我预期的 requirements.txt
将是:
So, is there a way (other than the manual cleanup) to get only the imported top dependencies? My expected requirements.txt
would be:
PyYAML==3.12
requests==2.18.4
推荐答案
老问题,但以防万一有人仍然感兴趣.从 pipdeptree pip 站点,您可以使用 pipdeptree |grep -P '^\w+'
仅打印顶级依赖项.
Old question, but just in case somebody is still interested.From the pipdeptree pip site you can use pipdeptree | grep -P '^\w+'
to print only the top-leve dependencies.
如果您使用的是 Mac 版本的 grep,那么您可能必须先使用 brew 安装 grep 并执行 pipdeptree |ggrep -P '^\w+'
之后.
If you are using a Mac version of grep than you may have to install grep with brew first and do pipdeptree | ggrep -P '^\w+'
afterwards.
这篇关于如何仅列出(冻结)我的项目导入/需要的 Python 模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!