How to create a virtual environment in Python (2026)
Quick Answer
To create a Python virtual environment, first make sure Python is installed, then open a terminal in your project folder and run Python’s built-in venv module to create an isolated environment. Activate it, install your project packages inside it, and deactivate it when you are done. This keeps dependencies separate from your system Python and from other projects.
Overview
A virtual environment in Python is a self-contained folder that holds a project-specific Python interpreter path and installed packages. Using one is standard good practice because it helps you avoid version clashes between projects, keeps your system Python cleaner, and makes it easier to reproduce a working setup on another machine. For most users, the simplest method is Python’s built-in venv module, which is available in modern Python 3 versions. The usual workflow is: create a project folder, create the virtual environment inside or alongside it, activate the environment in your shell, install packages with pip, and save your dependencies if you plan to share or redeploy the project. The exact activation command differs between Windows Command Prompt, PowerShell, and macOS or Linux shells, so it is important to use the right one for your terminal. If anything fails, the most common causes are using the wrong Python command, not having venv available, or forgetting to activate the environment before installing packages. Once set up, you can work normally and only deactivate when you want to return to your global shell environment.
Who this is for
Python beginners, developers starting a new project, students following tutorials, and anyone who wants separate package sets for different Python projects.
What you’ll need
- A working Python 3 installation
- Access to a terminal, command prompt, or PowerShell
- A project folder
- pip, usually installed with Python
Before you start
Check that Python is installed and available from your terminal by running a version command such as python --version or python3 --version, depending on your system. If your machine has more than one Python installation, decide which one you want this project to use. Also choose the terminal you will use, because activation commands differ between Windows shells and macOS or Linux shells.
Step-by-step
- 1
Create or open your project folder
Make a dedicated folder for your project, then open a terminal in that folder. If the folder already exists, change into it before creating the environment.
Why: Keeping the environment tied to one project makes it easier to manage dependencies, avoid clutter, and understand which packages belong to which project.
- 2
Confirm which Python command works on your system
Run a version check such as python --version or python3 --version. On some Windows systems, the Python Launcher command py also works. Use the command that points to the Python version you want for the project.
Why: If you create the environment with the wrong interpreter, you may end up using the wrong Python version or installing packages in an unexpected place.
- 3
Create the virtual environment
Run the built-in venv module from your project folder, using a name such as .venv or venv for the environment directory. For example, use your working Python command followed by -m venv and the environment folder name.
Why: This creates an isolated environment with its own scripts and package installation location, which prevents one project’s dependencies from interfering with another’s.
- 4
Activate the environment in your shell
Use the activation script that matches your terminal. On macOS or Linux shells, activate from the environment’s bin folder. On Windows Command Prompt or PowerShell, activate from the Scripts folder. After activation, your shell prompt usually shows the environment name.
Why: Activation changes your shell session so python and pip point to the environment instead of the global installation, which is the main protection virtual environments provide.
- 5
Upgrade pip if needed and install packages
Once activated, install the packages your project needs using pip. If package management behaves oddly, consider upgrading pip inside the environment first, then install your dependencies.
Why: Installing inside the activated environment keeps dependencies local to the project and reduces the risk of permission issues or accidental global installs.
- 6
Record dependencies for reuse
If you want others, or your future self, to recreate the environment, save the installed packages to a dependency file such as requirements.txt using pip’s freeze output, then keep that file with the project.
Why: A dependency file helps you rebuild the same package set on another machine and improves repeatability for development, testing, and deployment.
- 7
Deactivate when finished
When you are done working in that environment, run the deactivate command in the same shell session.
Why: This returns your shell to the normal system context so later commands do not accidentally use the project environment.
Why this works
A Python virtual environment works by creating an isolated location for package installation and adjusting your shell so Python and pip resolve to that location first. That isolation means each project can use its own package versions without changing the system-wide Python setup.
Common mistakes to avoid
- Installing packages before activating the environment, which often puts them in the global Python instead
- Using the wrong Python command when creating the environment, so the project ends up on an unexpected Python version
- Committing the whole virtual environment folder to version control instead of the dependency file
- Forgetting that activation commands differ between Command Prompt, PowerShell, and macOS or Linux shells
- Assuming venv is available on every installation without checking, especially on some Linux distributions
Troubleshooting
The terminal says the python or python3 command is not recognised
Install Python or fix your PATH setup, then reopen the terminal. On Windows, the py command may work even if python does not.
You get an error saying no module named venv
Install or enable the venv component for your Python installation. On some operating systems, it is packaged separately; check your Python distribution or operating system package manager documentation.
Packages still seem to install globally
Make sure the environment is activated first, then confirm that python and pip point to the environment location. If needed, run pip via python -m pip to ensure the correct interpreter is being used.
PowerShell blocks the activate script
Check your execution policy and use Microsoft’s guidance to allow suitable script execution for your environment, or use Command Prompt instead.
The environment works in one terminal but not another
Activation only affects the current shell session. Activate the environment separately in each new terminal window or tab.
Compare your options
venv
Best for: Most standard Python 3 projects
Pros: Built into Python, simple, widely documented, no extra tool needed
Cons: Basic feature set compared with some third-party environment managers
virtualenv
Best for: Users who want a mature third-party tool or compatibility with setups that rely on it
Pros: Well known, fast, works across many workflows
Cons: Requires separate installation
Conda environments
Best for: Data science, scientific computing, or projects needing non-Python package management
Pros: Manages Python and non-Python dependencies together
Cons: Heavier workflow if you only need a simple Python project environment
| Option | Best for | Pros | Cons |
|---|---|---|---|
| venv | Most standard Python 3 projects | Built into Python, simple, widely documented, no extra tool needed | Basic feature set compared with some third-party environment managers |
| virtualenv | Users who want a mature third-party tool or compatibility with setups that rely on it | Well known, fast, works across many workflows | Requires separate installation |
| Conda environments | Data science, scientific computing, or projects needing non-Python package management | Manages Python and non-Python dependencies together | Heavier workflow if you only need a simple Python project environment |
Alternatives
- Use virtualenv instead of venv
- Use Conda environments if you need broader dependency management
- Use tools such as Poetry or Pipenv if you want dependency and environment management together
Pro tips
- Name the environment folder .venv or venv so editors and tools can detect it easily
- Keep the environment out of version control and commit a dependency file instead
- Use python -m pip rather than plain pip if you are unsure which interpreter pip is linked to
- Create one virtual environment per project rather than sharing one across unrelated work
- If your editor supports interpreter selection, point it at the virtual environment’s Python executable
Safety notes
- Only install packages from sources you trust, because pip packages can run installation code
- Be careful when changing shell execution settings on shared or managed computers
- Avoid running package installation commands with unnecessary administrator or root privileges unless your environment setup specifically requires it
What this guide does not cover: This guide covers creating and using a basic Python virtual environment with venv, not advanced environment automation, IDE-specific setup, container-based development, or dependency pinning strategies beyond the basics.
Cost considerations
Python, venv, and pip are generally available at no extra cost. Some third-party development tools or enterprise package repositories may have their own licensing or subscription terms.
Frequently asked questions
Do I need a virtual environment for every Python project?+
It is strongly recommended for most projects. It keeps dependencies separate and avoids package version conflicts between projects.
What should I call the environment folder?+
Common names are .venv and venv. The exact name is not important, but a standard name helps editors, IDEs, and other developers recognise it.
Should I commit the virtual environment folder to Git?+
Usually no. Commit your source code and a dependency file such as requirements.txt instead, then recreate the environment on each machine.
Can I have more than one virtual environment on the same computer?+
Yes. That is one of the main benefits. Each project can have its own isolated environment and package versions.
How do I know the environment is active?+
Your shell prompt often shows the environment name, and commands such as python or pip should resolve to paths inside the environment folder.
Sources & references
Guidance on this page is traced to documented sources. Last checked 25 September 2026.
- Python Packaging User Guide · official
Supports the standard workflow for creating and using virtual environments with venv and installing packages with pip.
- Python Standard Library documentation: venv · official
Supports that venv is the built-in module for creating virtual environments and documents activation and usage.
- Microsoft Learn: about_Execution_Policies · official
Supports troubleshooting for PowerShell script execution restrictions that can affect activation scripts.
The basic venv workflow is stable, though shell commands and packaging tools can evolve over time.