Lesson 13 of 30
Modules and Packages
Importing standard library modules, creating your own modules, and using pip.
What is a Module?
A module is simply a .py file containing Python definitions and statements. Python ships with a huge standard library of ready-to-use modules.
Importing Modules
import math
print(math.pi) # 3.141592653589793
print(math.sqrt(144)) # 12.0
from math import factorial, gcd
print(factorial(5)) # 120
print(gcd(48, 18)) # 6
Creating Your Own Module
Create a file mytools.py in the same folder:
# mytools.py
def celsius_to_fahrenheit(c):
return c * 9 / 5 + 32
PI = 3.14159
Then import it:
import mytools
print(mytools.celsius_to_fahrenheit(100)) # 212.0
Installing Third-Party Packages with pip
In the Visual Studio 2026 Terminal (View → Terminal):
pip install requests
pip install pandas
pip install matplotlib
pip list # show installed packages
ℹ️ Python Environments
In Visual Studio 2026, each project can have its own virtual environment. Go to Python Environments in Solution Explorer to create, select, and manage them.