campovski
campovski

Reputation: 3153

Export everything from Python module or import everything from module

This may be well written somewhere on the internet (even on SO) but I could not find it.


Suppose I have a Python project structure like the following

mymodule
|—-__init__.py
|—-a.py
|—-b.py
|—-c.py
main.py

Now, I want to import in main.py everything from mymodule, meaning I want to be able to do the following:

import mymodule

mymodule.a.something()
mymodule.b.something_else()

I do not want to do the following:

import mymodule.a
import mymodule.b
import mymodule.c

How can I achieve this? In JS ES6, you would create a file mymodule.js for example, in which you would import everything that you want to export, and then export it. Is there something similar possible in Python?

Upvotes: 0

Views: 833

Answers (1)

Dolev Pearl
Dolev Pearl

Reputation: 284

As previously mentioned, when importing a module you are essentially importing its __init__.py file. The correct way to handle this is to import whatever you need inside mymodule/__init__.py, and then all those imports will be available to whoever imports mymodule.

Upvotes: 2

Related Questions