What are Packages and Virtual Environments?
Chapter 1: What are Packages and Virtual Environments?
Welcome to Lesson 10! In the previous lessons, you learned how to write Python code, organize it with functions and classes, and handle errors. Now, as you start building real projects—especially our upcoming Tasks API—you will need to use code written by other developers. This is where packages and virtual environments become essential.
Think of a package as a toolbox full of ready-made tools. Instead of writing everything from scratch, you can install a package (like FastAPI) and use its tools immediately. A virtual environment is like a separate workshop for each project. It keeps the tools for one project isolated from another, preventing conflicts.
Why Do We Need Packages?
Python comes with a standard library—a set of built-in modules for common tasks. But for specialized tasks like building a web API, you need external packages. For example, FastAPI is a package that makes it easy to create APIs. Without packages, you would have to write hundreds of lines of code to handle HTTP requests, data validation, and documentation.
Why Do We Need Virtual Environments?
Imagine you are working on two projects: Project A needs FastAPI version 0.100, and Project B needs version 0.95. If you install packages globally, you can only have one version. A virtual environment creates an isolated space for each project, so you can install different versions without conflicts. It also keeps your global Python installation clean.
How to Create and Use a Virtual Environment
Let's go step by step. We will use the built-in venv module, which comes with Python 3.3 and later.
Step 1: Open your terminal or command prompt.
Step 2: Navigate to your project folder. For example:
cd my_project
Step 3: Create a virtual environment. Run:
python -m venv venv
This creates a folder named venv inside your project. It contains a fresh copy of Python and the pip package manager.
Step 4: Activate the virtual environment.
- On Windows (Command Prompt):
venv\Scripts\activate - On Windows (PowerShell):
venv\Scripts\Activate.ps1 - On macOS/Linux:
source venv/bin/activate
After activation, you will see (venv) at the beginning of your terminal prompt. This means you are now inside the isolated environment.
Step 5: Install a package. For example, let's install the requests package, which is used to make HTTP requests:
pip install requests
Now, requests is available only inside this virtual environment.
Step 6: Deactivate the environment. When you are done, simply run:
deactivate
The (venv) prefix will disappear, and you are back to the global Python environment.
Practical Code Example: Using a Package
Let's write a small script that uses the requests package to fetch data from a public API. First, make sure your virtual environment is activated and requests is installed.
Create a file named fetch_data.py with the following code:
import requests
# Make a GET request to a public API
response = requests.get("https://api.github.com")
# Check if the request was successful
if response.status_code == 200:
print("Success! Here is some data:")
# Print the first 200 characters of the response
print(response.text[:200])
else:
print(f"Failed with status code: {response.status_code}")
Run the script:
python fetch_data.py
You should see a JSON response from GitHub's API. This is a simple example of how packages save you time—you didn't have to write the HTTP logic yourself.
Managing Dependencies with requirements.txt
When you share your project, you need to tell others which packages are required. The standard way is to create a requirements.txt file. To generate it, run:
pip freeze > requirements.txt
This file lists all installed packages and their versions. For example:
requests==2.31.0
Another developer can then install all dependencies by running:
pip install -r requirements.txt
Common Mistake: Forgetting to activate the virtual environment before installing packages. If you install packages globally, they will not be available inside your project's environment, and you may get "ModuleNotFoundError" when running your code.
Common Errors and How to Fix Them
- "pip is not recognized": This usually means Python is not in your system PATH. Reinstall Python and check "Add Python to PATH" during installation.
- "ModuleNotFoundError: No module named 'requests'": You either forgot to install the package or you are not inside the virtual environment. Activate the environment and run
pip install requests. - "Permission denied" when installing packages: This happens when you try to install globally without admin rights. Always use a virtual environment to avoid this.
Practice Task
Now it's your turn! Follow these steps:
- Create a new folder called
practice_lesson10. - Open a terminal in that folder and create a virtual environment named
env. - Activate the virtual environment.
- Install the package
numpy(a popular package for numerical computations). - Create a Python script that imports
numpyand prints its version (hint: usenumpy.__version__). - Run the script to confirm it works.
- Generate a
requirements.txtfile. - Deactivate the environment.
If you can do this, you have mastered the basics of managing packages and virtual environments. In the next lesson, we will use this knowledge to install FastAPI and build our first API endpoint!

Loading ratings...