How to Simplify Python Imports with Explicit Packaging?
A step towards better Python project development.
When developing in Python, it is always recommended to package your project.
Simply put, if a project is packaged, you can import stuff from it.
Some terminology before proceeding ahead:
Module: A Python file.
Package: A collection of Python files in a directory.
Library: A collection of Packages.
We can package a project by adding an __init__.py
file inside a directory.
While Python 3.3+ provides Implicit Namespace Packages — a directory with modules is considered a package by default, it is still advised to create an explicit __init__.py
file.
A couple of major benefits of doing this are that it helps in:
Explicitly specifying which classes/functions can be imported from the package.
Avoiding redundant imports.
Let’s understand!
Consider this is our directory structure (and we are using Python 3.3+):
train.py
has aTraining
class.test.py
has aTesting
class.
As we are using Python 3.3+, we can directly import the Training
and Testing
class in pipeline.py
as follows:
While this will work as expected, the problem is that we have to explicitly import the specific class from each of the modules.
This creates redundant imports.
Defining the __init__.py
file can simplify this.
Let’s see how.
As depicted above:
We first explicitly package the directory by creating an
__init__.py
file.Next, we specify the imports directly in this file.
Now, instead of writing redundant imports, you can directly import the intended classes from the “model” package, as shown below:
In other words, specifying the __init__.py
file lets you treat your package like a module.
This simplifies your imports.
Also, as discussed earlier, an __init__.py
file lets you explicitly specify which classes/functions can be imported from the package, which, otherwise, will not be evident.
This simplifies things for other users of your project.
Isn’t that cool?
If you want to learn more about Python project development, packaging, handling imports from one place to another, best practices, etc., I am open to writing a full article on this.
Please mark your interest below:
I think this skill can be immensely useful in helping you build bigger and better Python projects and gain more confidence in Python development :)
Let me know what you think.
If most of you are interested, I will write an article on this topic pretty soon!
👉 Over to you: What are some other cool Python project development tips you are aware of?
👉 If you liked this post, don’t forget to leave a like ❤️. It helps more people discover this newsletter on Substack and tells me that you appreciate reading these daily insights. The button is located towards the bottom of this email.
Thanks for reading!
Latest full articles
If you’re not a full subscriber, here’s what you missed last month:
Formulating and Implementing the t-SNE Algorithm From Scratch.
Generalized Linear Models (GLMs): The Supercharged Linear Regression.
Gaussian Mixture Models (GMMs): The Flexible Twin of KMeans.
Where Did The Assumptions of Linear Regression Originate From?
To receive all full articles and support the Daily Dose of Data Science, consider subscribing:
👉 Tell the world what makes this newsletter special for you by leaving a review here :)
👉 If you love reading this newsletter, feel free to share it with friends!
Pro tip: import all your relevant functions on the top directory __init__.py then you can do “from package import *” in your scripts for easier access
Insightful