Python import

Filed Under: Python

Python import statement is used to import modules that we want to use in our program. Python modules are python scripts containing utility functions, types, classes etc. There are many modules that we use regularly in python programs such as sys, os, collections etc.

Python import

If we want to import python built-in modules or any third party module installed using a package manager such as PIP, then we can very easily import and use them in our program.

import collections
import sys

Python looks for modules and packages into sys.path property. This path always contains the current directory from where the script is executed, so any module in the current directory can be imported as is.

Python imports are case sensitive, so import sys and import Sys are looking for different modules to import.

Python looks for a module into the built-in modules first. If not found, then it searches for modules in the current directory. So if we have file in the same directory as our main script, then it will be loaded when import math is called if ‘math’ module is not in the built-in modules. You can get a list of built-in modules using sys.builtin_module_names. Below image shows the built-in modules in my python installation.

Python built-in modules list

Python import class/functions from module

We can import specific classes from module too. This way we can import specific parts of a module and use it. This also helps in writing fluent code. We can achieve this using from keyword with the import statement.

from collections import namedtuple
from os import path

Python import user-defined module

When we create a python script, we can import it into another python script using its name. Let’s say we have following directory structure with multiple python scripts.

python import

We have following functions defined in file.

def add(i, j):
    return int(i) + int(j)

def uppercase(s):
    return str(s).upper()

We can import it into and use its functions.

import utils




Python import as

We can define our own name for the imported module using import as statement.

# python import as
import utils as u


The result will be the same as the earlier program.

Python import from another directory

If the python script we are importing is in the same directory, then we can import is easily just like built-in modules. However, if the python script is present in another directory, then we can use importlib library to import them as a module.

Let’s say our has following functions:

def uppercase(s):
    return str(s).upper()

def lowercase(s):
    return str(s).lower()

Now let’s see how to use importlib to import this python script into our example.

# Refer:
# Refer:
import importlib, importlib.util

def module_from_file(module_name, file_path):
    spec = importlib.util.spec_from_file_location(module_name, file_path)
    module = importlib.util.module_from_spec(spec)
    return module

strutils = module_from_file("strutils", "../mymodule/")


Python import class from another file

We can import scripts and use the classes defined in them using importlib too. Let’s say we have classes Person and Student defined in file.

class Person:  
	name = ""  
	def __init__(self, personName): = personName  
	def showName(self):  

class Student(Person):
	id = 0
	def __init__(self, studentName, studentId):  
		Person.__init__(self, studentName) = studentId  
	def getId(self):  

Here is the example code where I am using the earlier defined function to import this script and use its classes.

#python import class from another file
mc = module_from_file("myclasses", "../mymodule/")

p = mc.Person('Pankaj')

s = mc.Student('David',25)

Notice that we can keep any name for the module we are importing, it’s similar to using import as statement.

There is another way to import scripts from another directory using the sys module.

import sys
from myclasses import Person as PC
p = PC('Meghna')

This is useful when we want to import only specific classes and functions from the imported file. Also, using this code is much easier to understand.


Python import statement allows us to import modules, python scripts, specific classes and functions from modules. It’s very easy to use and since most of the times we work on built-in modules or modules installed using PIP, we don’t need to write logic to load scripts from another directory.

You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: Official Documentation


  1. Stanford says:

    Maybe I’m off here, but this seemed more complicated than it needed to be considering we just want to import a python module and or its contents from whatever file (package) it may reside within the project. So I am speaking with that goal in mind.

    That’s a great explanation but in part, I wanted to address two things:
    1. The need for appropriate structuring of projects.
    2. The simplicity that comes with doing things correctly.

    So in this example, the project is structured well. All the scripts that we need to run things are within the same project and there are modules in their respective packages, great. That’s how it should be done by anyone so they write good code. Which leads to 2, the simple solution:
    To import a module from the same level as the script you are coding in,

    -> import [module]

    To import a module in another package as in your example:

    -> from .. import my_module.strututils

    Typically though:

    – > import package.module

    We use the dot method to access files that are at the same level.

  2. Isaac Quaye says:

    very educative but could not solve my problem

Leave a Reply

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

Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages