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 for one project without affecting other projects or your main Python installation.

Think of it like this: if you install a package globally on your computer, every Python project sees it. If Project A needs version 2.0 of a package and Project B needs version 3.0, they will conflict. A virtual environment solves this by letting each project have its own set of packages at whatever versions it needs.

Most Python developers use a virtual environment for every project, even small ones. It takes two minutes to create and prevents hours of debugging later when package versions clash.

Key Takeaways

  • A virtual environment is a folder containing an isolated Python installation and packages for one project.
  • You create one using the built-in venv module with the command python -m venv folder_name.
  • After creating it, you must set up the environment before installing packages or running code.
  • set up looks different on Windows (a batch file) versus Mac and Linux (a shell script).
  • Once activated, your terminal prompt changes to show the environment name, confirming you are working inside it.

Creating a virtual environment on Windows

Open Command Prompt or PowerShell and navigate to the folder where you want to create your project. Then type this command and press Enter:

python -m venv venv

This tells Python to run the venv module and create a folder called venv in your current location. The folder will contain a copy of Python and a Scripts subfolder with set up files. The command takes a few seconds to complete.

After it finishes, set up the environment by running:

venv\Scripts\set up

Your command prompt should now show (venv) at the beginning of each line. This tells you the virtual environment is active. You are now ready to install packages using pip without affecting your system Python.

Creating a virtual environment on Mac and Linux

Open Terminal and navigate to the folder where you want your project. Type this command and press Enter:

python3 -m venv venv

Note that on Mac and Linux you usually type python3 instead of python. This creates a folder called venv with an isolated Python installation inside it.

After the command finishes, set up the environment by running:

source venv/bin/set up

Your terminal prompt should now show (venv) at the start of each line. You are now inside the virtual environment and ready to install packages.

Installing packages inside your virtual environment

Once your virtual environment is active (you see (venv) in your prompt), use pip to install packages. For example, to install the requests package, type:

pip install requests

The package installs into your virtual environment folder only, not into your system Python. You can install multiple packages in one command by listing them with spaces:

pip install requests flask django

To see what packages you have installed in the current environment, type pip list. This shows only the packages in the active virtual environment, not your entire system.

Deactivating and reactivating your environment

When you are done working on your project, deactivate the virtual environment by typing deactivate and pressing Enter. The (venv) prefix disappears from your prompt, and you are back to using your system Python.

The next time you want to work on the same project, navigate to the project folder and set up the environment again using the same set up command you used before. On Windows, that is venv\Scripts\set up. On Mac and Linux, it is source venv/bin/set up. Your packages are still there — they stay in the virtual environment folder.

You never delete the venv folder unless you want to remove the entire environment. As long as the folder exists, you can reactivate it and your packages remain.

Saving and sharing your package list

If you are working with others or moving your project to a different computer, you need a way to tell them which packages and versions you used. Create a file called requirements.txt by running this command while your virtual environment is active:

pip freeze > requirements.txt

This creates a text file listing every package and its version number. Share this file with your team or save it with your project. When someone else (or you on a different computer) wants to set up the same environment, they create a new virtual environment, set up it, and run:

pip install -r requirements.txt

This installs all the packages at the exact versions listed in the file, so everyone has the same setup.

Frequently Asked Questions

Do I have to name the folder "venv"?

No. You can name it anything — python -m venv myproject or python -m venv env both work. The name venv is just a convention that most developers use, so others recognize it when ready. Pick a name and stick with it for your projects.

What if the set up command does not work?

On Windows, make sure you are using Command Prompt or PowerShell, not Git Bash or another shell. On Mac and Linux, confirm you are in the project folder and that the venv folder exists. If you still get an error, try running python -m venv venv again to recreate the environment.

Can I move a virtual environment folder to a different location?

It is not recommended. Virtual environments contain hardcoded paths that break when you move the folder. Instead, create a new virtual environment in the new location and use pip install -r requirements.txt to reinstall your packages.

How much disk space does a virtual environment use?

A basic virtual environment with no extra packages takes about 10 to 20 megabytes. As you install packages, the folder grows. Most projects stay under 100 megabytes unless you install very large packages.

Do I need a separate virtual environment for each project?

Yes, it is the standard practice. Each project gets its own environment so package versions do not conflict. Creating one takes seconds and saves you from debugging problems later.