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 your main Python installation or other projects.
The practical reason: different projects often need different versions of the same package. Project A might need Django 3.2, while Project B needs Django 4.0. Without virtual environments, you'd have to uninstall and reinstall packages constantly. With a virtual environment for each project, both versions can exist peacefully on the same computer.
Virtual environments also make it easier to share your project with others. When you list all the packages your project uses (in a file called requirements.txt), someone else can recreate your exact setup in minutes, rather than guessing which versions you had installed.
Key Takeaways
- Create a virtual environment by running python -m venv project_name in the folder where you want it to live.
- Activate the environment before you work: on Windows, run project_name\Scripts\activate; on Mac or Linux, run source project_name/bin/activate.
- When the environment is active, your command prompt will show the environment name in parentheses, and any packages you install will go into that environment only.
- Deactivate the environment by typing deactivate when you're done working, or close the terminal window.
- Save your project's packages to a file by running pip freeze > requirements.txt, so others can install the same versions.
Creating a virtual environment on Windows
Open Command Prompt or PowerShell and navigate to the folder where you want your project to live. If you don't have a project folder yet, create one first: mkdir my_project, then cd my_project.
Type this command and press Enter:
python -m venv venv
Replace the second venv with whatever you want to name your environment folder — env, myenv, or project_env all work. The folder will be created in your current directory, and it will contain everything your isolated Python setup needs.
If you get an error saying Python is not recognized, Python may not be in your system PATH. Check that you installed Python correctly and selected "Add Python to PATH" during installation. Restart your computer after reinstalling if needed.
Creating a virtual environment on Mac or Linux
Open Terminal and navigate to your project folder. If you need to create one, use mkdir my_project and then cd my_project.
Type this command:
python3 -m venv venv
Note the python3 — on Mac and Linux, you usually need to specify version 3 explicitly. The rest works the same as Windows: a folder named venv (or whatever you named it) will be created in your current directory.
If the command fails, you may need to install the venv module separately. On Ubuntu or Debian, run sudo apt-get install python3-venv. On Mac with Homebrew, run brew install python3.
Activating your virtual environment
Creating the environment is only the first step. You must activate it before you start working, so that Python uses the isolated copy instead of your system Python.
On Windows: In Command Prompt or PowerShell, run:
venv\Scripts\activate
On Mac or Linux: In Terminal, run:
source venv/bin/activate
After activation, your command prompt will change. You'll see the environment name in parentheses at the start of the line, like (venv) C:\Users\yourname\my_project> on Windows or (venv) yourname@computer my_project % on Mac. This tells you the environment is active and ready to use.
Installing packages and checking what's installed
Once your environment is active, use pip install to add packages. For example, pip install requests installs the requests library into your virtual environment only, not your system Python.
To see what's already installed, type pip list. You'll see a much shorter list than if you ran this on your system Python — usually just pip, setuptools, and wheel to start.
To save a record of what your project needs, run pip freeze > requirements.txt. This creates a file listing every package and its version number. When someone else (or you on a different computer) wants to run your project, they can activate their own virtual environment and run pip install -r requirements.txt to install everything at once.
Deactivating and managing multiple environments
When you're done working, type deactivate to exit the virtual environment. Your command prompt will return to normal, and Python will use your system installation again. You don't have to deactivate — closing the terminal window does the same thing — but it's good practice to be explicit about it.
You can create as many virtual environments as you have projects. Each one is just a folder, so you can have project_a/venv, project_b/venv, and project_c/venv all on the same computer without any conflict. The only rule is to activate the right environment before you work on each project.
If you want to delete an environment, just delete its folder. Since everything is self-contained, there's nothing left behind on your system. If you later need that environment again, you can recreate it and reinstall packages from your requirements.txt file.
Frequently Asked Questions
Do I have to name my virtual environment "venv"?
No. You can name it anything: env, myenv, .venv, or project_env all work. The name only matters to you — it's just a folder. Many developers use .venv (starting with a dot) to hide it from view, or venv because it's the most common convention and others will recognize it immediately.
What if I activate the wrong environment by mistake?
Type deactivate to exit it, then navigate to the correct project folder and activate the right environment. There's no harm in activating the wrong one — you just won't have the packages you need for your current project.
Can I move a virtual environment to a different folder?
It's not recommended. Virtual environments contain absolute paths to your Python installation, so moving the folder can break things. Instead, create a new environment in the new location and use your requirements.txt file to reinstall packages.
Why does my IDE ask me to select a Python interpreter?
IDEs like VS Code and PyCharm let you choose which Python installation to use. Point it to your virtual environment's Python executable — on Windows it's venv\Scripts\python.exe, on Mac or Linux it's venv/bin/python — and the IDE will use that isolated environment automatically.
Do I need to commit my virtual environment folder to version control?
No. Add the environment folder name to your .gitignore file so it doesn't get uploaded. Other developers will create their own environment and install from requirements.txt. This keeps your repository small and avoids conflicts between different operating systems.