Modules and import in Python

Sometimes you may think that “how do I import some code I wrote in one file into another”?. You don’t want to write the same block of codes or redefine in your file.

I this tutorial we talked about it.


Contents

  1. What is Module
  2. Import
  3. Import with alias
  4. from with import
  5. Difference between from import and import

 


1. What is Module

A module is a file that containing definitions of variables, functions, and classes. The file name is the module name with the suffix .py

Create a new file and name it calculation.py

# Even or Odd Number
def evenorodd(num):
    if num%2 == 0:
        print("Even Number")
    else:
        print("Odd Number")


# Prime Number 
def prime(num):
    count = 2
    while count<=num:
        if num%count == 0:
            break
        count+=1

    if count == num:
        print("Prime Number")
    else:
        print("Not Prime Number")

Above we create a Python file in which we define two functions. First, one check number is even or odd and Second one is check number is prime or not.


2. Import

import statement is used to import python module in another module. For importing more than one module

Syntax –

import module1[,module2,module3,.....,moduleN]

Example –

import calculation

calculation.evenorodd(2)
calculation.prime(9)

For accessing the Module variable, function, etc. use module name then put a period (.) symbol.

When we execute the above program, it will produce the following output –

Even Number
Not Prime Number

3. Import with alias

We can make module name little shorter while importing module. It will reference to the importing module. For this, we use as a keyword for defining alias name of the module.

After defining alias name of the module, now we can use this alias name for accessing module contents.

Syntax –

import module1 as Alias

Example –

import calculation as calc

calc.evenorodd(2)
calc.prime(9)

Now for accessing the module contents we don’t need to specify module name instead of this use alias name.

When we execute the above program, it will produce the following output –

Even Number
Not Prime Number

We can also use a variable for this purpose. Declare a variable and assign module name to it.

Example –

import calculation
calc = calculation
calc.evenorodd(2)
calc.prime(9)

Now for accessing the module contents we use variable name.

When we execute the above program, it will produce the following output –

Even Number
Not Prime Number

4. from … import

from ... import allows us to import specific attribute from the module. For importing all attributes use asterisk (*) symbol.

Syntax –

from modulename import attribute1[,attribute2,attribute3,....,attribute4]

For importing all attributes

from modulename import *

Example –

from calculation import evenorodd,prime

evenorodd(2)
prime(9)

As in import where we need to specify module name with a period (.) symbol for accessing module content but in from ... import we don’t need to specify this. We can now directly access it.

When we execute the above program, it will produce the following output –

Even Number
Not Prime Number

5. Difference between from import and import

Let’s create two new modules –

First module name is module1.py

def addOne(num):
    num = num + 1
    print("number : ",num)

def display(num):
    print("number : ",num);

The second module name is module2.py

def display(num):
    num = num + 5
    print("number : ",num);

import module1

This brings the name “module1” into your module.

You cannot access module content directly while using import you need to use module name with period (.) symbol.

module1.display(11)

from module1 import *

This does NOT bring the name “module1” into your module,instead it brings all of the contents inside module1 into your module. Now you can directly access them:

display(11)

So why not we directly use from ... import instead of import.

The reason is that the second form will overwrite any names that you have already declared.

When you have imported more than one module using from .. import then there may be the same name in them, for example:

from module1 import *
from module2 import *

display(11)   # which open gets called, module1.open or module2.open or open?

Can you see how it gets confusing?

So best way to overcome this is –

Use import instead of from … import

import foo

or when using from … import type names

from module1 import addOne

Related Post

Spread the love

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *