Import Your Python Package as a Module
A python module is a single python file (.𝗽𝘆). An organized collection of such python files is called a python package.
While developing large projects, it is a good practice to define an __𝗶𝗻𝗶𝘁__.𝗽𝘆 file inside a package.
Consider 𝘁𝗿𝗮𝗶𝗻.𝗽𝘆 has a 𝗧𝗿𝗮𝗶𝗻𝗶𝗻𝗴 class and 𝘁𝗲𝘀𝘁.𝗽𝘆 has a 𝗧𝗲𝘀𝘁𝗶𝗻𝗴 class.
Without __𝗶𝗻𝗶𝘁__.𝗽𝘆, one has to explicitly import them from specific python files. As a result, it is redundant to write the two import statements.
With __𝗶𝗻𝗶𝘁__.𝗽𝘆, you can group python files into a single importable module. In other words, it provides a mechanism to treat the whole package as a python module.
This saves you from writing redundant import statements and makes your code cleaner in the calling script.
Read more in this blog: Blog Link.
Read this post on LinkedIn: Post Link.
Thanks for reading Daily Dose of Data Science! Subscribe for free to learn something new and insightful about Python and Data Science every day. Also, get a Free Data Science PDF (250+ pages) with 200+ tips.