# Dynamically discover and import Python modules, classes, and functions.
## Install
`pip install dynamic_imports`
## Examples
### Import a module via module name or file path
```python
from dynamic_imports import import_module
module = import_module('my_package.my_module')
# or
module = import_module('/home/user/my_package/my_module.py')
```
### Import a module attribute
```python
from dynamic_imports import import_module_attr
function = import_module_attr('my_package.my_module', 'my_function')
# or
function = import_module_attr('/home/user/my_package/my_module.py', 'my_function')
```
### Find all modules in a package or nested packages
```python
from dynamic_imports import discover_modules
modules = discover_modules(
package=my_package, # str `my_package' works too.
search_subpackages=True,
# return the actual module objects, not str names.
names_only=False # this is the default value...
)
```
The `search_in` argument for all functions below can be an imported package or module, string name of the package (e.g. `"my_package"`) or module (e.g. `"my_package.my_module"`), or path to a python file (e.g. `"/home/user/my_package/my_module.py"`)
### Find all implementations of a base class within a module.
```python
from dynamic_imports import class_impls
from my_package.my_module import Base
from my_package import my_module
my_classes = class_impls(
base_class=Base, # str 'Base' works too
search_in=my_module,
names_only=False # this is the default value...
)
```
### Find all implementations of a base class within nested packages.
```python
from dynamic_imports import class_impls
from my_package.my_module import Base
import my_package
my_classes = class_impls(
base_class=Base, # str 'Base' works too.
search_in=my_package
search_subpackages=True,
names_only=False # this is the default value...
)
```
### Find all instances of a class within a module.
```python
from dynamic_imports import class_inst
from my_package import my_module
from my_package.my_module import MyClass
my_classes_instances = class_inst(
search_in=my_module,
class_type=MyClass
)
```
### Find all instances of a class within nested packages.
```python
from dynamic_imports import class_inst
from my_package.my_module import MyClass
import my_package
my_classes_instances = class_inst(
class_type=MyClass,
search_in=my_package,
search_subpackages=True,
)
```
Raw data
{
"_id": null,
"home_page": "https://github.com/djkelleher/dynamic-imports",
"name": "dynamic-imports",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8.1,<4.0.0",
"maintainer_email": "",
"keywords": "",
"author": "Dan Kelleher",
"author_email": "kelleherjdan@gmail.com",
"download_url": "",
"platform": null,
"description": "# Dynamically discover and import Python modules, classes, and functions.\n\n## Install\n`pip install dynamic_imports`\n\n## Examples\n### Import a module via module name or file path\n```python\nfrom dynamic_imports import import_module\nmodule = import_module('my_package.my_module')\n# or\nmodule = import_module('/home/user/my_package/my_module.py')\n```\n### Import a module attribute\n```python\nfrom dynamic_imports import import_module_attr\n\nfunction = import_module_attr('my_package.my_module', 'my_function')\n# or\nfunction = import_module_attr('/home/user/my_package/my_module.py', 'my_function')\n```\n### Find all modules in a package or nested packages\n```python\nfrom dynamic_imports import discover_modules\n\nmodules = discover_modules(\n package=my_package, # str `my_package' works too.\n search_subpackages=True,\n # return the actual module objects, not str names.\n names_only=False # this is the default value...\n)\n```\nThe `search_in` argument for all functions below can be an imported package or module, string name of the package (e.g. `\"my_package\"`) or module (e.g. `\"my_package.my_module\"`), or path to a python file (e.g. `\"/home/user/my_package/my_module.py\"`)\n### Find all implementations of a base class within a module.\n```python\nfrom dynamic_imports import class_impls\nfrom my_package.my_module import Base\nfrom my_package import my_module\n\nmy_classes = class_impls(\n base_class=Base, # str 'Base' works too\n search_in=my_module,\n names_only=False # this is the default value...\n)\n```\n### Find all implementations of a base class within nested packages.\n```python\nfrom dynamic_imports import class_impls\nfrom my_package.my_module import Base\nimport my_package\n\nmy_classes = class_impls(\n base_class=Base, # str 'Base' works too.\n search_in=my_package\n search_subpackages=True,\n names_only=False # this is the default value...\n)\n\n```\n### Find all instances of a class within a module.\n```python\nfrom dynamic_imports import class_inst\nfrom my_package import my_module\nfrom my_package.my_module import MyClass\n\nmy_classes_instances = class_inst(\n search_in=my_module,\n class_type=MyClass\n)\n```\n### Find all instances of a class within nested packages.\n```python\nfrom dynamic_imports import class_inst\nfrom my_package.my_module import MyClass\nimport my_package\n\nmy_classes_instances = class_inst(\n class_type=MyClass,\n search_in=my_package,\n search_subpackages=True,\n)\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Dynamically discover and import Python modules, classes, and functions.",
"version": "1.0.2",
"project_urls": {
"Homepage": "https://github.com/djkelleher/dynamic-imports",
"Repository": "https://github.com/djkelleher/dynamic-imports"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "58275fa5301081ba8f9600fcf09cea787395ca8a02eeeb8ced8f247bb47eda30",
"md5": "3357120c953fcf74c6cfcff5dc5f03f0",
"sha256": "3930b9112eb5d103610bd2272e3c1666143e4d8c7da623a8f641425c4cc1bcb2"
},
"downloads": -1,
"filename": "dynamic_imports-1.0.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "3357120c953fcf74c6cfcff5dc5f03f0",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8.1,<4.0.0",
"size": 4047,
"upload_time": "2024-01-05T00:53:15",
"upload_time_iso_8601": "2024-01-05T00:53:15.311986Z",
"url": "https://files.pythonhosted.org/packages/58/27/5fa5301081ba8f9600fcf09cea787395ca8a02eeeb8ced8f247bb47eda30/dynamic_imports-1.0.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-01-05 00:53:15",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "djkelleher",
"github_project": "dynamic-imports",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "dynamic-imports"
}