Python Packages & EnvironmentsGet Certified
Why Do We Need Packages and Environments?
The Python standard library is powerful, but it cannot cover every need. When you want to analyze data like a data scientist, create charts, build web apps, or work with machine learning, you install third-party packages. These packages are written by other developers and shared on the Python Package Index (PyPI).
Installing packages directly into your main Python installation can quickly become messy. Different school projects may need different versions of the same package. A virtual environment solves this problem by creating an isolated Python workspace for each project. Inside that workspace you can install exactly the packages you need without affecting other projects or your system Python.
This page teaches the practical workflow that high-school and college students use every day: create a virtual environment, activate it, install packages with pip, and freeze the list of packages so the project can be shared.
What Is a Virtual Environment?
A virtual environment is a self-contained folder that contains its own Python interpreter and its own set of installed packages. When the environment is active, any package you install goes only into that folder. When you deactivate it, your terminal returns to the system-wide Python.
Using virtual environments is considered best practice for every non-trivial project. It prevents version conflicts, makes projects reproducible, and keeps your main Python installation clean. Teachers and classmates can recreate your exact setup by reading a simple requirements.txt file.
Try It: Conceptual View of a Virtual Environment
Click Test My Code. This example shows the idea behind a virtual environment (the real commands are run in your computer’s terminal, not in the browser).
print("System Python (global):")
print(" Location: /usr/bin/python3 or C:\\Python3\\python.exe")
print(" Packages: available to every project")
print()
print("Virtual Environment (project-specific):")
print(" Location: myproject/venv/")
print(" Packages: only for this project")
print()
print("When the venv is active, 'python' and 'pip' point inside the venv.")Creating a Virtual Environment
Python includes a built-in module called venv that creates virtual environments. You run it once per project from the terminal (Command Prompt on Windows, Terminal on macOS/Linux). After creation you activate the environment so that subsequent commands use it.
The activation command is slightly different on Windows versus macOS/Linux, but the idea is the same: after activation your prompt usually shows the name of the environment, and any package you install stays inside that environment.
Try It: Commands to Create and Activate a venv
Click Test My Code. These are the exact commands you would type on your own computer.
print("1. Create the virtual environment:")
print(" python -m venv venv")
print()
print("2. Activate it:")
print(" macOS / Linux : source venv/bin/activate")
print(" Windows : venv\\Scripts\\activate")
print()
print("3. Your prompt should now show (venv)")
print("4. When finished: deactivate")Installing Packages with pip
Once a virtual environment is active, you install packages with the pip command. pip downloads the package from PyPI and places it inside the active environment. Popular packages for student projects include requests (for web data), matplotlib (for charts), pandas (for data tables), and pillow (for images).
Always install packages while the virtual environment is active. That way the packages stay isolated to the current project and can be easily listed later.
Try It: Common pip Commands
Click Test My Code. These commands are meant to be run in your terminal after activating a venv.
print("Install a package:")
print(" pip install requests")
print()
print("Install a specific version:")
print(" pip install matplotlib==3.8.0")
print()
print("Install several packages at once:")
print(" pip install pandas numpy matplotlib")
print()
print("See what is installed:")
print(" pip list")
print()
print("Uninstall a package:")
print(" pip uninstall requests")Freezing Dependencies with requirements.txt
After you have installed the packages your project needs, you should record them in a file named requirements.txt. This file lists every package and its version. Anyone who receives your project can recreate the exact same environment by running one command.
Creating and sharing a requirements.txt file is one of the most important professional habits you can learn. It is expected in college courses, internships, and open-source contributions.
Try It: Creating and Using requirements.txt
Click Test My Code. These are the two commands that make a project reproducible.
print("Save the current packages to a file:")
print(" pip freeze > requirements.txt")
print()
print("Later, on another computer or after a fresh venv:")
print(" pip install -r requirements.txt")
print()
print("Example contents of requirements.txt:")
print(" requests==2.31.0")
print(" matplotlib==3.8.0")
print(" pandas==2.1.1")A Typical Student Project Workflow
Putting the pieces together gives a reliable workflow that works for science-fair projects, class assignments, and personal experiments. Start a new folder, create a virtual environment, activate it, install what you need, write your code, and freeze the dependencies before you share the project.
Following this workflow from the beginning saves hours of “it works on my machine” problems later. Many university teachers now require a requirements.txt file with every major programming submission.
Try It: Complete Project Setup Checklist
Click Test My Code. Use this as a checklist the next time you start a Python project on your computer.
print("=== New Project Checklist ===")
print("1. mkdir my_science_project")
print("2. cd my_science_project")
print("3. python -m venv venv")
print("4. source venv/bin/activate # or venv\\Scripts\\activate")
print("5. pip install pandas matplotlib")
print("6. # write your Python files")
print("7. pip freeze > requirements.txt")
print("8. deactivate # when you are finished working")Popular Packages for Student Projects
Certain packages appear again and again in high-school and introductory college courses. Knowing the most common ones helps you choose the right tool quickly.
You do not need to memorize every package. Start with the ones your current project needs and grow your toolkit over time.
Try It: Common Packages and Their Purpose
Click Test My Code. This is a quick reference you can return to later.
print("Package Typical student use")
print("------- --------------------")
print("requests Download data from websites or APIs")
print("pandas Work with tables of data (CSV, Excel)")
print("matplotlib Create charts and graphs")
print("numpy Fast numeric calculations")
print("pillow Open and edit images")
print("flask Build simple web applications")
print("beautifulsoup4 Extract information from web pages")Checking That a Package Is Available
Sometimes you want to know whether a package is already installed before you try to import it. The standard pattern is to attempt the import inside a try/except block. This technique is also useful when you write code that should work both with and without an optional package.
In a real virtual environment the import will succeed only after you have run pip install. In the browser sandbox many scientific packages are already available through Pyodide, but the pattern remains the same.
Try It: Safe Import Check
Click Test My Code. The example shows how to test for a package gracefully.
try:
import pandas as pd
print("pandas is available")
print("Version:", pd.__version__)
except ImportError:
print("pandas is not installed in this environment")
print("Run: pip install pandas")Putting It All Together – A Realistic Scenario
Imagine you are preparing a data-analysis project about average temperatures in three American cities. You need pandas to load the data and matplotlib to draw a chart. The complete workflow from empty folder to finished, shareable project is shown below.
Following these steps every time will make your projects professional and easy for teachers or classmates to run on their own computers.
Try It: End-to-End Project Story
Click Test My Code. This narrative walks through a complete student project setup.
print("Project: Average High Temperatures in Austin, Denver, Seattle")
print()
print("Step 1 – Create and activate a virtual environment")
print("Step 2 – pip install pandas matplotlib")
print("Step 3 – Write analysis.py that loads CSV data and draws a chart")
print("Step 4 – pip freeze > requirements.txt")
print("Step 5 – Share the folder (code + requirements.txt + data)")
print()
print("Anyone else can now run:")
print(" python -m venv venv")
print(" source venv/bin/activate")
print(" pip install -r requirements.txt")
print(" python analysis.py")Tip: Always keep the virtual-environment folder (usually named venv) out of version control. Add a line with “venv/” to your .gitignore file so you do not upload hundreds of package files by accident.
Common Mistakes with Packages and Environments
- Installing packages without activating the venv: The packages go into the system Python instead of the project environment.
- Forgetting to freeze requirements: Classmates or teachers cannot recreate your setup.
- Committing the venv folder to Git: The folder is large and platform-specific; only the requirements.txt should be shared.
- Using the system Python for everything: Eventually different projects will need conflicting package versions.
- Mixing pip and conda in the same environment: Stick to one tool per environment to avoid conflicts.
- Assuming a package is installed because it worked last week: Always work inside the correct activated environment.
Good Habits for Packages and Environments
- Create a new virtual environment for every non-trivial project.
- Activate the environment before installing anything or running your code.
- Keep a requirements.txt file and update it whenever you add or upgrade a package.
- Give the environment folder a standard name such as venv or .venv.
- Document the Python version your project expects (for example in a README).
- When you finish working, deactivate the environment so you do not accidentally install packages in the wrong place.
Key Takeaways
- Third-party packages extend Python far beyond the standard library.
- A virtual environment isolates the packages of one project from all others.
- Create an environment with python -m venv venv and activate it before installing packages.
- Use pip install to add packages and pip freeze > requirements.txt to record them.
- Anyone can recreate your environment with pip install -r requirements.txt.
- This workflow is the professional standard used in schools, internships, and industry.
Frequently Asked Questions
What is the difference between a package and a module?
A module is a single .py file. A package is a folder that contains multiple modules (and an __init__.py file). Third-party libraries you install with pip are almost always distributed as packages.
Do I need a virtual environment for every small script?
For tiny one-file experiments you can work without a venv. As soon as a project needs one or more third-party packages, or will be shared with others, create a virtual environment.
What does pip stand for?
pip is the package installer for Python. It is the standard tool for downloading and installing packages from the Python Package Index (PyPI).
Why do I see (venv) in my terminal prompt?
That indicator means a virtual environment is currently active. All pip install commands and the python command now refer to that environment instead of the system Python.
Should I put the venv folder on GitHub?
No. The venv folder is large and contains compiled files specific to your operating system. Share only the requirements.txt file so others can rebuild the environment themselves.
What is the difference between pip and conda?
pip is the official Python package installer and works with virtual environments created by venv. conda is a separate package and environment manager popular in data science. For most student projects pip + venv is sufficient and simpler.
How do I know which packages my project needs?
Start by writing the code. Each time you need a capability that is not in the standard library, install the appropriate package with pip. When the project works, run pip freeze > requirements.txt to capture everything.
Can I use the same virtual environment for two different projects?
It is possible but not recommended. Different projects often need different package versions. Giving each project its own environment avoids conflicts and keeps requirements.txt accurate.