SoFunction
Updated on 2024-11-18

Functional analysis of the __init__.py file in the Python module package

This article example describes the __init__.py file function in Python module package. Shared for your reference, as follows:

django development has been more than a year, but basically did not pay attention to the significance of the existence of python modules in the __init__.py file, stumbled upon the introduction of its startled, this file is too important!

Module packages:

A package is usually a directory, and the first file in that directory is __init__.py. Then there are module files and subdirectories, and if there is a subdirectory with __init__.py in it, then it's a subpackage of the package.

It's almost like this:

Package1/ __init__.py          Package2  
Package1/Package2/ __init__.py             

We can import a package just like that:

import Package1 

Or tune into a sub-module and sub-package:

from Package1 import Module1 
from Package1 import Package2 
       ,import Packag1.Module1 
       ,import Packag1.Package2 

It is possible to go several layers deeper into the package structure:

from Package1.Package2 import Module1 
           ,import Package1.Package2.Module1 

That's about it, I think, the essence of the package should be in the __init__.py file.

Readers interested in more Python related content can check out this site's topic: theSummary of Python image manipulation techniques》、《Python Data Structures and Algorithms Tutorial》、《Python Socket Programming Tips Summary》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniques》、《Python introductory and advanced classic tutorialsand theSummary of Python file and directory manipulation techniques

I hope that what I have said in this article will help you in Python programming.