What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Use Python’s built-in venv module to give each project its own environment for installed packages. From the project folder, run python -m venv .venv, activate it if you want shell convenience, and install packages with python -m pip. This keeps project dependencies separate from your base Python installation and from other environments by default.

Why use a virtual environment?

A virtual environment is a project-specific Python installation context with its own package directory. Packages installed there are isolated from the base Python installation and other environments by default. That separation helps prevent one project’s dependency choices from disrupting another’s.

Python includes venv in its standard library, so you can start without installing a separate environment tool. The steps below use .venv, a common name for the environment directory inside a project. The Python documentation describes the module at venv — Creation of virtual environments.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Create an environment in your project

  1. Open a terminal and change to the project directory.

  2. Run:

    python -m venv .venv

The interpreter selected by python creates the environment. If that command does not select the Python installation or version you intend, use the appropriate launcher or version-specific command for your system. The new directory contains the environment’s Python executable and package-related files. To target a particular Python version, create the environment with that version’s interpreter. See the Python tutorial on virtual environments and packages.

Python’s venv bootstraps pip by default; --without-pip disables that behavior. Avoid --system-site-packages for the usual isolated beginner setup, because it allows the environment to access packages from the base installation.

Activate it, or call its Python directly

Activation is optional. It adjusts the shell’s command lookup so that typing python or running installed scripts uses the environment. The exact activation command depends on your operating system and shell.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Shell Activation command
Unix-like shell, such as bash source .venv/bin/activate
Windows Command Prompt .venvScriptsactivate.bat
Windows PowerShell .venvScriptsActivate.ps1

Other Unix shells, including fish and csh, use their own activation scripts. After activation, the prompt commonly displays the environment name. Activation changes shell command lookup by prepending environment-specific commands to PATH; it does not change PYTHONPATH. If an incompatible PYTHONPATH interferes with imports, the Python tutorial recommends unsetting it.

Use an explicit interpreter path instead

You can run the environment’s interpreter without activating it. Python’s venv documentation says: “You don’t specifically need to activate a virtual environment, as you can just specify the full path to that environment’s Python interpreter when invoking Python.” For example:

# Unix-like systems
.venv/bin/python -m pip install requests

# Windows
.venvScriptspython.exe -m pip install requests

Activation is convenient when you will run several commands in the same shell. An explicit path makes the selected interpreter clear and avoids relying on activation or shell state. Both approaches use the same environment.

If PowerShell blocks activation

PowerShell may block the activation script under its execution policy. If your local security policy permits changing it, the Python documentation gives this per-user command:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

Do not treat that change as a required setup step. If you cannot or should not change the policy, use the environment’s explicit Python path instead.

Install packages with the environment’s Python

With the environment active, install a package and inspect what is installed:

python -m pip install requests
python -m pip list

Using python -m pip runs pip through the Python selected by the shell, reducing the risk that a standalone pip command belongs to a different installation. The same rule works without activation when you supply the environment’s full interpreter path. The Python Packaging Authority’s guide to installing packages in a virtual environment covers this workflow and states that its guidance supports Python 3.8 and higher and assumes an official Python distribution. If Python came from an operating-system package manager, make sure the needed Python installation is present.

To check which interpreter a shell will run, use:

python -c "import sys; print(sys.executable)"

When the environment is active, the printed path should be inside .venv. This is a practical verification check; it is not a special venv command.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Record dependencies so you can recreate the environment

For a basic pip workflow, save the environment’s installed package versions to a requirements file:

python -m pip freeze > requirements.txt

Keep requirements.txt with the project when this freeze-based approach suits it. Later, create a fresh environment and install the recorded packages:

python -m venv .venv
# Activate .venv using the command for your shell, then run:
python -m pip install -r requirements.txt

A requirements file records package requirements; it does not turn the existing .venv directory into a portable environment. Python’s documentation advises having a simple way to recreate an environment, such as installing its packages from requirements.txt, and cautions against treating an environment directory as movable.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Deactivate, reset, or remove an environment

When you are done in an activated shell, leave the environment with:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
deactivate

To reset it, deactivate first, remove the project’s .venv directory, create the environment again, and reinstall the project’s dependencies. Do not commit the environment directory to source control or copy it to another location as if it were portable: installed scripts can contain absolute paths to the environment’s interpreter.

When to consider another environment tool

For learning the basics or isolating packages for one project, start with the standard-library venv. A higher-level environment manager may be useful later if you want automatic environment creation or broader dependency-management features. The Python Packaging Authority describes virtualenv as a separately installed alternative with broader Python-version support; it is not required for the workflow in this guide.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.