Using __init__ to connect modules
When we create a new Python package using the PyDev plugin for the Eclipse IDE, it automatically creates an __init__.py
module. We can also create it ourselves manually, when not using Eclipse.
Note
The __init__.py
module is usually empty and, then, has a size of 0 kilobytes.
We can use this usually empty module to connect different Python modules by entering code into it. This recipe will show how to do this.
Getting ready
We will create a new GUI similar to the one we created in the previous recipe, Avoiding spaghetti code.
How to do it…
As our project becomes larger and larger, we naturally break it out into several Python modules. Sometimes it can be a little bit complicated to find modules that are located in different subfolders, either above or below the code that needs to import it.
One practical way to get around this limitation is to use the __init__.py
module.
Note
In Eclipse, we can set the Eclipse internal project PyDev
PYTHONPATH
to certain folders and...