How to create virtual env with Python 3?

Learn how to create virtual env with python 3? with practical examples, diagrams, and best practices. Covers python, python-3.x, virtualenv development techniques with visual explanations.

Mastering Python Virtual Environments with venv

Hero image for How to create virtual env with Python 3?

Learn how to create, activate, and manage isolated Python environments using the built-in venv module for clean and reproducible project dependencies.

Python virtual environments are essential for managing project dependencies. They create isolated spaces for your Python projects, preventing conflicts between different package versions required by various applications. This article will guide you through using Python 3's built-in venv module to set up and manage these environments effectively.

Why Use Virtual Environments?

Imagine you're working on two Python projects. Project A requires Django==2.2 and requests==2.20.0, while Project B needs Django==3.0 and requests==2.25.0. Without virtual environments, installing these packages globally would lead to version conflicts, potentially breaking one or both projects. Virtual environments solve this by allowing each project to have its own set of installed packages, completely separate from the global Python installation and other projects.

flowchart TD
    A[Global Python Installation] --> B{Project A}
    A --> C{Project B}
    B -- Requires --> D[Django 2.2, Requests 2.20]
    C -- Requires --> E[Django 3.0, Requests 2.25]
    subgraph Virtual Environments
        B -- Isolated From --> C
    end
    D -- Installed In --> B
    E -- Installed In --> C

How virtual environments isolate project dependencies.

Creating a Virtual Environment

Python 3.3 and later include the venv module as part of the standard library, making it easy to create virtual environments without installing any external tools. The process involves navigating to your project directory and running a simple command.

1. Navigate to your project directory

Open your terminal or command prompt and change the directory to your project's root folder. If you don't have one, create a new directory for your project.

2. Create the virtual environment

Run the command python3 -m venv myenv. Replace myenv with your desired name for the virtual environment (e.g., venv, .venv, env). It's common practice to name it venv or .venv.

3. Activate the virtual environment

After creation, you need to activate it. The activation command varies slightly depending on your operating system and shell.

Linux/macOS (Bash/Zsh)

source myenv/bin/activate

Windows (Command Prompt)

myenv\Scripts\activate.bat

Windows (PowerShell)

myenv\Scripts\Activate.ps1

Once activated, your terminal prompt will usually change to indicate that you are inside the virtual environment (e.g., (myenv) user@host:~/myproject$). Now, any Python packages you install using pip will be installed into this isolated environment.

Deactivating and Deleting a Virtual Environment

When you're done working on a project, you can easily deactivate the virtual environment. If you no longer need the environment, deleting it is as simple as removing its directory.

deactivate

Deactivating a virtual environment

To delete a virtual environment, simply remove the directory you created (e.g., myenv). Make sure you have deactivated it first.

rm -rf myenv  # Linux/macOS
rd /s /q myenv # Windows Command Prompt

Deleting a virtual environment directory