mbilyanov
mbilyanov

Reputation: 2521

Importing multiple files as a single module?

I have been chasing my tail for the last 4 hours here and can't find the solution.

I have the following module/package structure for my project.

.
├── app-cli.py
├── tools
│   ├── __init__.py
│   ├── adapters
│   │   ├── __init__.py
│   │   ├── cli.py
│   │   ├── web.py
│   ├── utils
│   │   ├── __init__.py
│   │   ├── core.py
│   │   │   ├── my_public_method()
│   │   ├── io.py
│   │   │   ├── some_other_public_method()

What I'm trying to do is bundle everything inside utils within the utils name space.

So when I do import tools at the main level, I can access the util functions as:

tools.utils.my_public_method()
tools.utils.some_other_public_method()

Instead of:

 tools.utils.core.my_public_method()
 tools.utils.io.some_other_public_method()

I have been editing the __init__.py messing around with the levels of imports, attempting to create a shortcut but with no success.

Upvotes: 0

Views: 790

Answers (1)

abc
abc

Reputation: 11949

In your __init__.py inside the utils package you can add

from .core import my_public_method
from .io import some_other_public_method

and then you can do:

import tools.utils

tools.utils.my_public_method()
tools.utils.some_other_public_method()

Upvotes: 2

Related Questions