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_taxandmoneyworks 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, andround(math.pi, 4)gives 3.1416.statistics.meanandstatistics.medianare 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. Nowpipandpythonuse the private copy. pip freeze > requirements.txtsaves the list of installed packages and their versions. Anyone can rebuild the same set withpip install -r requirements.txt.- Do not commit the
.venvfolder to git. Add.venvto.gitignoreand commitrequirements.txtinstead. - 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.batin 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.pyormath.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 somethingsaysModuleNotFoundError, the package is not installed in the environment you are using. Check that the environment is active, and runpip install something.
Your task
- Create
shop_utils.pyandmain.py. Import two functions from the first and use them in the second. - Add the
if __name__ == "__main__":test block and run the helper file directly. - Create a virtual environment named
.venv, activate it and runpip --versionto see that it points inside.venv. - Install
requestswith pip, runpip freeze > requirements.txtand open the file.
How to check it worked
- Running
main.pyprints your helper functions' results. - Running the helper file directly shows the test line, but importing it does not.
- Your terminal shows
(.venv)andrequirements.txtexists.