A virtual environment is a private Python workspace for one project.
It keeps installed packages away from other projects so version changes do not break unrelated code.
Use a virtual environment before installing packages for scripts, web apps, automation projects, or data tasks.
The venv module creates a folder that contains a project-specific Python interpreter and package directory.
python -m venv .venv
The command creates a .venv folder for the current project.
After activation, pip installs packages into the active environment instead of the global Python installation.
pip install requests
pip freeze > requirements.txt
The freeze file records package versions used by the project.
A virtual environment is a directory containing an environment-specific Python executable and package location. Activation only adjusts the current shell so python and pip resolve to that environment; scripts and CI can call the environment interpreter directly without activation.
Create the environment with the Python version the project supports. Verify both the executable path and pip binding before installation. Deactivate to restore the shell, and delete and recreate the directory instead of moving it between machines or committing it to source control.
The environment directory is disposable; the dependency declaration is the durable artifact. Applications should record direct dependencies and use a lock or constraints workflow that captures resolved versions. Libraries normally declare compatible ranges and test against every supported Python version.
If activation appears to work but packages install globally, compare where python and pip resolve and switch to python -m pip. If venv is unavailable on a Linux distribution, install that distribution's Python venv package. If the base interpreter changes, recreate the environment instead of trying to repair its embedded paths.
0 of 2 checked
Try this next
0 of 3 completed
Use one for any project that installs packages. Very small scripts that only use the standard library may not need one, but using venv is still a safe habit.
No. It is a generated local folder. Keep project files and requirements.txt, then recreate the environment when needed.
python -m pip is safer when you want pip to run with the exact Python interpreter currently selected.
Explore 500+ free tutorials across 20+ languages and frameworks.