#### 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.additionand bounded

locally

locals() → It will return a dictionary representing the current local symbol table.

importcalc.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=1deffn(x,y):returnx+y

**Example:**

importcalc.mathcalc.addition print (calc.mathcalc.addition.fn(3,4))#Output:7print(calc.mathcalc.addition.var)#Output:1

**2. Using local name**

`import calc.mathcalc.addition as mc`

import calc.mathcalc.addition→Imported

calc.mathcalc.additionand bounded as

mc

This loads the submodule

. It must be referenced with its local name *calc.mathcalc.addition*`mc`

only.

importcalc.mathcalc.additionasmc 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:

importcalc.mathcalc.additionasmc print (mc.fn(3,4))#Output:7print (mc.var)#Output:1

**3. Using from clause**

`from calc.mathcalc import addition`

from calc.mathcalc import addition→Imported

calc.mathcalc.additionand 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`

fromcalc.mathcalcimportaddition print (locals())

In the local symbol table only `addition `

is udpated.

**Example:**

fromcalc.mathcalcimportaddition print (addition.fn(3,4))#Output:7print (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.additionand

calc.mathcalc.addition.fnis bounded as

fn

We can access the function `fn()`

directly . But other functions or variables in the addition module can’t be accessed.

**Example:**

fromcalc.mathcalc.additionimportfn print (locals())

In local symbol table, only` fn`

is updated.

fromcalc.mathcalc.additionimportfn print (fn(3,4))#Output:7print (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.additionand

calc.mathcalc.addition.fnis bounded as

fn,

calc.mathcalc.addition.varis bounded as

var

We can access the function `fn()`

and variable `var`

directly .

fromcalc.mathcalc.additionimportfn,var print (fn(3,4))#Output:7print (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