![]() |
One of the features of Python is that it allows users to organize their code into modules and packages, which are collections of modules. The __init__.py file is a Python file that is executed when a package is imported. In this article, we will see what is __init__.py file in Python and how it is used in Python. What Is __Init__.Py File in Python?The __init__.py file is a Python file that is executed when a package is imported.
Syntax of Importing and Using __Init__.py FileTo import a package or module, use the ‘import‘ keyword followed by the package or module name. For instance, importing module1 from the ‘package’ package is done with: import mypackage.module1
Alternatively, use ‘from’ followed by the package/module name, and ‘import’ for specific functions, classes, or variables. Example: from mypackage.module1 import func1
Avoid using the ‘*’ operator to import all names from a package/module, as it may lead to conflicts and reduce code readability. For instance: from mypackage import *
Note that this method only imports names defined in the init.py file, excluding submodules. To include submodules, use all variables in init.py to specify the modules or names to import. Usage of __Init__.py File in PythonBelow are some of the example of __Init__.Py File uses in Python:
Creating a Simple Package Using __init__.py FileCreate a directory named ‘mypackage’ with two modules inside: ‘module1.py’ containing ‘func1’ and ‘module2.py’ containing ‘func2’. To make it a Python package, include an empty ‘init.py’ file in the ‘mypackage’ directory. The File structure should appear as follows: File Structure __init__.py :In below code, the __all__ variable is a list of strings that indicate the names that should be imported when using the * operator. The dot (.) before the module names means that they are relative imports, which means that they are imported from the same package.
module1.py : Define a function named “func1” in Python, which prints “This is func1 from module1” when called.
module2.py : Define a function named “func2” in Python, which prints “This is func2 from module2” when called.
main.py : In below code, a package named “mypackage” is imported, and then two specific modules, “module1” and “module2,” within that package are imported. Finally, functions from each module, namely “func1” from “module1” and “func2” from “module2,” are called.
Output This is func1 from module1
This is func2 from module2
Package Import Optimization Using __init__.py FileImagine we intend to utilize the from package import * syntax to import all modules or names from our package. For instance, we aim to include the following code in main.py: main.py : In the
__init__.py : Below, code defines the __all__ variable to specify which modules or names should be imported when using the wildcard (*) import. Then, it imports the specified submodules (module1 and module2) within the current package using relative import syntax.
Output This is func1 from module1
This is func2 from module2
Define Variable and Execute Code in __init__.py fileIf there is a need to initialize variables or execute code when a Python package is imported, this can be accomplished in the __init__.py file. For instance, to define a version variable holding the package version and display a welcome message upon import, the following code can be included in the __init__.py file. __intit__.py : In below code , a variable named “version” is defined with the value “1.0.0,” and a welcome message including the package version is printed.
main.py : In below code, the “mypackage” is imported, and the version variable from the package is printed, displaying the version information of the package.
Output Welcome to mypackage version 1.0.0
1.0.0
|
Reffered: https://www.geeksforgeeks.org
Python |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 16 |