Different Python projects need different packages, and sometimes different versions of the same package. Without isolation, installing a package for one project can break another. Virtual environments solve this by giving each project its own Python environment.
This article assumes you know how to install packages with pip and have at least a single-file Python project to work with.
What a virtual environment is
A virtual environment is a folder that contains a copy of the Python interpreter and its own site-packages directory. When you activate the environment, any packages you install go into that folder, not into your global Python installation.
Think of it as a clean room for each project. Project A can use requests version 2.28 and Project B can use requests version 2.32, and they never interfere with each other.
Without virtual environments, all packages go into one global location. Install something for one project and it affects every other Python script on your machine.
Create a virtual environment
Python includes a built-in module called venv for creating virtual environments. Open a terminal, navigate to your project folder, and run:
python -m venv .venvOn systems where Python 3 uses python3:
python3 -m venv .venvThis command creates a folder called .venv inside your project directory. The dot at the start keeps it hidden on macOS and Linux (similar to .git folders). You can name it anything, but .venv is the most common convention.
The .venv folder contains:
- A copy of the Python interpreter
- A
lib/directory withsite-packagesfor installed packages - Activation scripts for different shells
- pip, the package installer
The folder is typically a few megabytes. You can safely delete and recreate it at any time because it contains no project code, only the environment.
Activate the virtual environment
Creating the environment is not enough. You must activate it so your terminal uses the isolated Python instead of the global one.
macOS and Linux:
source .venv/bin/activateWindows (Command Prompt):
.venv\Scripts\activateWindows (PowerShell):
.venv\Scripts\Activate.ps1After activation, your terminal prompt changes to show the environment name:
(.venv) user@computer:~/projects/number-guesser$This is the visual cue that the environment is active. Now python and pip commands use the virtual environment.
Verify the environment is active
Check which Python you are using:
which pythonOn macOS/Linux, this should point to .venv/bin/python inside your project. On Windows, use where python instead.
Also check pip:
python -m pip --versionThe output should show pip installed inside your .venv folder.
Install packages inside the environment
With the environment active, install packages normally:
python -m pip install requestsThe package installs into .venv/lib/site-packages and is available only when this environment is active. Other projects on your machine do not see it.
Verify with:
python -m pip listSave the package list for your project:
python -m pip freeze > requirements.txtThis records the exact versions, which is important when you share the project or come back to it later.
Deactivate the environment
When you are done working on the project, deactivate:
deactivateYour terminal prompt returns to normal. The python command now points to your global installation again.
Deactivation does not delete anything. Your .venv folder and all installed packages remain intact. Reactivate whenever you return to the project.
Recreate an environment from requirements.txt
If you clone a project from GitHub or move to a new computer, you do not copy the .venv folder. You recreate it:
git clone https://github.com/user/number-guesser.git
cd number-guesser
python -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txtThis gives you an identical environment with the exact same package versions the project was built with.
Add .venv to .gitignore
Never commit the .venv folder to version control. It is large, platform-specific, and can be recreated from requirements.txt. Create a .gitignore file in your project root and add:
.venv/
__pycache__/The __pycache__/ entry ignores compiled bytecode caches that Python generates. These should also stay out of version control. For more on bytecode, see how Python runs your code.
Common workflows
Starting a new project:
mkdir my-project
cd my-project
python -m venv .venv
source .venv/bin/activateReturning to an existing project:
cd my-project
source .venv/bin/activateAdding a new dependency:
# Make sure the environment is active
python -m pip install pandas
python -m pip freeze > requirements.txtSwitching between projects:
# In project A
deactivate
cd ../project-b
source .venv/bin/activateAlways deactivate one environment before activating another. Having two environments active in the same terminal is not possible, but it is easy to forget which one is active. Check your terminal prompt.
When not to use a virtual environment
For a quick one-off script that you plan to delete after running it once, a virtual environment is unnecessary. The same goes for experimenting in the interactive REPL.
But any project you plan to keep, share, or work on for more than a day deserves a virtual environment. The overhead is tiny -- one command to create, one to activate -- and it prevents hours of debugging mysterious package conflicts later.
Editor integration
Most editors can detect virtual environments and activate them automatically. In VS Code, open the Command Palette (Cmd+Shift+P or Ctrl+Shift+P), run "Python: Select Interpreter", and choose the path inside .venv. After that, running code from the editor uses the virtual environment automatically.
In PyCharm, the IDE usually detects and offers to use a .venv folder when you open a project. Accept the prompt and PyCharm handles activation behind the scenes.
Next steps
With virtual environments set up, you have all the tools needed for a professional Python workflow. The next article covers how to debug installation problems, and after that, set up your complete Python workspace for daily coding.
When your environment is ready, move on to Python syntax and indentation to start learning the language itself.
Rune AI
Key Insights
Create a virtual environment with python -m venv .venv; Activate with source .venv/bin/activate (macOS/Linux) or .venv\\Scripts\\activate (Windows); Always activate the environment before installing packages or running your code; Add .venv/ to .gitignore so it is never committed to version control.
Frequently Asked Questions
Why do I need a virtual environment?
Should I create a virtual environment inside my project folder?
What is the difference between venv and virtualenv?
Do I need to activate the virtual environment every time?
Conclusion
Virtual environments are the standard way to keep Python project dependencies isolated. Use python -m venv .venv once per project, activate it every time you work on the project, and install packages with pip inside the active environment.
More in this topic
Create and Assign Variables in Python
Learn every way to create and assign variables in Python, from basic single assignment to expressions, type hints, and practical patterns beginners use every day.
Python Variable Naming Rules
Learn Python's variable naming rules, including allowed characters, reserved keywords, PEP 8 conventions, and how to choose names that make your code readable.
Python Variables Explained
Understand what Python variables are, how they store and reference data, and why Python's variable model is different from other programming languages.