Importing Packages in Python

Photo by Sascha Hormel on Pexels.com

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.

Image by Author

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.

 

Image by Author

Different ways to import packages in python.

  1. Using the Import statement

import calc.mathcalc.addition

import calc.mathcalc.addition→Imported calc.mathcalc.addition and bounded locally

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

 

Image by Author

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 as mc

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

 

Image by Author

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 as addition

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.

 

Image by Author

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 and calc.mathcalc.addition.fn is bounded as fn

We can access the function fn() directly . But other functions or variables in the addition module can’t be accessed.

 

Image Source: Author

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 and calc.mathcalc.addition.fn is bounded as fn , calc.mathcalc.addition.var is bounded as var

We can access the function fn() and variable var directly .

 

Image Source: Author
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):

Packages

path

import

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s