Modules, pip and Virtual Environments in Python

Python for Beginners: Learn to Code with Real Projects · Functions, Text and Files · 5 min read · Updated

Packages and environments

  1. PyPI is the shared library of Python packages written by other people.
  2. The pip command downloads a package and its helpers.
  3. With a virtual environment, the packages are installed in a private folder for this project only.
  4. Your code can now import the package. A requirements file records the versions for others.

A program grows beyond one file quickly. A module is just a Python file whose code you can use from another file. Python also comes with a large library of ready-made modules, and a huge community shares many more that you can install. In this lesson you learn to import modules, write your own, install packages with pip and keep each project's packages separate with a virtual environment.

Importing

Use import to load a module. Python's own standard library has modules for maths, dates, files, JSON, CSV, statistics, random numbers, regular expressions, command-line tools and much more, and none of them needs installing. There are three common forms: import math (then write math.sqrt(9)), from math import sqrt (then write sqrt(9)) and import numpy as np, which gives a short alias.

Your own module

Any .py file is a module. Make a file shop_utils.py with small helper functions, and import them from another file in the same folder.

"""Small helper functions we can import from other files."""

TAX_RATE = 0.18

def with_tax(amount):
    return round(amount * (1 + TAX_RATE), 2)

def money(amount):
    return f"Rs {amount:,.2f}"

if __name__ == "__main__":
    # Runs only when you start this file directly, not when it is imported
    print("Testing:", money(with_tax(1000)))
import math
import statistics
from shop_utils import with_tax, money

print(math.sqrt(144), math.ceil(2.1), round(math.pi, 4))
print(statistics.mean([10, 20, 60]), statistics.median([10, 20, 60]))
print(money(with_tax(1000)))
print(__name__)
12.0 3 3.1416
30 20
Rs 1,180.00
__main__
  • The import of with_tax and money works because both files are in the same folder. The name of the module is the file name without .py.
  • math.ceil(2.1) rounds up to 3, and round(math.pi, 4) gives 3.1416.
  • statistics.mean and statistics.median are in the standard library. The mean of 10, 20 and 60 is 30, and the median is 20.
  • The last line prints __main__. Every module has a __name__. When you run a file directly, its name is __main__. When another file imports it, the name is the module name instead.

The if __name__ block

The bottom of shop_utils.py has if __name__ == "__main__":. The code in it runs only when you start that file directly with python shop_utils.py, and not when another file imports it. That lets one file serve as both a module and a small program, and it is very handy for quick tests of your functions. When you run it directly, it prints Testing: Rs 1,180.00.

Packages from the internet with pip

pip is Python's installer. It downloads packages from PyPI, the Python Package Index. For example, requests is a popular package for talking to web services, pandas is for tables of data, and openpyxl reads and writes Excel files. You install one with pip install name. Packages are written by other people. Choose well-known ones, check the name carefully for typos, and do not install packages you do not need.

Virtual environments

If you install every package into one global place, different projects start to fight over versions. One project needs version 1 of a package and another needs version 2. A virtual environment is a private folder with its own copy of Python's packages for one project. You create one for each project. These are the commands. The activate line is different on each system.

python -m venv .venv

# activate it
#   Windows PowerShell:  .venv\Scripts\Activate.ps1
#   Windows cmd:         .venv\Scripts\activate.bat
#   macOS / Linux:       source .venv/bin/activate

pip install requests
pip freeze > requirements.txt
  • After you activate it, your terminal usually shows (.venv) at the start of the line. Now pip and python use the private copy.
  • pip freeze > requirements.txt saves the list of installed packages and their versions. Anyone can rebuild the same set with pip install -r requirements.txt.
  • Do not commit the .venv folder to git. Add .venv to .gitignore and commit requirements.txt instead.
  • To leave the environment, type deactivate.
  • On Windows PowerShell, you may see an error about running scripts. It is a security setting. Search for the exact message, or use activate.bat in the normal command prompt.

We ran python -m venv, installed requests with pip and used pip freeze to check the commands above. The package versions in the list will differ on your computer because newer versions appear all the time, and that is exactly why a requirements.txt file is useful.

Naming trouble

  • Never name your own file the same as a module you import, such as random.py or math.py. Python would import your file instead of the real module, and you get strange errors.
  • Use lowercase names with underscores for module names.
  • If import something says ModuleNotFoundError, the package is not installed in the environment you are using. Check that the environment is active, and run pip install something.

Your task

  1. Create shop_utils.py and main.py. Import two functions from the first and use them in the second.
  2. Add the if __name__ == "__main__": test block and run the helper file directly.
  3. Create a virtual environment named .venv, activate it and run pip --version to see that it points inside .venv.
  4. Install requests with pip, run pip freeze > requirements.txt and open the file.

How to check it worked

  • Running main.py prints your helper functions' results.
  • Running the helper file directly shows the test line, but importing it does not.
  • Your terminal shows (.venv) and requirements.txt exists.

Frequently asked questions

Why use a virtual environment?

It keeps each project packages separate, so different projects can use different versions without clashing, and your system Python stays clean.

What does if __name__ == "__main__" do?

It runs the code below it only when the file is started directly, and not when another file imports it.