Python Modules: 1. Files and Packages
Python Modules: 1. Files and Packages
-Narendra Allam
1. python files
2. packages
if we want to reuse this file in any other python file, we can simply import the file as module using import
statement. To reuse the ‘fact’ function, we just have to import file name without ‘.py’ extension as below.
Plot No. 28, 4th Floor, Suraj Trade Center, Opp. Cyber Towers, Hitech City, Hyderabad - 500081,
Tel: 040 - 66828899, Mob:+91 7842828899, Email: [email protected]
Another way of importing
using from <module>import func1, func2, ... we can import multiple functions from a module.
Package:
Package is folder in the python project folder structure, which is having __init__.py. This is the difference between a
folder and a package in python. lets create a folder Utils in the Sample Project.
Let’s place mathfuncs.py inside Utils folder. Now, if we try to import fact in main.py, we see above error ‘No
module named Utils’. Because Utils is just a folder, not a package, only package or a python file is importable. we
need to create __init__.py inside Utils folder to make it as a package and importable. Packages have namespaces.
Name space is the place where all the symbols(class names, function names, variable names; in fact, all identifiers)
can be found. E.g., all the functions are available, if we have module. ’fact’ is available under ‘mathfuncs’ module.
That means, if we have ‘mathfuncs’, ‘fact’ is also available. A symbol table is maintained to each module to group all
the names under the module name.
Plot No. 28, 4th Floor, Suraj Trade Center, Opp. Cyber Towers, Hitech City, Hyderabad - 500081,
Tel: 040 - 66828899, Mob:+91 7842828899, Email: [email protected]
We did not get the error this time as, Utils has been converted to a package.
__init__.py is just an empty file, which makes the folder as a package. But there are other uses too.
Shapes is another package with two files, cube.py and triangle.py. volume() and area() are the functions inside
those files respectively. Now, how do we access volume()and area()from main.py.
we have to use the long path name. Some developers do not want to expose the intermediate names, like Shapes,
cube, triangle etc. What if, we could access all the functions directly from Utils namespace.
If we export fact() to Utils name space we can directly access fact from Utils as below.
Plot No. 28, 4th Floor, Suraj Trade Center, Opp. Cyber Towers, Hitech City, Hyderabad - 500081,
Tel: 040 - 66828899, Mob:+91 7842828899, Email: [email protected]
This is where we need __init__.py and __all__ built-in variable.
First, we have to import all symbols to __init__.py then add those symbols to __all__.
Now all those symbols in __all__ are available directly in Utils. To export all functions from Shapes to Utils
namespace we have to make changes in both __init__.py files, one is in Shapes and another one is in Utils.
__init__.py file acts as a bridge to export symbols to next higher levels, this reduces so much complexity when there
are complex project structures. Let’s make changes to Shapes/__init__.py.
From now, volume, and area are available directly in Shapes namespace. Let’s import them from Shapes and export
to Utils namespace.
If we observe, we are actually exporting symbols from leaf level to root level in a project structure, by just
connecting each level with __init__.py and __all__. Now, we can directly import all the functions from Utils as
below.
Plot No. 28, 4th Floor, Suraj Trade Center, Opp. Cyber Towers, Hitech City, Hyderabad - 500081,
Tel: 040 - 66828899, Mob:+91 7842828899, Email: [email protected]
We can also import all symbols at once as below.
In the below example. I have developed a function fact()and tested it in the same file.
Now I want to reuse the same function fact() in another module called entry.py. I imported fact into entry.py and
executed some code.
Plot No. 28, 4th Floor, Suraj Trade Center, Opp. Cyber Towers, Hitech City, Hyderabad - 500081,
Tel: 040 - 66828899, Mob:+91 7842828899, Email: [email protected]
Why we are seeing unwanted output if we want to execute entry.py?
Because, all the statements in a module are executed, when module is loading first time.
When we are importing fact from Utils, all the statements (test code) are executed once.
__name __:Within a module, the module’s name (as a string) is available as the value of the global variable.
All the global statements should be conditionally executed using __name__, unless it is really required.
Plot No. 28, 4th Floor, Suraj Trade Center, Opp. Cyber Towers, Hitech City, Hyderabad - 500081,
Tel: 040 - 66828899, Mob:+91 7842828899, Email: [email protected]
global variable, __name__’s value is ‘__main__’ in the start-up module of every project. In all other modules
__name__ value is set to its module name. Now if we execute entry.py we get do not get the unwanted output.
Let’s run entry.py and print __name__ value in both the modules. Check the output.
Its is a good practice to keep all the global statements, which do not belong to any functions or classes, inside
Plot No. 28, 4th Floor, Suraj Trade Center, Opp. Cyber Towers, Hitech City, Hyderabad - 500081,
Tel: 040 - 66828899, Mob:+91 7842828899, Email: [email protected]