Skip to content

Instantly share code, notes, and snippets.

@phpdude phpdude/Result.example.log
Last active Mar 17, 2016

Embed
What would you like to do?
Find used python packages list needed to be installed (or used in requirements.txt)
grep -Rhs --color=never import . | awk '{print $2;}' | sort | uniq | xargs -I{} sh -c 'python -c "list(__import__(x) for x in filter(None, map(str.strip, '"'"'{}'"'"'.split('"'"','"'"'))))" 2>/dev/null || echo {}'
GcsManager
Robot
WitInterface
WitResponse
XmppReporter
cjson
cloudsql
cloudsql.beans
cloudsql.dao
cloudsql.dao.dialogdao
cloudstorage
context
demoprotocol
dialogentrydao,historyentrydao,botuserdao
file
flask
flask.ext.babel
flask.json
flask_babel
geocoding
google.appengine._internal.django.utils
google.appengine.api
google.appengine.api.apiproxy_stub_map
google.appengine.api.app_identity
google.appengine.api.taskqueue
google.appengine.ext
google.appengine.ext.webapp
history
jsonpath_rw
langdetect
lazyawarejsonencoder
peewee
playhouse.shortcuts
plivogae
rtiprotocol
simplejson
smsinterface
spartanprotocol
speaklater
telegram
telegramInterface
tropointerface
twilio.twiml
useraddress
xmltodict
yaml
@phpdude

This comment has been minimized.

Copy link
Owner Author

commented Mar 17, 2016

Sometimes you getting dirty code where was not used requirements.txt file or you want to check code for dependencies (maybe your source code requires some libraries which are not installed in current virtualenv yet).

If you want to get python required python packages from scratch - create temporary virtualenv & activate it before running this code.

This short code features:

  • grep source files for import statements
  • extract imported library path from import statements (simply uses next column from the line)
  • split imported libraries by ","
  • try to import library
  • if failed to import - it prints imported libraries list

Try it! :)

@phpdude

This comment has been minimized.

Copy link
Owner Author

commented Mar 17, 2016

Also this solution can be used as alias in ~/.profile file for use as a command in your #bash.

Just drop these lines to your ~/.profile file and do . ~/.profile in current bash session to reload this file. After this you can use python.libraries command in your bash session.

_python_libraries () {
        grep -Rhs --color=never import . | awk '{print $2;}' | sort | uniq | xargs -I{} sh -c 'python -c "list(__import__(x) for x in filter(None, map(str.strip, '"'"'{}'"'"'.split('"'"','"'"'))))" 2>/dev/null || echo {}'
}
alias python.libraries='_python_libraries'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.