Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list (freeze) only the Python modules imported/required by my project

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

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)

==============

UPDATE

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

Ok, this is closer to my only two non-builtin imports (PyYAML and requests).

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!

All the other requirements (certifi, chardet, idna, urllib3) are actually requirements of requests.

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
like image 492
4 revs, 2 users 76% Avatar asked Oct 30 '22 00:10

4 revs, 2 users 76%


1 Answers

=> https://pypi.python.org/pypi/pipdeptree

However, you will need to create an empty virtualenv and reinstall the main app/library to see only the dependencies of it, with no manually installed packages.

like image 184
Sergey Vasilyev Avatar answered Nov 15 '22 07:11

Sergey Vasilyev