Modules and Packages in Python

Module basics

While chipping away at basic models you most likely type your code directly into the interpreter. Be that as it may, each time you quit from the interpreter and start it again you lose all the definitions you made previously. So as you begin composing bigger projects it bodes well to set up your code ahead of time utilizing a text editor and afterward run it with the interpreter. A document containing a list of operations that further are read and interpreted is called script.

You additionally might need to keep in touch with certain functions and afterward use them in different projects or even reuse code another person composed previously. One way is simply to duplicate the code into your program, however it soon leads to code that is bad-structured and hard to read. Fortunately, there is another way in Python to organize and reuse code called modules.

The module is just a file that contains Python statements and definitions. It for the most part has a .py extension. What truly makes the module system powerful is the capacity to load or import one module from another.

Create a Module in python

Python module contains statements and definitions and has a .py extension i.e. to create a module just save the code you want in a file with the file extension .py:

class Person:
    age = 50
    def hello(self):

To create a module of the above code just save it in a file named

Note: You can use any filename of your choice but it should have a .py extension.

Module loading in python

To load a module just use an import statement. In a basic form, it has the following syntax import module.

import super_module

super_module is the name of the module you need to import. For instance, a file called has a name super_module. So as to be accessible for import, should be situated in a similar directory as the file you are attempting to import it from. At first, Python importing system searches for a module in the current directory, at that point it checks the built-in modules, and if nothing is found an error will be raised. After importing, the module is now available under its name and you can access functions and variables characterized in it using the dot(.) notation.

It’s likewise normal to just import required functions or variables from a module yet not the module itself. You can do this by utilizing a form of the import statement.

from super_module import super_function

A good practice is to load a single module in a single line and put all your imports at the top of the file because it increases readability.

import mymodule1
import mymodule2
import mymodule3

A special type of import statement permits you to load all the names defined in a module. It is called wildcard import and has syntax from module import *. You should generally keep away from this in your code. It can cause unexpected behavior since you don’t have the idea what names precisely are imported into the current namespace. Plus, these names may shadow a portion of the existing ones without your knowledge. It’s smarter to make it explicit and indicate what you’re importing.

In the chance you need to utilize a few import statement, focus on their order:

  • standard library imports
  • third party dependency imports
  • local application imports

Having your imports grouped, you may put a blank line between import areas. Additionally, a few rules, suggest arranging imports one after another in alphabetical order is a good practice.

Variables in Modules

The module can contain functions, but also variables of all types (arrays, dictionaries, objects etc):

person1 = {
  "name": "Rohit",
  "semester": 3,
  "gender": "Male"

Save the above code in the file (or you can use your own module name).

Naming and Renaming a Module

You can name the module file whatever you like, but it must have the file extension .py. You can create an alias when you import a module, by using the keyword when you want to rename your module. So instead of always using your module name, you can use the alias name:

import person as pr

a = pr.person1["name"]

Suppose you do not save your file with the correct extension(.py) or you import your module without saving it. It will raise a ModuleNotFoundError:

Built-in Modules

Python comes with a great standard library. It contains a lot of built-in modules that provide useful functions and data structures. Another advantage is that the standard library is available on every system that has Python installed. You can find an official library reference

Python has a math module that provides access to mathematical functions.

import math


string module contains common string operations and constants.

from string import digits


A random module provides functions that let you make a random choice.

from random import choice
print(choice(['one', 'two', 'thirty']))



At the point when our code becomes bigger, it turns out to be difficult to maintain and keep track of all the modules included. So as to make the code progressively organized, we can restore to packages. A package is a method of organizing modules hierarchically with the help of the so-called “dotted module names”. Thus the module name sun.moon assigns a submodule named “moon” in a package named “sun”.

The possible structure may be the:


Note: it’s necessary to create files, that will make Python treat the directory as a package/subpackage. They can be empty or execute the initialization code for the package.

Importing and referencing packages

There are two ways to import the “artificial” submodule from the subpackage:

from package.subpackage import artificial
  • This method allows to use the submodule content without naming the package and subpackage:
artificial.function(arg1, arg2)
  • The second method is more straightforward:
import package.subpackage.artificial

After we’ve loaded the submodule in such a way, its content should be referenced with its full name:

package.subpackage.artificial.function(arg1, arg2)

Import * from

You can likewise use it from package.subpackage import *. This code will import all the submodules that your subpackage has, in spite of the fact that you may not so much need that. Besides, it will be truly tedious and viewed as a bad practice. How might we deal with these?

The significant thing to do is to provide the package a specific file with the assistance of __all__ statements that ought to be embedded into file. There you need to list the submodules to be imported while from package import * operation is executed.

__all__ = ["submodule1", "submodule3"]

Intra package references

Python is significantly more remarkable than you could imagine: if you have a need, you can refer to submodules of siblings packages. For example, if you utilize the package.subpackage1.artificial and there you need something from package.subpackage2.amazing, you can import it by from package.subpackage2 import amazing in the file.

You can likewise do the alleged “relative imports” that use leading dots to demonstrate the current and parent package included. In this way, for “amateurs” you can use the:

from . import artificial
from .. import subpackage2  
from ..subpackage2 import module

Hence we can conclude:

  • Using packages is a very good way to structure your code.
  • Packages make your project simpler to perceive. They allow reusing code more easily.
  • Different ways of importing have their own advantages and disadvantages. Remember one of the main rules of Python: readability counts!