Skip to content
Laviyx Learn

Try

Python course menu

Python Packages and pip: Organise Code and Install Libraries

Learn Python packages: organise modules into folders with __init__.py, import from packages, install third-party libraries with pip, use virtual environments (venv), and record dependencies in requirements.txt.

Intermediate7 min readUpdated
On this page

The word package means two related things in Python:

  1. A folder of modules that you organise in your own project.
  2. A library published by someone else (on PyPI, the Python Package Index) that you install with pip.

This lesson covers both, plus virtual environments, which keep each project's libraries separate.

Part 1: Your own packages#

As a project grows, one folder full of .py files gets messy. A package groups related modules into a sub-folder:

Text
project/
├── main.py
└── shop/
    ├── __init__.py
    ├── pricing.py
    └── inventory.py
shop/pricing.py (Python)
def add_tax(price, rate=0.18):
    return round(price * (1 + rate), 2)
shop/inventory.py (Python)
STOCK = {"pen": 120, "notebook": 40}

def in_stock(item):
    return STOCK.get(item, 0) > 0
shop/__init__.py (Python)
from .pricing import add_tax

Import with dots, from main.py:

main.py (Python)
from shop import pricing
from shop.inventory import in_stock
import shop

print(pricing.add_tax(200))
print(in_stock("pen"), in_stock("chair"))
print(shop.add_tax(50))
Output
236.0
True False
59.0

What is init.py for?#

__init__.py marks the folder as a regular package and runs when the package is first imported. It can be empty. Here it re-exports add_tax, so users can write shop.add_tax without knowing it lives in pricing.py. The . in from .pricing import add_tax means "from this same package" (a relative import).

Part 2: Installing libraries with pip#

Thousands of free libraries are published on PyPI: requests for web requests, pandas for data analysis, flask for web apps. Install them with pip:

Terminal
python -m pip install requests

Useful pip commands:

CommandDoes
python -m pip install requestsinstall the latest version
python -m pip install "requests==2.32.5"install an exact version
python -m pip install --upgrade requestsupgrade
python -m pip uninstall requestsremove
python -m pip listlist installed packages
python -m pip show requestsdetails about one package

You can check whether a package is installed, and which version, from Python itself:

Python
import importlib.metadata as md

try:
    print(md.version("requests"))
except md.PackageNotFoundError:
    print("requests is not installed")
Output
2.32.5

Your version number will depend on what is installed on your computer.

Part 3: Virtual environments#

If one project needs requests 2.28 and another needs 2.32, installing both globally causes conflicts. A virtual environment is a private folder of packages for one project.

Terminal
# 1. Create it (once per project), inside the project folder
python -m venv .venv

# 2. Activate it
# Windows (PowerShell):
.venv\Scripts\Activate.ps1
# macOS / Linux:
source .venv/bin/activate

# 3. Install packages: they go into .venv only
python -m pip install requests

# 4. Leave it when done
deactivate

While it is active, your terminal prompt usually shows (.venv). Editors like VS Code detect .venv automatically.

requirements.txt#

Record a project's dependencies so anyone can recreate them:

Terminal
python -m pip freeze > requirements.txt

The file lists every package in the environment with its exact version, one per line, for example requests==2.32.5. It also includes the libraries requests itself depends on (such as urllib3 and certifi), because pip freeze records everything installed. This is another reason to work in a virtual environment: the file then contains only what this project uses.

Someone else (or you, on another computer) runs:

Terminal
python -m pip install -r requirements.txt

Common mistakes#

Practice#

Practice exercise

Create a package called textutils with a module cleaning.py containing a function normalize(text) that strips spaces and lowercases text. Make from textutils import normalize work, then call it on " Hello World ".

Show hint

You need textutils/__init__.py containing from .cleaning import normalize.

Show solution
Text
project/
├── main.py
└── textutils/
    ├── __init__.py
    └── cleaning.py
textutils/cleaning.py (Python)
def normalize(text):
    return text.strip().lower()
textutils/__init__.py (Python)
from .cleaning import normalize
main.py (Python)
from textutils import normalize

print(repr(normalize("  Hello World  ")))
Output
'hello world'

Key takeaway#

Finished this lesson?