VENV

This information is from: Python Virtual Environments - Full Tutorial for Beginners

Introduction

A virtual environment is a self-contained location that enables you to maintain separate and isolated environments for your Python projects.

Virtual Environments (VEs) are directories on your system.

Advantages are

without conflicts across different projects.

VENV

There are several virtual environments available, but 'venv' is built into Python.

Some other VEs are: Poetry, Python virtualenv, Conda, pipenv.

Using VENV

Note: VENV commands are slightly different for Windows and Linux/Mac.

  1. Navigate to a directory where you want create a virtual environment. You may need to create it. (Use the project's name as the directory name?)

  2. Create a VE

    The convention is to use the name "env" but it is not a requirement.

    • Windows: python -m venv env
      A new directory "env" is created in the current directory. Use 'dir' to verify it's creation.
    • Mac or Linux: python3 -m venv env
      A new directory "env" is created in the current directory. Use 'ls' to verify it's creation.

  3. Activate a VE

    • Windows: .\env\Scripts\activate.bat
    • Mac or Linux: ./source env/bin/activate

    You should see "(env)" before the shell prompt (e.g. (env) <shell prompt>)

  4. Deactivate a VE

    • (env) <shell prompt> deactivate

    This deactivates a VE on Mac,Windows,Linux and returns back to the global Python environment. (Back to the root directory of project?)

  5. Delete a VE

    • Windows: rmdir <project_name> /s
    • Mac or Linux: rm /r <project_name>

Links

venv — Creation of virtual environments (Python Documentation)

Python Tutorial: VENV (Windows) - How to Use Virtual Environments with the Built-In venv Module (YouTube)

Python Tutorial: VENV (Mac & Linux) - How to Use Virtual Environments with the Built-In venv Module (YouTube)

Virtual Environments in Python - Crash Course (YouTube)

Clean New Projects with venv - Virtual Environments (YouTube)