Home > Software engineering >  Find Python module name
Find Python module name

Time:10-11

The software package to install Django RESTful framework is djangorestframework:

$ pip freeze|grep rest
djangorestframework==3.11.1

I can find the location of this software with command:

$ pip show djangorestframework
Name: djangorestframework
Version: 3.11.1
Summary: Web APIs for Django, made easy.
Home-page: https://www.django-rest-framework.org/
Author: Tom Christie
Author-email: [email protected]
License: BSD
Location: /home/abc_venv/lib/python3.6/site-packages
Requires: django
Required-by:

$ cd /home/abc_venv/lib/python3.6/site-packages

How do I know which folder is the software installed in (i.e., the name of the module to import)? in this case,

rest_framework 

which is different from the software package name?

CodePudding user response:

You can see the folder inside the djangorestframework-3.12.4.dist-info/top_level.txt file.

I just installed djangorestframework:

$ cat /home/abc_venv/lib/python3.6/site-packages/djangorestframework-3.12.4.dist-info/top_level.txt
rest_framework

If you want to be fancy, you can do it with Python:

import os
import site
from pathlib import Path

site_package_path = site.getsitepackages()[0]  # "/home/abc_venv/lib/python3.6/site-packages"
dist_info_path = [p for p in Path(site.getsitepackages()[0]).rglob("djangorestframework-*")][0]  # "/home/abc_venv/lib/python3.6/site-packages/djangorestframework-3.12.4.dist-info/"
top_level_path = os.path.join(dist_info_path, "top_level.txt")

with open(top_level_path, "r") as f:
    folder_name = "\n".join(l for l in f.readlines()).strip()

print(folder_name)  # "rest_framework"
  • Related