Skip to main content
Practice

What Are Packages and How to Use Them?

In Python, a Package is a directory that contains multiple related modules.

A package includes several Python files (modules).

Packages help to systematically organize and manage related modules, enhancing the reusability of code.


Python Package Structure

A Python package is typically composed of a directory containing an __init__.py file.

The __init__.py file indicates that the directory is a package, and it can contain initialization code that runs when the package is imported.

Example: Simple Package Structure
my_package/
__init__.py
module1.py
module2.py

In the above example, the my_package directory is a Python package containing two modules: module1.py and module2.py.

You can then import the modules from the package in other Python code as shown below.

Using Modules in a Package
# Importing module1 from the my_package package
from my_package import module1

# Calling some_function defined in module1.py
result = module1.some_function()

Using packages in this way helps to organize and reuse your code more effectively.


Maximizing Code Reusability: The Structure

In Python, the structure to maximize code reusability progresses from functions > modules > packages. Functions are the smallest units, while packages are the largest.

Note: A package is a structural unit that can contain multiple modules, whereas a library is a functional unit that provides a collection of code for specific purposes. A library can consist of one or more packages but does not necessarily follow the package structure.


Managing Python Packages with pip

pip is a tool for installing and managing Python packages.

pip can be used in the terminal (a text-based interface for interacting with your computer) to install and use numerous publicly available packages provided by the Python Package Index (PyPI).

For example, to install the requests package, which allows Python programs to communicate with external websites and servers, open the terminal and run the following command.

Installing the requests Package
pip install requests

Running this command installs the requests package in your Python environment on your computer.

You can then use the package in your project by importing it with import requests.

Using the requests Package
# Importing the requests package
import requests

# Using the requests package to get data from a website
response = requests.get("https://www.example.com")

# Printing the HTML code of the website
print(response.text)

While importing generally works within the same directory, packages installed via pip can be imported from anywhere because they are stored and managed in the Python installation directory.


Differences between from and import

  • import keyword: Imports the entire package or module

  • from keyword: Imports specific modules from a package or specific functions from a module

For example, import requests imports the entire requests package, while from requests import get imports only the get function from the requests package.

Although requests is a package, it includes an __init__.py file, allowing it to be treated like a single module.

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.