Python has the ability to create a namespace packages. How do you get a list of installed packages under a namespace?
i.e. something like:
import namespace_package
dir(namespace_pageage)
Python has the ability to create a namespace packages. How do you get a list of installed packages under a namespace?
i.e. something like:
import namespace_package
dir(namespace_pageage)
From the Python Packaging User Guide:
import pkgutil
list(
pkgutil.iter_modules(
namespace_package.__path__,
namespace_package.__name__ + "."
)
)
Note that this will not return (sub-)namespace packages within the main namespace package (i.e. if you have nested namespace packages).
You can use the find_packages function from setuptools
. This function is often used in setup.py
files but you can use it for other purposes too.
# find the folder of the package
from pathlib import Path
package_root_folder = Path(namespace_package.__file__).parent
# for that folder, detect all Python packages inside
from setuptools import find_packages
all_packages = find_packages(str(package_root_folder))