What a virtual environment does and why you need one
A virtual environment is a folder on your computer that holds a separate copy of Python and its packages, isolated from the rest of your system. When you create one, you get a self-contained workspace where you can install packages without affecting Python on the rest of your machine or other projects.
Think of it like this: if you install a package version that breaks your other code, it only breaks code in that virtual environment. Your main Python installation stays clean. This matters because different projects often need different versions of the same package, and virtual environments let you run them side by side.
Most Python developers create a new virtual environment for each project. It takes two minutes and saves hours of debugging later when package versions collide.
Key Takeaways
- Create a virtual environment by running python -m venv folder-name in your project directory, where folder-name is usually venv.
- Activate the environment by running the activation script: source venv/bin/activate on Mac or Linux, or venv\Scripts\activate on Windows.
- Once activated, your terminal prompt changes to show the environment name, and pip install commands only affect that environment.
- Deactivate the environment by typing deactivate when you are done working, or close the terminal window.
- Save your installed packages to a file called requirements.txt using pip freeze > requirements.txt so others can recreate your setup.
Creating a virtual environment on Windows
Open Command Prompt or PowerShell and navigate to the folder where your project lives. Type this command exactly:
python -m venv venv
This creates a folder called venv inside your project directory. The folder contains a complete Python installation for just this project. The command takes a few seconds to run. When it finishes, you will see a new venv folder appear in your file explorer.
If you get an error saying Python is not recognized, Python is not installed or not in your system PATH. Download it from python.org and make sure to check the box that says "Add Python to PATH" during installation.
Creating a virtual environment on Mac or Linux
Open Terminal and navigate to your project folder. Run the same command:
python3 -m venv venv
Note that on Mac and Linux you usually type python3 instead of python. The command creates a venv folder with a complete Python installation inside it. This takes a few seconds.
If you get a "command not found" error, Python 3 is not installed. On Mac, install it from python.org or using Homebrew. On Linux, use your package manager: sudo apt install python3-venv on Ubuntu or Debian.
Activating the virtual environment
Creating the environment is step one. Activating it tells your terminal to use the Python and packages inside that folder instead of your system Python.
On Windows: Run this command in Command Prompt or PowerShell:
venv\Scripts\activate
On Mac or Linux: Run this command in Terminal:
source venv/bin/activate
When activation works, your terminal prompt changes. You will see (venv) at the start of the line, like this:
(venv) C:\Users\YourName\project> on Windows, or (venv) user@computer project % on Mac.
This tells you the virtual environment is active. Any package you install now goes into the venv folder, not your system Python. If you do not see (venv) in your prompt, the activation did not work — check that you typed the path exactly right and that you are in the correct folder.
Installing packages in your virtual environment
Once the environment is active, use pip install as normal. For example, to install the requests package:
pip install requests
The package installs into your virtual environment only. Other projects on your computer do not see it. If you install a different version of requests in another project's virtual environment, the two versions do not interfere with each other.
You can also install multiple packages at once: pip install requests flask numpy. Or install a specific version: pip install requests==2.28.0.
Saving and sharing your package list
When you finish your project or want to share it with someone else, save the list of installed packages to a file called requirements.txt. Run this command while the virtual environment is active:
pip freeze > requirements.txt
This creates a text file listing every package and its version number. When someone else clones your project, they create their own virtual environment and run:
pip install -r requirements.txt
This installs all the same packages at the same versions, so the code runs the same way for them. This is standard practice for any Python project you plan to share or come back to later.
Deactivating and cleaning up
When you finish working, deactivate the virtual environment by typing:
deactivate
The (venv) prefix disappears from your prompt. You are back to using your system Python. You can close the terminal window or open a new project folder and create a new virtual environment there.
The virtual environment folder stays on your computer until you delete it. If you want to remove it completely, just delete the venv folder. This does not affect your system Python or any other projects. If you ever need it again, create a new one with the same command.
Frequently Asked Questions
Do I have to name the folder venv?
No. You can name it anything: python -m venv my-env or python -m venv env both work. The name venv is just a convention that most Python developers use, so other people recognize it immediately. If you use a different name, remember to activate from that folder instead.
What if I activate the environment but pip install still does not work?
Check that (venv) appears in your prompt. If it does not, the activation failed — try running the activation command again, making sure you are in the project folder. If you still get errors, try upgrading pip: pip install --upgrade pip. On some systems, you may need to use pip3 instead of pip.
Can I use the same virtual environment for multiple projects?
Technically yes, but do not. Each project should have its own virtual environment. If two projects need different versions of the same package, they will conflict. Separate environments take almost no disk space and prevent hours of debugging.
What happens if I delete the venv folder by accident?
Nothing breaks. Just create a new one with python -m venv venv and run pip install -r requirements.txt to reinstall your packages. This is why saving requirements.txt matters — it lets you recreate the environment in seconds.
Do I need to commit the venv folder to version control?
No. Add venv/ to your .gitignore file so the folder does not get uploaded. Other people clone your project and create their own virtual environment, then run pip install -r requirements.txt. This keeps repositories small and avoids platform-specific issues.