PythonPython Environment and Development ToolsPython environment setup
What is the difference between venv and virtualenv?
Question 21 of 21 · Updated
On this page
- 1. venv vs virtualenv at a glance
- 2. What does venv actually do?
- 3. What does virtualenv do?
- 4. Creating environments with each tool
- Option A: standard-library venv
- Option B: third-party virtualenv
- 5. Selecting the Python version: an important difference
- 6. How do both tools isolate packages?
- 7. pip provisioning and environment creation performance
- 8. Is activation different?
- 9. Practical engineering decision: which should you choose?
- Scenario A: a new Python application
- Scenario B: a test automation system
- Scenario C: a project with an established toolchain
- Scenario D: a project needs another Python version
- 10. Can you switch from one tool to the other?
- 11. Common mistakes
- 12. Frequently asked questions
- Is virtualenv better than venv?
- Does venv require installing virtualenv?
- Can both tools create environments for the same Python version?
- Does virtualenv run Python programs faster?
- Is Conda the same as virtualenv?
- Interview-ready answer
- Key takeaways
- Official references
venv is the standard-library module for creating Python virtual environments, whereas virtualenv is a separately installed Python tool that provides additional environment-creation and interpreter-discovery features. Both can create isolated project environments with their own package installation directories.
For most new projects using a supported Python 3 release, start with venv. Choose virtualenv when its additional capabilities are useful to your workflow, when an existing toolchain depends on it, or when you need more control over environment creation.
Neither tool is a complete operating-system sandbox, a dependency resolver by itself or a general Python-version installer.
1. venv vs virtualenv at a glance#
| Feature | venv | virtualenv |
|---|---|---|
| Distribution | Python standard library since Python 3.3 | Separately distributed third-party package |
| Installation | Normally available with Python 3; some OS distributions package support separately | Install virtualenv into a suitable tooling environment |
| Typical invocation | python -m venv .venv | python -m virtualenv .venv |
| Python selection | Run venv using the desired installed interpreter | Can discover or select an installed interpreter using --python |
| Package isolation | Yes, by default | Yes, by default |
| pip provisioning | Normally bootstrapped using ensurepip | Uses configurable seeding mechanisms |
| Environment creation controls | Standard options such as --without-pip and --system-site-packages | Additional interpreter-discovery, creator, seeding and activation options |
| Creation speed | Depends on the interpreter, filesystem and provisioning work | Can benefit from cached seeding resources; benchmark the actual workflow |
| Typical use | Most ordinary Python 3 projects | Workflows that need its extra capabilities or already rely on it |
The third-party tool's supported interpreter versions, options and defaults can change between releases. Check the documentation for the version you install rather than assuming that current virtualenv supports every historical Python release.
2. What does venv actually do?#
The venv module implements Python's standard mechanism for creating a lightweight virtual environment, introduced through PEP 405.
It creates a directory containing an environment-specific interpreter executable or link, configuration in pyvenv.cfg, an environment package directory and shell activation scripts. By default it bootstraps pip when the required support is available.
The environment is associated with an existing Python installation. It does not ordinarily contain a fully independent copy of every standard-library file. On many platforms, the environment's executable uses the base installation's standard library while keeping third-party packages separate.
Create a new environment using the intended interpreter:
BASHpython -m venv .venv
On systems where the Python 3 command is python3, use python3 instead. On Windows, an available Python launcher can select a suitable interpreter.
The command does not need to contact a package index for an ordinary creation using the bundled pip bootstrap. Optional operations such as --upgrade-deps may require network access.
3. What does virtualenv do?#
virtualenv is a separate open-source tool that creates environments serving the same general purpose. It predates the standard-library venv module and has evolved independently.
Its architecture separates several aspects of environment creation:
- Discovery: locating an interpreter compatible with the requested target.
- Creation: preparing the environment's executable, configuration and directories.
- Seeding: provisioning initial packages such as pip, according to the tool's options and version.
- Activation: generating scripts for supported shells.
The tool can use cached seeding resources to reduce repeated provisioning work. Its behaviour depends on the installed release, platform, options and cache state.
Do not confuse virtualenv, the package and command, with the general concept of a virtual environment. Both venv and virtualenv create virtual environments.
4. Creating environments with each tool#
Option A: standard-library venv#
From a project directory, run:
BASHpython -m venv .venv
The following commands use Linux/macOS-style paths and the environment executable directly, so shell activation is unnecessary:
BASH.venv/bin/python -c "import sys; print(sys.version_info[:2]); print(sys.prefix != sys.base_prefix)"
Example output, tested using Python 3.13.5:
(3, 13)
TrueThe first line identifies the major and minor Python version. The second indicates that sys.prefix refers to an environment different from the base interpreter's prefix.
On Windows, substitute the environment executable path .venv\Scripts\python.exe. The exact version depends on the interpreter used to create the environment.
Option B: third-party virtualenv#
Install virtualenv using your project's approved tooling workflow. For example, you can create a separate tooling environment instead of modifying system Python:
BASHpython -m venv .tools .tools/bin/python -m pip install virtualenv .tools/bin/python -m virtualenv .venv
These are illustrative Linux/macOS commands for a new project where .venv does not yet exist. Installing the external tool requires access to a trusted package source or an approved local package artifact. For a reproducible team setup, use an approved pinned version of virtualenv instead of leaving its version unspecified.
The .tools environment contains the environment-creation tool. The .venv environment contains the application's Python packages. Keeping those roles separate prevents the tooling package from becoming an unnecessary application dependency.
On Windows, substitute .tools\Scripts\python.exe for the tooling executable.
The virtualenv installation and creation commands were not executed in the offline test environment because the third-party package was unavailable. Consult the linked official virtualenv documentation before applying release-specific options.
5. Selecting the Python version: an important difference#
With venv, the base interpreter is the one that executes the venv module. For example, if Python 3.12 is installed and available as python3.12:
BASHpython3.12 -m venv .venv
With virtualenv, an installed target interpreter can be selected through its interpreter-discovery interface:
BASH.tools/bin/python -m virtualenv --python python3.12 .venv
You can also supply a suitable interpreter path rather than relying on command-name discovery. The target must be installed and supported by the virtualenv version in use.
In the second example, the Python interpreter running virtualenv does not have to be the same installation as the interpreter selected for the resulting application environment, provided the combination is supported.
Neither command downloads Python 3.12 simply because that version is requested. If the interpreter does not exist, install it using a trusted distribution or version-management tool first.
After creation, verify the target environment itself:
BASH.venv/bin/python --version
Check the executable path as well when several installations exist. Interpreter availability and selection are distinct from package isolation.
6. How do both tools isolate packages?#
A typical Python environment can be visualised as follows:
Existing Python installation
|
+--> Project A environment
| +--> Environment-specific package directory
|
+--> Project B environment
+--> Environment-specific package directoryEach project's interpreter normally searches its environment-specific site-packages directory rather than automatically using the base installation's third-party packages.
You can inspect the standard environment configuration using Python:
BASH.venv/bin/python -c 'import pathlib,sys; print((pathlib.Path(sys.prefix)/"pyvenv.cfg").is_file())'
Output in the locally tested venv environment:
TrueThe configuration file provides information used by the environment machinery. Its exact contents vary between tools and releases, so do not assume that the two creators produce byte-for-byte identical environments.
Both tools offer a system-site-packages option that allows access to base-installation packages. This can be useful for a specific workflow but reduces package isolation and can make reproducibility harder.
Environment isolation is not a security boundary. A process can still access files, environment variables and network resources according to its operating-system permissions.
7. pip provisioning and environment creation performance#
The standard venv module normally provisions pip using ensurepip, which bootstraps from components bundled with the Python distribution. It can also create an environment without pip:
BASHpython -m venv --without-pip no-pip no-pip/bin/python -c 'import importlib.util; print(importlib.util.find_spec("pip") is None)'
Output in the locally tested isolated environment:
TrueThe environment can execute Python even without pip. Package installation tooling is a separate concern.
virtualenv has its own configurable seeding machinery. Depending on the tool version and setup, it may reuse cached package resources, which can improve repeated environment creation in development or automated test systems.
It would be misleading to claim a fixed speed advantage. Creation time depends on filesystem performance, interpreter discovery, the creator implementation, the packages seeded, cache state and whether any network access occurs.
When environment creation is a measurable bottleneck, benchmark the two tools on the same machine using equivalent target interpreters and provisioning requirements. Measure cold-cache and warm-cache runs separately, repeat the experiment and compare distributions rather than a single timing. Do not compare an environment created without pip against one that provisions additional packages and attribute the entire difference to the tool.
No comparative performance benchmark is reported here because virtualenv could not be installed in the offline test environment.
8. Is activation different?#
The resulting environments can ordinarily be used with familiar shell activation commands. The specific scripts depend on the shell, creator and platform.
For example, in Bash or Zsh:
BASHsource .venv/bin/activate python -c 'import sys; print(sys.prefix != sys.base_prefix)' deactivate
Output in the tested venv environment:
TrueActivation adjusts the current shell's command resolution. It is not required to use an environment: running .venv/bin/python directly selects it without modifying the shell.
On Windows PowerShell, a typical activation script path is .venv\Scripts\Activate.ps1. If policy prevents execution of an activation script, use the environment's executable directly rather than weakening system security settings.
The existing MachinoAI Python environment guide contains detailed cross-platform creation, activation and troubleshooting instructions. The important distinction in this article is the tool that creates the environment, not a fundamentally different way of running Python once the environment exists.
9. Practical engineering decision: which should you choose?#
Scenario A: a new Python application#
A developer starts a web application with a supported Python 3 installation. The project needs its own dependencies, and the team wants a straightforward setup.
Choose venv. It provides the required isolation without adding a separate environment-creation dependency.
Scenario B: a test automation system#
A test runner needs to create environments repeatedly for several installed interpreters. Environment creation and initial package provisioning account for a meaningful portion of test time.
Consider virtualenv or the environment-creation mechanism supported by the test runner. Its discovery and seeding features may fit the workflow. Measure the benefit rather than assuming that switching tools will improve overall test duration.
Scenario C: a project with an established toolchain#
An existing project already relies on virtualenv through documented development scripts or another tool. Replacing it solely because venv is built in may introduce unnecessary compatibility work.
Keep the established workflow unless a migration provides a concrete maintenance or reliability benefit.
Scenario D: a project needs another Python version#
Neither tool is a general Python installer. Obtain the required interpreter first, then use venv or virtualenv to create an isolated package environment with that interpreter.
10. Can you switch from one tool to the other?#
Yes. Application code normally does not need to know which tool created its environment. However, a safe migration is to recreate the environment rather than converting the existing directory in place.
- Confirm the project's required Python version.
- Preserve the reviewed dependency specification or lockfile.
- Create a new environment in a separate directory using the new tool.
- Install the project's required dependencies from the specification.
- Run the tests using the new environment.
- Update editor and automation configuration to select the new interpreter.
- Remove the old environment only after confirming that it is no longer needed.
Use the project's own dependency installation workflow. A clean environment is not automatically reproducible if the dependencies are unpinned or unavailable.
Check installed dependency consistency using the selected environment's pip:
BASH.venv/bin/python -m pip check
Output in a freshly created local venv:
No broken requirements found.This verifies installed-package dependency metadata, not the correctness of application logic. Automated application tests are still necessary.
11. Common mistakes#
Installing virtualenv globally just to create a project environment. If venv already satisfies the requirement, do not add another tool. When virtualenv is needed, install it through a suitable tooling environment or approved package-management workflow.
Assuming virtualenv installs arbitrary Python versions. It discovers or selects available interpreters. Install the target Python separately if necessary.
Treating environment creation as dependency locking. Both tools create environments, but reproducibility requires an appropriate dependency specification and installation process.
Installing packages into the wrong interpreter. Invoke pip through the intended Python executable, for example .venv/bin/python -m pip, and check sys.executable.
Copying or committing an environment directory. Environment files may contain machine-specific paths, interpreter links and platform-specific packages. Commit project source and dependency metadata; recreate environments on each target machine.
Treating an isolated environment as protection from untrusted code. Package isolation does not prevent malicious software from accessing resources available to the current user.
12. Frequently asked questions#
Is virtualenv better than venv?#
Not universally. venv is a good default for most Python 3 projects. virtualenv is useful when its additional environment-creation capabilities justify the external dependency.
Does venv require installing virtualenv?#
No. venv is part of the Python standard library. Some operating-system distributions require installing the appropriate venv support package separately.
Can both tools create environments for the same Python version?#
Yes, when that interpreter is installed and supported by the relevant tool. The two resulting environments can still have different initial package versions or configuration.
Does virtualenv run Python programs faster?#
Not inherently. Environment creation performance and application runtime performance are different. With equivalent interpreters and dependencies, the choice of creator alone does not generally determine application execution speed.
Is Conda the same as virtualenv?#
No. Conda manages environments and packages and can also manage Python and non-Python runtime dependencies. virtualenv is primarily an isolated Python environment-creation tool. Choose the workflow that fits the project.
Interview-ready answer#
venv is Python's built-in virtual environment module, available since Python 3.3. virtualenv is an external tool with additional capabilities such as configurable interpreter discovery, environment creation and package seeding. Both isolate project-level Python packages and provide environment-specific executables. venv uses the interpreter that invokes it, while virtualenv can select another supported installed interpreter through its --python option. For most modern Python projects, venv is sufficient; choose virtualenv when the additional features or existing toolchain provide a real benefit. Neither replaces a dependency-management or Python-version installation workflow.
Key takeaways#
- venv is the standard-library default; virtualenv is a third-party alternative.
- Both create isolated Python package environments based on an existing interpreter.
- Their meaningful differences concern tool installation, interpreter discovery, provisioning and creation controls.
- Compare performance under equivalent conditions rather than assuming a universal advantage.
- Use a documented dependency workflow and recreate environments rather than copying them.
Official references#
- Python 3.13 documentation — venv
- PEP 405 — Python Virtual Environments
- Python documentation — ensurepip
- virtualenv documentation — User Guide
- virtualenv documentation — CLI interface
- Python Packaging User Guide — Install packages in a virtual environment
Verification note: The standard-library venv workflow, interpreter and prefix checks, pip availability, pyvenv.cfg presence, --without-pip creation, activation/deactivation and pip check were tested locally on Python 3.13.5 under Linux. The external virtualenv package was not available, so its installation and creation commands are documented examples, not locally executed results. Windows commands and live documentation were not verified under the authorised offline research exception.
References
- 1.Python 3.13 documentation — venvdocs.python.org
- 2.PEP 405 — Python Virtual Environmentspeps.python.org
- 3.Python documentation — ensurepipdocs.python.org
- 4.virtualenv documentation — User Guidevirtualenv.pypa.io
- 5.virtualenv documentation — CLI interfacevirtualenv.pypa.io
- 6.Python Packaging User Guide — Install packages in a virtual environmentpackaging.python.org
Related questions
Explore other topics:Forward Deployed EngineeringMachine LearningSQL: Structured Query LanguageAI System Design