Tutorials Logic, IN info@tutorialslogic.com

Python Virtual Environments: Isolated Project Setup

Virtual Environments

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.

Environment Folder

The venv module creates a folder that contains a project-specific Python interpreter and package directory.

  • Create the environment inside the project folder.
  • Do not commit the environment folder to version control.
  • Recreate it from a requirements file when needed.

Create venv

Create venv
python -m venv .venv

The command creates a .venv folder for the current project.

Install Packages

After activation, pip installs packages into the active environment instead of the global Python installation.

  • Activate the environment before installing.
  • Freeze dependencies when the project needs to be shared.
  • Install from requirements.txt on another machine.

Save Dependencies

Save Dependencies
pip install requests
pip freeze > requirements.txt

The freeze file records package versions used by the project.

Activation, Interpreter Selection, and Deactivation

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.

  • Create: python -m venv .venv.
  • Windows PowerShell: .\.venv\Scripts\Activate.ps1.
  • Windows Command Prompt: .venv\Scripts\activate.bat.
  • macOS/Linux shells: source .venv/bin/activate.
  • Verify: python -c "import sys; print(sys.executable)" and python -m pip --version.
  • Leave the environment: deactivate.

Dependency Reproduction and Troubleshooting

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.

  • Add .venv/ to .gitignore; never commit installed packages.
  • Keep development tools separate from runtime dependencies when the project format supports groups.
  • Rebuild from the dependency files in CI to prove the setup is reproducible.
  • Use pip freeze as a snapshot tool, not as a substitute for understanding direct dependencies.
Before you move on

Can You Use Virtual Environments?

5 checks
  • You can explain why global pip installs are risky.
  • You can create and activate a project environment.
  • You can install a package inside the active environment.
  • You can write and reuse a requirements.txt file.
  • You know not to upload .venv as project source code.

venv Decisions

0 of 2 checked

Q1. What problem does a virtual environment solve?

Q2. What should be committed instead of the .venv folder?

Environment Isolation Traps

  • Installing globally by habit

    Activate the project environment first, then run pip install.
  • Committing .venv

    Add the environment folder to .gitignore and commit requirements.txt instead.
  • Using the wrong interpreter

    Check which python or python -m pip so commands point to the active environment.

Try this next

Isolate a Project

0 of 3 completed

  1. Make a folder, create .venv, activate it, install one package, and freeze dependencies.
  2. Delete the environment, recreate it, and install packages from requirements.txt.
  3. Write two lines explaining how venv protects another Python project on the same machine.

Questions About Virtual Environments

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.

Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.