Moment 2: The Python Environment - venv and pip
Reading time: approx. 10 min
In the previous moment we set up our basic toolbox. Now it's time to use it to build our first specific development environment, and we start with one of the world's most popular and versatile languages: Python.
One of the biggest pitfalls when starting with Python is installing all packages and libraries directly in the system. This can quickly lead to conflicts. Imagine that one project requires version 1.0 of a library, while another requires version 2.0. The solution is virtual environments. In this moment you will learn the professional method for keeping your Python projects clean, isolated and reproducible.
What You Will Learn
After this moment you will be able to:
- Explain why virtual environments are crucial for serious Python development.
- Create, activate and deactivate a virtual environment with
venv. - Use the package manager
pipto install program libraries in an active environment. - Document your project's dependencies in a
requirements.txtfile for easy sharing and reinstallation.
The Basics: Keep Things Organized with Virtual Environments
Imagine you are building two different LEGO models. One is a red sports car and the other is a blue spaceship. Instead of having all the LEGO pieces, red and blue, in one single large box, it is much smarter to have two separate boxes, one for each model.
A virtual environment is exactly such a separate box for your Python project. Each project gets its own isolated set of tools and libraries, completely independent of other projects.
venv: Is Python's built-in tool for creating these "boxes".pip: Is the tool you use to add new "LEGO pieces" (packages/libraries) to the box you are currently working with.pipfetches packages from the Python Package Index (PyPI), a massive, public library of Python code.
Practical Examples: Create Your First Python Project
Now we put it all together in practice. Open your terminal and follow along.
1. Create a project folder First we create a folder for our project and navigate into it.
mkdir my_python_project
cd my_python_project
2. Create the virtual environment Now we create the virtual environment inside our project folder. We name it .venv. The dot at the beginning makes the folder hidden, which is a common convention.
python3 -m venv .venv
3. Activate the environment To "open our LEGO box" and start working in it, we need to activate the environment.
source .venv/bin/activate
You will immediately see that your terminal prompt changes! It gets a (.venv) at the beginning, which is your visual confirmation that the virtual environment is active.
4. Use pip to install a package Let's install a fun package called cowsay.
pip install cowsay
5. Run code that uses the package Now we can run Python code that uses our newly installed package.
python -c "import cowsay; cowsay.cow('Hello from my virtual environment!')"
You should now see an ASCII cow saying hello. This proves that the package is installed and working inside our environment.
6. Deactivate the environment When you are done with your work in the project, you "close the LEGO box" by deactivating the environment.
deactivate
The prompt returns to normal. If you now try to run the cowsay command again, you will get an error message, because the package only exists inside the virtual environment. This proves that the isolation works!
Document Your Dependencies: requirements.txt
When you want to share your project with a colleague or student, they need a simple way to install exactly the same packages you have used. This is done with a file that is by default called requirements.txt.
- Create the file (with the environment active):
pipcan automatically create a list of all installed packages.pip freeze > requirements.txt - Install from the file (in a new environment): Another person can now create their own virtual environment, activate it, and then run this command to install everything needed:
pip install -r requirements.txt
This is the foundation for reproducible and shareable Python code.
Exercise: Your Own Library
Now it's your turn to go through the entire workflow.
- Create a new folder in any location, e.g.
my_project_2. - Navigate into it and create a virtual environment.
- Activate the environment.
- Use
pipto install therequestspackage (a very common library for making web requests). - Run
pip freeze > requirements.txtto create your dependency file. - Open the project folder in VS Code and look at the contents of your
requirements.txt. - Finally deactivate the environment.
Next Steps
Excellent! You can now create clean and professional environments for your Python projects. But what happens when you start writing code and want to save your progress in a safe way? In the next moment, Version Control with Git and GitHub, we take your new project and learn how to track changes and share your code with others.

