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
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())
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With