Python Module – What are modules and packages in python?


Any Python file with a .py extension is a Module in Python. A python package is a collection of such modules along with a file. Let’s understand how to work with modules and packages with clear examples.


When you work on python projects, it’s not a good practice to have all you python code in one single python file (.py).
You are better off splitting your code, classes, functions and variables thoughtfully in separate python files (.py files), aka modules. Python allows you to import code in one module for use in other modules.

This will:
1. Make your code modular, there by make the python objects reusable across modules.
2. Allows you to focus on a small part of problem at a time without disturbing the whole.
3. Makes bug fixing easier.
4. Allow multiple developers to contribute to your project effectively
5. Organize the code and maintain the project a lot more easier.

So what is a Package? and how is it different from a module?

What is a Module and Package?

A Python module is any Python files with a .py extension. It can be imported into python without the .py part.

A Python package is nothing but a collection of modules along with a file. The modules can also be arranged in hierarchy of folders inside a package.

Just by adding an empty file to the in the folder, Python knows it is a Package.

In fact, a package is also really a module that contains other modules.

Python provides a wide variety of modules as standard modules. You can find the full list here.

Pre-requisite to follow along the code below

To follow along the code, download this file and extract it. Then, open your project from it. That is in your terminal or command prompt, do cd folder_name and type jupyter notebook if you have anaconda installed. Or if you are using VSCode, Pycharm etc, open your project from this folder.

Once inside you can see python files and folders.

The python file can be imported in python as a module. In fact, the type such an imported object is module.

# Import the file as a `cars` module
import cars

#> module

Inside the python module (the .py file) you can define one or more class and import them.

Want to become awesome in ML?

Hi! I am Selva, and I am excited you are reading this!
You can now go from a complete beginner to a Data Science expert, with my end-to-end free Data Science training.
No shifting between multiple books and courses. Hop on to the most effective way to becoming the expert. (Includes downloadable notebooks, portfolio projects and exercises)

Start free with the first course 'Foundations of Machine Learning' - a well rounded orientation of what the field of ML is all about.

Enroll to the Foundations of ML Course (FREE)

Sold already? Start with the Complete ML Mastery Path

# Import a class from the file
from cars import Car

Initialize and start using.

# Start the car
car1 = Car(make="Toyota", model="Camry")

# Start driving

#> …VROOOOM….Started!



I’m driving at: 0 in gear: 0

Package Example

You can also import from the cars package. You will see the carspackage folder inside. Since this contains a, this is a python package.

Inside it also contains and

from carspackage import cars as carsp
from carspackage import suv as suvp

#> name:
#> I am outside the guard!


car2 = carsp.Car(make="Toyota", model="Camry")

#> Make Toyota, model: Camry

Start using..

# Start driving

#> …VROOOOM….Started!



#> I’m driving at: 5 in gear: 0

Now, let’s try running the SUV

suv1 = suvp.SUV(make="Honda", model="CRV")

#> Init success!!
#> Shift Up and Drive.
#> I am driving at 5 mph
#> I’m driving at: 5 in gear: 1


Purpose of

Just like how you call a python script in terminal python, you can call your package from command prompt / terminal as well via python {pacakge_name}.

But when called so, which module / code will be run?

That is the purpose of

When a package is called from terminal, the Python will look for executing the contents of file inside the package.

In practical uses, have a python package designed to do a spacific task, say convert a color image to b/w, you can build your scripts as a package and pass the path to image you to convert as an argument to python pkgname --image.png.

Let’s call carspackage from terminal / command prompt.

!python carspackage

#> name: cars
#> Let’s create a Toyota RAV4!
#> Make is Toyota, Model is RAV4

#> —————————

That simply executed the

You can make it receive arguments from the user as well.

Receiving command line arguments

What are command line arguments?

When you call a python program or a pacakge, you can pass additional input values based on which the output of your python program can change.

For example:

a. An email sending program may receive the ‘To address’ as an input
b. A program to process data can take the number of lines of data as input.

The simplest way to pass argument to your python script from command is using sys.argv()

Now, uncomment the sys.argv part inside __main.py__ and run the below code.

!python carspackage make="Toyota" model="RAV4

#> name: cars
#> Let’s create a Toyota RAV4!
#> Make is Toyota, Model is RAV4

#> —————————

A more sophisticated and convenient way of receiving and processing arguments is provided by the argparse package. This is part of python standard library and has been adopted by developers.

Packages with hierarchy

The carspackagedeep folder contains contains folders that contain the python files. So, it’s 1 additional level deep.

So, you need to point to that folder and then import the module.

from carspackagedeep.Car import cars

#> name:

Now, import suv as well.

from carspackagedeep.Suv import suv

#> I am outside the guard!

If you notice the contents of, it contains a __name__ == "__main__" statement. why?

What does __name__ == “__main__” do?

Whenever the Python interpreter reads a source file, it does two things:

  1. It sets a few special variables like __name__
  2. It executes all of the code found in the file.

When you import a python package or module, all the code present in the module is run.

So, when you run import mypackage, there is every chance that certain code present in mypackage you didn’t want to execute may get executed on import.

You can prevent this by checking the condition: __name__ == "__main__". It acts as a guard. The parts of your code that you don’t want to be run can be placed inside the __name__ == "__main__" condition block.

If the code is run when getting imported from another package, the value of __name__ will bear the path/name of the module. For example: the value of __name__ for ‘carspackage/’ when called from other places will be

Only when you are directly running python carspackage/, that is, only when you run the module as the main program, the value of __name__ will be __main__.

!python carspackage/

When run this way, all the code inside the guard will get executed.

Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science