
Exploring different ways to import packages in Python
Importing Packages in Python
Packages are a way of structuring Python’s module namespace by using “dotted module names”.
Refer to my story for importing modules in python.
For example, the module name A.B
designates a submodule named B
in a package named A
I have created the following packages and sub-packages. Let’s see how to import these packages.

I have created a package calc
I have created a sub-package mathcalc
I have created files addition.py,subtract.py under mathcalc
subpackage.
All packages and subpackages have init.py
.It can be just an empty file. The __init__.py
files are required to make Python treat directories containing the file as packages.

Different ways to import packages in python.
- Using the Import statement
import calc.mathcalc.addition
import calc.mathcalc.addition→Imported
calc.mathcalc.addition
and boundedlocally
locals() → It will return a dictionary representing the current local symbol table.
import calc.mathcalc.addition print (locals())

In the local symbol table only calc
name is updated.
import calc.mathcalc.addition
→This loads the submodule calc.mathcalc.addition. It must be referenced with its full name only.
The functions and variables inside “addition.py” are accessed like this.
calc.mathcalc.addition.fnname
calc.mathcalc.addition.varname

addition.py
var=1 def fn(x,y): return x+y
Example:
import calc.mathcalc.addition print (calc.mathcalc.addition.fn(3,4)) #Output:7 print(calc.mathcalc.addition.var) #Output:1
2. Using local name
import calc.mathcalc.addition as mc
import calc.mathcalc.addition→Imported
calc.mathcalc.addition
and bounded asmc
This loads the submodule calc.mathcalc.addition
. It must be referenced with its local name mc
only.
import calc.mathcalc.addition as mc print (locals())

In local symbol table only mc
is updated.
The functions and variables inside “addition.py”
are accessed like this.
mc.fnname
mc.varname

Example:
import calc.mathcalc.addition as mc print (mc.fn(3,4)) #Output:7 print (mc.var) #Output:1
3. Using from clause
from calc.mathcalc import addition
from calc.mathcalc import addition→Imported
calc.mathcalc.addition
and bounded asaddition
This also loads the submodule addition
and makes it available without its package prefix, so it can be used as follows.
The functions and variables inside “addition.py”
are accessed like this.
addition.fnname
addition.varname
from calc.mathcalc import addition print (locals())

In the local symbol table only addition
is udpated.

Example:
from calc.mathcalc import addition print (addition.fn(3,4)) #Output:7 print (addition.var) #Output: 1
4. To import the desired function or variable directly
from calc.mathcalc.addition import fn
It loads the submodule addition
, but this makes its function fn()
directly available.
from calc.mathcalc.addition import a→Imported
calc.mathcalc.addition
andcalc.mathcalc.addition.fn
is bounded asfn
We can access the function fn()
directly . But other functions or variables in the addition module can’t be accessed.

Example:
from calc.mathcalc.addition import fn print (locals())

In local symbol table, only fn
is updated.
from calc.mathcalc.addition import fn print (fn(3,4)) #Output:7 print (var) #Output: NameError: name 'var' is not defined
Accessing functions and variables directly by mentioning both in the import statement.
from calc.mathcalc.addition import fn,var
It loads the submodule addition
, but this makes its function fn()
and variable var
directly available.
from calc.mathcalc.addition import a→Imported
calc.mathcalc.addition
andcalc.mathcalc.addition.fn
is bounded asfn
,calc.mathcalc.addition.var
is bounded asvar
We can access the function fn()
and variable var
directly .

from calc.mathcalc.addition import fn,var print (fn(3,4)) #Output:7 print (var) #Output:1
Important points to remember:
from package import item
The item can be either a submodule (or subpackage) of the package or some other name defined in the package, like a function, class, or variable.
The import
statement first tests whether the item is defined in the package, if not, it assumes it is a module and attempts to load it. If it fails to find it, an ImportError
exception is raised.
import item.subitem.subsubitem
Each item except for the last must be a package.
The last item can be a module or a package but can’t be a class or function or variable defined in the previous item.
Resources(Python Documentation):
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
Or enter a custom amount
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.
Buy Me a CoffeeBuy Me a CoffeeBuy Me a Coffee