Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of actuals import names in python

Tags:

python

pip

With "pip freeze" I'll get a list of package names. e.g.:

Django==1.9.7
psycopg2==2.6.1
djangorestframework==3.3.3
djangorestframework-jwt==1.8.0
django-rest-swagger==0.3.7
django-environ==0.4.0
python-dateutil==2.5.3
django-sendfile==0.3.10

Is there any way to receive a list of actual names to import? e.g. instead of djangorestframework => rest_framework

like image 852
kharandziuk Avatar asked Jun 15 '16 09:06

kharandziuk


2 Answers

You may use the standard pkgutil module to get the list of top-level imports like this:

import pkgutil
list(pkgutil.iter_modules())

That will only find modules that live in regular files, zip files or another loader that supports module enumration. Should be most of them on a standard system.

The result is a list of 3-tuple, with the loader, the module name, and whether it is a single module or a package. If you are only interested in the module name, simply do:

list(item[1] for item in pkgutil.iter_modules())
like image 117
spectras Avatar answered Oct 11 '22 14:10

spectras


Yes, top_level.txt will be a correct module name. You can use pkg_resources module to extract metadata from packages.

Python code for this:

import pkg_resources


def read_requirements(requirements_file):
    with open(requirements_file, 'r') as f:
        return f.read().splitlines()


def get_package_name(package):
    return list(pkg_resources.get_distribution(package)._get_metadata('top_level.txt'))[0]


requirements = read_requirements('/path/to/requirements.txt')
packages = [get_package_name(p) for p in requirements]
like image 45
doomatel Avatar answered Oct 11 '22 16:10

doomatel