Skip to content

Instantly share code, notes, and snippets.

Last active October 3, 2016 12:39
Show Gist options
  • Save chkumar246/29eb4d28a85c5837b48ee574c60a71f6 to your computer and use it in GitHub Desktop.
Save chkumar246/29eb4d28a85c5837b48ee574c60a71f6 to your computer and use it in GitHub Desktop.
Script to find out all third party imports from a code repository
# Script to find all third party imports from python projects
# How to Run
# sudo pip install find-import virtualenv pymod2pkg
# python <path to python code repository>
# Author: Chandan Kumar <>
import pymod2pkg
import os
import shutil
import subprocess
import shlex
import sys
import virtualenv
import tempfile
def list_all_pyfiles(path):
list down all .py files with complete path
files_path = []
for root, subdir, files in os.walk(path):
for file in files:
if file.endswith('.py'):
files_path.append(os.path.join(root, file))
return files_path
def get_imports(filepath):
Returns a list of all imports
output = subprocess.check_output(['find-import', filepath])
if output.split():
return output.split()
def get_all_imports(file_list):
Return a dict of all imports with all imports
{'file_path':[a, b, c, d]}
imports, import_list = {}, []
for file in file_list:
if get_imports(file) != None:
imports.update({file: get_imports(file)})
for data in imports.values():
return list(set(import_list))
def check_import(modules):
Find thirdparty modules
third_party_imports = []
path = tempfile.mkdtemp()
virtualenv.create_environment(path, clear=True)
venv_path = os.path.join(path, 'bin', 'python')
for module in modules:
# FIX ME corner case for import import and import the statement
if not module in ['import', 'the']:
cmd = shlex.split("%s -c 'try:\n import %s\n print('True')\nexcept ImportError: print('False')'" % (venv_path, module))
out = subprocess.check_output(cmd)
if 'False'in out.split():
# Remove virtualenv
return third_party_imports
def list_third_party_pkgs(third_party_imports):
List all third party packages from imports
pkgs = [pymod2pkg.module2package(module, pymod2pkg.platform.linux_distribution()[0]) for module in third_party_imports]
return pkgs
if __name__ == "__main__":
if len(sys.argv) > 1:
if os.path.exists(sys.argv[1]):
files = list_all_pyfiles(sys.argv[1])
imports = get_all_imports(files)
third_party_imports = check_import(imports)
pkgs = list_third_party_pkgs(third_party_imports)
print("Path Not found.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment