Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python: How to not need to import from my moduels like from web.web import WebConfig

I have a python app with a file tree like so

├── app
│   ├── __init__.py
│   ├── app.py
│   └── utils.py
├── files.json
├── hello.txt
├── main.py
└── web
    ├── __init__.py
    ├── utils.py
    └── web.py

In my main.py, I have to import the contents of web.py with the import statement
from web.web import *. How can I shorten this to from web import * while still keeping web.py and utils.py in their own folder? Is there something I can edit in my __init__.py?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you can import things in web/__init__.py and your main.py will import these things when from web import *.

try this:

# main.py
from web import *

print(TEST)
# web/__init__.py
from .web import *
# web/web.py
TEST = "test"

and you will see test in the console when you run python main.py

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading