Daily Dose of Data Science

Share this post

How to Simplify Python Imports with Explicit Packaging?

www.blog.dailydoseofds.com

Discover more from Daily Dose of Data Science

High-quality insights on Data Science and Python, along with best practices — shared daily. Get a 550+ Page Data Science PDF Guide and 450+ Practice Questions Notebook, FREE.
Over 36,000 subscribers
Continue reading
Sign in

How to Simplify Python Imports with Explicit Packaging?

A step towards better Python project development.

Avi Chawla
Sep 16, 2023
28
Share this post

How to Simplify Python Imports with Explicit Packaging?

www.blog.dailydoseofds.com
2
Share

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 a Training class.

  • test.py has a Testing 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:

Loading...

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.

  • Bayesian Optimization for Hyperparameter Tuning.

  • Formulating the PCA Algorithm From Scratch.

  • Where Did The Assumptions of Linear Regression Originate From?

To receive all full articles and support the Daily Dose of Data Science, consider subscribing:

I want to read full articles.


👉 Tell the world what makes this newsletter special for you by leaving a review here :)

Review Daily Dose of Data Science

👉 If you love reading this newsletter, feel free to share it with friends!

Share Daily Dose of Data Science

28
Share this post

How to Simplify Python Imports with Explicit Packaging?

www.blog.dailydoseofds.com
2
Share
Previous
Next
2 Comments
Share this discussion

How to Simplify Python Imports with Explicit Packaging?

www.blog.dailydoseofds.com
Silvermine AI
Writes Embracing AI/ML/DS
Sep 16

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

Expand full comment
Reply
Share
Nitish Yadav
Sep 16

Insightful

Expand full comment
Reply
Share
Top
New
Community

No posts

Ready for more?

© 2023 Avi Chawla
Privacy ∙ Terms ∙ Collection notice
Start WritingGet the app
Substack is the home for great writing