Skip to content

Instantly share code, notes, and snippets.

@twidi
Last active August 29, 2015 14:17
Show Gist options
  • Save twidi/b88cdbdbf7e45dd782db to your computer and use it in GitHub Desktop.
Save twidi/b88cdbdbf7e45dd782db to your computer and use it in GitHub Desktop.
Find all non-builtins import in a python project
- we use `ack` (`ack-grep`)
- we assume builtins are marked so, or in a `python*` directory in a `lib` one, but not in `*packages*` (`site-packages for example`)
- everything that cannot be imported is not a builtin package
If your python is not in a "lib/pythonX.Y" directory, you can change the path.
PS: on my machine, it works in a virtualenv
ack --py '^\s*(from [^.][^"\s]+\s)?import' -h | sed -r 's/^\s+//' |sed -r ' s/^(from|import) (\w+).*/\2/' | sort | uniq | xargs python -c "
import sys, imp
for mod in sys.argv[1:]:
try:
res = imp.find_module(mod)
except:
print(mod)
else:
if res[2][2] != imp.C_BUILTIN and ('lib/python' not in res[1] or 'packages' in res[1]):
print(mod)
"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment