Python

Create and activate a Python virtual environment

Give each Python project its own private set of packages, so one project never breaks another.

  1. Go to your project folder
    Terminal
    cd my-project

    Use your own folder name.

  2. Create the environment
    Terminal
    python3 -m venv .venv

    This makes a hidden folder called .venv inside your project.

  3. Switch it on
    Terminal
    source .venv/bin/activate

    Your prompt now starts with (.venv). Every pip install now goes into this project only.

Check it worked

  1. Check Python now comes from the environment
    Terminal
    which python

    It should end in .venv/bin/python.

Undo it
  1. Switch it off
    Terminal
    deactivate
  2. Delete the environment for good
    Terminal
    rm -rf .venv

    Your own code is untouched. You can make the environment again whenever you like.

  1. Make sure the venv tool is installedNeeds sudo
    Terminal · Ubuntu / Debian
    sudo apt install -y python3-venv

    Debian and Ubuntu split it out. Other systems usually include it.

  2. Go to your project folder
    Terminal · Ubuntu / Debian
    cd my-project

    Use your own folder name.

  3. Create the environment
    Terminal · Ubuntu / Debian
    python3 -m venv .venv

    This makes a hidden folder called .venv inside your project.

  4. Switch it on
    Terminal · Ubuntu / Debian
    source .venv/bin/activate

    Your prompt now starts with (.venv). Every pip install now goes into this project only.

Check it worked

  1. Check Python now comes from the environment
    Terminal · Ubuntu / Debian
    which python

    It should end in .venv/bin/python.

Undo it
  1. Switch it off
    Terminal · Ubuntu / Debian
    deactivate
  2. Delete the environment for good
    Terminal · Ubuntu / Debian
    rm -rf .venv

    Your own code is untouched. You can make the environment again whenever you like.

  1. Go to your project folder
    PowerShell
    cd my-project

    Use your own folder name.

  2. Create the environment
    PowerShell
    py -m venv .venv

    This makes a hidden folder called .venv inside your project.

  3. Switch it on
    PowerShell
    .venv\Scripts\Activate.ps1

    Your prompt now starts with (.venv). Every pip install now goes into this project only. If PowerShell says scripts are disabled, run the next step once.

  4. Only if scripts are blocked: allow your own scripts
    PowerShell
    Set-ExecutionPolicy -Scope CurrentUser RemoteSigned

    This changes the setting for your user only. Then run the activate step again.

Check it worked

  1. Check Python now comes from the environment
    PowerShell
    where.exe python

    The first line should end in .venv\Scripts\python.exe.

Undo it
  1. Switch it off
    PowerShell
    deactivate
  2. Delete the environment for good
    PowerShell
    Remove-Item -Recurse -Force .venv

    Your own code is untouched. You can make the environment again whenever you like.

New to dependency? I explain it in plain English on Layman Terms.