How to Create and Use a Virtual Environment in Python
A virtual environment lets you create an isolated Python setup for one project.
This is useful when you want to:
- install packages without affecting other projects
- keep project dependencies separate
- avoid version conflicts between libraries
On this page, you will learn how to create a virtual environment, activate it, install packages inside it, and leave it when you are done.
Quick answer #
Use this when you want the fastest working setup:
python -m venv .venv
# Windows
.venv\Scripts\activate
# macOS/Linux
source .venv/bin/activate
pip install requests
deactivate
What these commands do:
python -m venv .venvcreates a new virtual environment in a folder named.venvactivateturns that environment on in your terminalpip install requestsinstalls therequestspackage inside that environmentdeactivateturns the environment off
What a virtual environment is #
A virtual environment is a separate folder that contains:
- its own Python setup
- its own
pip - its own installed packages
This helps keep one project separate from another.
For example:
- Project A can use one version of a package
- Project B can use a different version
- neither project breaks the other
This is one of the safest habits to learn early in Python.
When you should use one #
Use a virtual environment for almost every Python project.
It is especially helpful when:
- you plan to install third-party packages with
pip - you are following a tutorial that needs extra libraries
- you want to keep your system Python clean
- different projects need different package versions
For a tiny one-file practice script, it may not always be necessary. But it is still a good habit.
If you are new to installing packages, see how to install a Python package with pip.
Before you start #
Before creating the environment, make sure:
- Python is installed
pythonworks in your terminal or command prompt- you opened the terminal in your project folder
- you picked a clear environment name such as
.venvorvenv
You can test Python with:
python --version
Example output:
Python 3.12.2
If this does not work, you may need to install Python on Windows, macOS, and Linux.
Create the virtual environment #
Run this command in your project folder:
python -m venv .venv
What it does:
- runs Python
- uses the built-in
venvmodule - creates a new folder named
.venv
That folder stores the environment files and installed packages.
A simple project might look like this after creation:
my_project/
├── .venv/
└── app.py
Do not edit files inside .venv manually. Python and pip manage that folder for you.
Activate the virtual environment #
After creating the environment, activate it.
Windows Command Prompt #
.venv\Scripts\activate
Windows PowerShell #
.venv\Scripts\Activate.ps1
macOS and Linux #
source .venv/bin/activate
After activation:
- package installs go into this environment
pythonandpipusually point to the environment versions- your terminal often shows the environment name at the start
For example, your prompt may change from:
C:\projects\my_project>
to:
(.venv) C:\projects\my_project>
Check that it is active #
You can confirm that the environment is active in a few ways.
Look at the terminal prompt #
Many terminals show the environment name, such as:
(.venv)
Check the Python version #
python --version
Check which pip is being used #
pip --version
You can also use:
python -m pip --version
This is often more reliable because it uses the current Python interpreter directly.
Check the Python path #
On Windows:
where python
where pip
On macOS/Linux:
which python
which pip
If the environment is active, these commands should point to files inside the .venv folder.
Install packages inside the environment #
Once the environment is active, you can install packages with pip.
Example:
pip install requests
This installs requests only inside the current project environment.
You can check installed packages with:
pip list
You can also test the package in Python code.
Create a file named app.py:
import requests
print("requests installed successfully")
print(requests.__version__)
Run it with:
python app.py
Example output:
requests installed successfully
2.32.3
If you are not sure how imports work, see how imports work in Python or how to import a module in Python.
Use the environment in your editor or IDE #
Many editors detect a .venv folder automatically.
If yours does not, choose the Python interpreter from the .venv folder manually.
This matters because:
- Run and Debug should use the same interpreter as your terminal
- installed packages must be available in both places
- using the wrong interpreter can cause import errors
A common beginner problem is:
- the package works in the terminal
- but the editor says the module cannot be found
That usually means the editor is using a different Python interpreter.
If needed, this can also help when learning how to run Python code from the command line and IDEs.
Deactivate the environment #
When you are done, run:
deactivate
This returns your terminal to the global Python environment.
Important:
deactivatedoes not delete the environment- it only turns it off for the current terminal session
- you can activate it again later
Delete and recreate an environment #
If you want to remove the environment, delete the .venv folder.
Then recreate it with:
python -m venv .venv
This is useful when:
- the environment becomes broken
- package versions get messy
- you want a clean setup
After recreating it, install your packages again.
Save and restore dependencies #
If you want to reuse the same packages later, save them to a file.
Save installed packages #
pip freeze > requirements.txt
This creates a requirements.txt file.
Restore packages later #
pip install -r requirements.txt
This is useful when:
- moving the project to another computer
- sharing the project with someone else
- recreating the environment from scratch
A common workflow looks like this:
python -m venv .venv
# activate the environment first
pip install requests
pip freeze > requirements.txt
Later, after deleting or recreating the environment:
python -m venv .venv
# activate the environment first
pip install -r requirements.txt
Common problems and quick fixes #
python command not found #
This usually means Python is not installed correctly, or it is not on your system PATH.
Try:
python --version
If that fails, review how to install Python on Windows, macOS, and Linux.
Activation script is blocked in PowerShell #
On Windows PowerShell, you may get an execution policy error when running:
.venv\Scripts\Activate.ps1
Quick fixes:
- use Command Prompt instead
- or adjust the PowerShell execution policy if allowed on your system
Package installs to the wrong place #
This often happens when you run pip install before activating the environment.
Check:
pip --version
python -m pip --version
If the path does not point into .venv, activate the environment first.
ModuleNotFoundError after installing a package #
This usually means one of these things happened:
- the package was installed in a different environment
- the editor is using a different interpreter
- the environment was not active during install
Related help:
Common mistakes #
These are the most common beginner mistakes with virtual environments:
- trying to install packages before activating the environment
- using the wrong activation command for the operating system or shell
- running
pipfrom the global Python instead of the environment - opening the editor with a different interpreter than the terminal
- assuming
deactivatedeletes the environment
Useful commands for checking problems:
python --version
python -m venv .venv
pip --version
python -m pip --version
pip list
On Windows:
where python
where pip
On macOS/Linux:
which python
which pip
FAQ #
Do I need a virtual environment for every project? #
For most projects, yes. It keeps packages separate and avoids version conflicts.
What is the difference between venv and pip? #
venv creates the isolated environment. pip installs packages into that environment.
Should I name the folder venv or .venv? #
Both work. .venv is common because many editors recognize it and it stays less visible.
Does deactivate remove installed packages? #
No. It only stops using the environment in the current terminal session.
Can I share a virtual environment folder with other people? #
Usually no. Share a requirements.txt file instead, and let each person create their own environment.