Last active
August 29, 2015 14:17
-
-
Save twidi/b88cdbdbf7e45dd782db to your computer and use it in GitHub Desktop.
Find all non-builtins import in a python project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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