codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

Β© 2026 codingstuff.io. All rights reserved.

Built with ❀️ for developers everywhere

/
/
All Tutorials
🐍

Python Programming

40 / 68 topics
36List Comprehensions37Python Iterators38Python Generators39Python Decorators40Python Modules, Packages & PIP41Python Main Function (__name__ == '__main__')42Python Dates and Time43Python Regular Expressions44Python JSON
Tutorials/Python Programming/Python Modules, Packages & PIP
🐍Python Programming

Python Modules, Packages & PIP

Updated 2026-05-15
30 min read

Python Modules, Packages & PIP

In this tutorial, you'll learn about organizing your Python code into reusable modules and packages. We'll explore how to import these modules using various techniques, create your own packages, and manage dependencies using the pip package manager and requirements.txt files. Understanding these concepts is crucial for developing well-structured and maintainable Python applications.

Introduction

Python modules are files containing Python definitions and statements. They allow you to organize your code into separate files, making it easier to manage and reuse. Packages are collections of modules that can be distributed and shared. The pip package manager is a tool used to install and manage third-party packages in Python. Together, these tools help you build complex applications by leveraging existing libraries and organizing your own code.

Importing Modules

Basic Import

The simplest way to import a module is using the import statement. This imports the entire module into your namespace.

basic_import.py
1# basic_import.py
2import math
3
4result = math.sqrt(16)
5print(result) # Output: 4.0
Output
4.0

From...Import

You can import specific attributes from a module using the from ... import ... syntax. This is useful when you only need certain functions or classes.

from_import.py
1# from_import.py
2from math import sqrt, pi
3
4result = sqrt(16)
5print(result) # Output: 4.0
6
7print(pi) # Output: 3.141592653589793
Output
4.0
3.141592653589793

Importing with Aliases

You can use the as keyword to give a module or attribute an alias, which can make your code cleaner and more readable.

import_alias.py
1# import_alias.py
2import math as m
3
4result = m.sqrt(16)
5print(result) # Output: 4.0
6
7from datetime import datetime as dt
8
9current_time = dt.now()
10print(current_time) # Output: 2023-09-25 12:34:56.789012
Output
4.0
2023-09-25 12:34:56.789012

Creating Packages

A package is a way of structuring Python’s module namespace by using β€œdotted module names.” For example, the module name mypackage.mymodule would indicate that a submodule named mymodule is in a package named mypackage.

To create a package, you need to create an __init__.py file inside the directory. This file can be empty or contain initialization code for the package.

Example Package Structure

mypackage/ init.py module1.py module2.py

Using Modules from a Package

To use modules from a package, you can import them using dot notation.

use_package.py
1# use_package.py
2from mypackage import module1, module2
3
4module1.function1()
5module2.function2()

Installing Packages with PIP

The pip package manager is used to install and manage third-party Python packages. You can install a package using the pip install command.

Terminal
$ pip install requests
Collecting requests
Downloading requests-2.28.1-py3-none-any.whl (60 kB)
|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 60 kB 2.4 MB/s
Installing collected packages: requests
Successfully installed requests-2.28.1

Listing Installed Packages

You can list all installed packages using pip list.

Terminal
$ pip list
Package Version
---------- -------
requests 2.28.1
...

Managing Dependencies with requirements.txt

A requirements.txt file is used to specify the dependencies of a Python project. This makes it easy for others (or yourself) to install all required packages.

Creating a requirements.txt File

You can generate a requirements.txt file using pip freeze.

Terminal
$ pip freeze > requirements.txt

Installing from requirements.txt

To install all dependencies from a requirements.txt file, use the following command:

Terminal
$ pip install -r requirements.txt
Collecting requests==2.28.1
Downloading requests-2.28.1-py3-none-any.whl (60 kB)
|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 60 kB 2.4 MB/s
Installing collected packages: requests
Successfully installed requests-2.28.1

Practical Example

Let's create a simple package with two modules and demonstrate how to use them.

Package Structure

myapp/ init.py utils.py main.py

utils.py

Python
1# myapp/utils.py
2def add(a, b):
3 return a + b
4
5def subtract(a, b):
6 return a - b

main.py

Python
1# myapp/main.py
2from .utils import add, subtract
3
4result1 = add(5, 3)
5print(f"Addition: {result1}") # Output: Addition: 8
6
7result2 = subtract(5, 3)
8print(f"Subtraction: {result2}") # Output: Subtraction: 2

Running the Application

To run the application, navigate to the parent directory of myapp and execute:

Terminal
$ python -m myapp.main
Addition: 8
Subtraction: 2

Summary

  • Importing Modules: Use import, from ... import ..., and as for different importing techniques.
  • Creating Packages: Create a package by adding an __init__.py file to your directory.
  • Installing Packages with PIP: Use pip install to manage third-party packages.
  • Managing Dependencies with requirements.txt: Specify dependencies in a requirements.txt file and use pip freeze to generate it.

What's Next?

In the next tutorial, we'll explore how to structure your Python code using the if __name__ == '__main__': construct. This will help you write scripts that can be both imported as modules and executed as standalone programs. Stay tuned!


PreviousPython DecoratorsNext Python Main Function (__name__ == '__main__')

Recommended Gear

Python DecoratorsPython Main Function (__name__ == '__main__')