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 all the packages (add-on libraries) your project needs. When you create one, Python stops using the global packages installed everywhere on your machine and instead uses only what you put in that folder.
This matters because different projects often need different versions of the same package. Project A might need version 2.0 of a library, while Project B needs version 3.0. Without virtual environments, installing version 3.0 would break Project A. A virtual environment lets each project have exactly what it needs without stepping on the others.
Virtual environments also make it easier to share your project with someone else or move it to a server. Instead of saying "install these 47 packages in whatever version you have," you can say "use this exact list," and their setup will match yours.
Key Takeaways
- A virtual environment is a folder that holds its own copy of Python and project-specific packages, separate from your computer's main Python installation.
- You create a virtual environment using the python -m venv command followed by a folder name, usually something like venv or env.
- After creating the environment, you must set up it before installing packages or running code, using a different command depending on whether you use Windows, Mac, or Linux.
- Once activated, pip install package-name installs packages into that environment only, and you can save the list of packages to a file called requirements.txt for sharing with others.
Creating a virtual environment on Windows
Open Command Prompt or PowerShell and navigate to the folder where you want your project to live. Type the following command and press Enter:
python -m venv venv
This tells Python to create a module called venv (the virtual environment tool) and use it to build a new environment in a folder named venv. You will see a new folder appear in your project directory. Inside it, Python has created a complete, isolated copy of itself.
Now set up the environment by running this command:
venv\Scripts\set up
If you are using PowerShell and get an error about execution policies, run this first:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
When the environment is active, your command prompt will show (venv) at the beginning of the line. This tells you that any packages you install or code you run will use this environment, not your global Python.
Creating a virtual environment on Mac and Linux
Open Terminal and navigate to your project folder. Run:
python3 -m venv venv
Note that on Mac and Linux, the command is python3, not python. This ensures you are using Python 3 rather than the older Python 2 that may still be on your system. The rest works the same way — a new venv folder appears in your project directory.
set up the environment with:
source venv/bin/set up
Your Terminal prompt will now show (venv) at the start of the line, the same as on Windows. You are now working inside the virtual environment.
Installing packages into your virtual environment
With the environment activated (you should see (venv) in your prompt), use pip to install any package you need. For example, to install a package called requests, type:
pip install requests
Pip will read and install the package into your virtual environment folder only. Your global Python installation remains untouched. You can install as many packages as your project needs, and each one goes into the same environment.
If you need a specific version of a package, use the == operator:
pip install requests==2.28.0
This installs version 2.28.0 of requests instead of the latest version. This is useful when you know your code works with a particular version and you want to avoid surprises from newer releases.
Saving and sharing your package list
When your project is complete and you want to share it or move it to another computer, you need to record which packages you installed and which versions. Create a file called requirements.txt by running:
pip freeze > requirements.txt
This command writes a list of every package in your environment and its version number to a text file. The file will look something like this:
requests==2.28.0 urllib3==1.26.12 certifi==2022.9.24
When someone else (or you on a different computer) gets your project, they create their own virtual environment and run:
pip install -r requirements.txt
Pip reads the file and installs every package at the exact version you specified. Their environment now matches yours, and your code will run the same way.
Deactivating and reusing your environment
When you are done working on your project, deactivate the virtual environment by typing:
deactivate
The (venv) prefix disappears from your prompt, and you are back to using your global Python. The environment folder stays on your computer — you have not deleted anything.
The next time you want to work on the same project, navigate to your project folder and set up the environment again using the same command you used before (venv\Scripts\set up on Windows, or source venv/bin/set up on Mac and Linux). All your packages are still there, exactly as you left them.
If you want to delete the environment entirely, straightforward delete the venv folder. Since all the packages live inside it, deleting the folder removes everything. You can always recreate it later using the same steps, then reinstall packages from your requirements.txt file.
Frequently Asked Questions
Can I name my virtual environment something other than venv?
Yes. The folder name does not matter — you can call it env, myproject, or anything else. The command stays the same: python -m venv your-folder-name. Most people use venv or env because it is when ready clear what the folder contains.
What happens if I forget to set up the environment before installing packages?
Pip will install packages into your global Python instead of your virtual environment. Your project will still work on your computer, but when someone else runs it without those global packages installed, it will fail. Always check that (venv) appears in your prompt before running pip install.
Do I need to commit the venv folder to version control?
No. The venv folder is large and specific to your computer. Instead, commit your requirements.txt file. When someone clones your project, they create their own virtual environment and install from requirements.txt. Most projects add venv/ to a .gitignore file to prevent it from being tracked.
Can I use the same virtual environment for multiple projects?
Technically yes, but it is not recommended. Each project should have its own environment so that different version requirements do not conflict. The disk space saved by sharing one environment is small compared to the confusion of managing packages across multiple projects.
What is the difference between venv and virtualenv?
venv is built into Python 3.3 and later, so it requires no separate installation. virtualenv is an older, third-party tool that does the same thing. For new projects, use venv since it comes with Python. You only need virtualenv if you are working with very old Python versions.