Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save CMCDragonkai/510ce9456a0429f616baa243d1de3dbf to your computer and use it in GitHub Desktop.
Save CMCDragonkai/510ce9456a0429f616baa243d1de3dbf to your computer and use it in GitHub Desktop.
Exporting Modules and Functions from Python `__init__.py` #python

Exporting Modules and Functions from Python __init__.py

Any directory with __init__.py is considered a package in Python.

Any python files inside a package is considered a module.

Modules contain functions and other bindings that is always exported.

If you are outside the package, and you want to import a module from a package:

from package import module
module.use_function()

However this is only because the __init__.py is empty. We can allow the package to export functions and modules as well. Which means this should be possible:

import package
package.module.use_function()
package.use_function()

To do this, the package's __init__.py must contain something like:

from . import module
from .mod import *

Therefore the __init__.py is kind of like an exporting script for the package. Similar to how I use index.js in my JavaScript packages. This means you can use the package __init__.py as a sort of staging point for all exports. And anything that isn't exported here is hidden. Unless the user really wants to acquire it, in which case they can explicitly use the module.

To actually have encapsulation at the module level. You need to use _ prefix on your bindings. This is different from __ prefix used in classes. Note that using the * import will ignore module bindings that have _ prefixed. But they can still be accessed explicitly if the module is directly accessed. See: https://stackoverflow.com/a/1547160/582917

I think this form of using __init__.py is the best way. Users shouldn't need to use the from ... import syntax unless they need to import specific bindings and don't want to use qualified modules.

@vmsh0
Copy link

vmsh0 commented Aug 24, 2022

from . import module
from .mod import *

what is ".mod" ?

Not sure what it's meant to represent here.

@Mohlady
Copy link

Mohlady commented Nov 6, 2022

I want to add a package with some files to the path after that use. import module. How can do that?

@Lamby777
Copy link

It took me ages to find this document and figure out, that . is needed to be prepended to module name. Thank you.

Same. Forgot that was a thing. TYSM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment