Skip main navigation

Module and Package : What are they?

Explore the modules and packages that make up the Python library.
2.7
When you work with large amounts of Python code, it can quickly get overwhelming. How do you keep track of everything you have written? Luckily, Python has some features to help you keep your work well-organized. Let’s look at modules and packages in Python. A module allows you to logically organize your Python code. By grouping related code into a module, you make your code easier to read, understand, and use. Simply speaking, a module is a Python file that contains Python code. In this file, you can define classes, functions, and variables, and even include runnable code.
42
You would usually call a module from within another Python file when you need to use the code written in the module file, but you can also import just a selection of functions from a module if that’s all you need. For example, the DateTime module contains five classes of functions. Depending on your needs, you could call the entire module into your Python file, or you can import just the tzinfo class if you just need time zone functions for your code. A module is a single file, but if you are creating a large complex application in Python, you would probably end up with many modules and other files. You would need some way to organize all these files in a useful structure.
86
This is exactly what packages are used for. In simple terms, a package is a hierarchical file directory structure that defines a single Python application or library. It consists of various sub-packages and modules. For example, NumPy is a useful core package for numerical computing, and Pandas is another core package that provides spreadsheet functionality. Here is a screenshot that shows the directory structure and various module files in the .py format. These are available in one of the applications source code on GitHub for reference purposes. With the individual .py file, the Python code is arranged consisting of variables, logics, functions, et cetera. Packages like these are essential for activities such as data analytics.
138.9
These packages are also open source, which means you are free to use and modify them. Look online for more open-source modules and packages to help you in your Python projects.

Watch the video to understand how Python has some useful features for keeping your code well organised. This video will give you an overview of modules and packages.

Efficiency is an important objective in writing code. You might not notice the difference in small projects, but as you create larger programs and work with larger data sets, you will see that efficient, well-structured code results in better program performance. Using various features of modules and packages, you can most certainly try to achieve that.

This table below summarises the differences between modules and packages:

Module Package
A module is a file of Python code. A package is a hierarchical file directory structure.
It can define classes, functions, and variables. It defines a single Python application, or library.
It can include runnable code. It consists of various sub-packages and modules.
Example use case: Write a set of functions once and reuse them in many different projects. Example use case: Organise a collection of modules together for accomplishing a particular activity, such as data analytics.
This article is from the free online

Python Packages: NumPy and Pandas Dataframe

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now