我想生成requirements.txt文件.在MacOS上使用pip冻结时,我得到一长串模块:
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).
如果有一个命令只列出我的项目/文件正在导入的第一级模块(没有子依赖项或系统库)
==============
UPDATE
我试过virtualenv和pip freeze现在报告:
certifi==2017.7.27.1
chardet==3.0.4
idna==2.6
PyYAML==3.12
requests==2.18.4
urllib3==1.22
好吧,这更接近我唯一的两个非内置导入(PyYAML和请求).
virtualenv确认我应该只在我的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)实际上是请求的要求.
那么,是否有一种方法(除手动清理之外)只获取导入的顶级依赖项?我期望的requirements.txt将是:
PyYAML==3.12
requests==2.18.4
最佳答案 =>
https://pypi.python.org/pypi/pipdeptree
但是,您需要创建一个空的virtualenv并重新安装主应用程序/库以仅查看它的依赖项,而不需要手动安装的软件包.