Open In App

Python Virtual Environment

Last Updated : 07 Aug, 2025
Comments
Improve
Suggest changes
93 Likes
Like
Report

A virtual environment is an isolated Python environment that allows you to manage dependencies for each project separately. It prevents conflicts between projects and avoids affecting the system-wide Python installation. Tools like venv or virtualenv are commonly used to create them.

Why do we need a Virtual Environment

  • Avoid Dependency Conflicts: Different projects may require different versions of the same library (e.g., Django 4.0 vs 4.1).
  • Isolate Project Environments: Keeps each project’s packages and settings separate from others and from the system Python.
  • Simplifies Project Management: Makes it easier to manage and replicate project-specific setups.
  • Prevents System Interference: Avoids accidentally modifying or breaking the global Python environment.
  • Enables Reproducibility: Ensures consistent behavior across development, testing and deployment environments.

When and Where to use a Virtual Environment?

A virtual environment should be used for every Python project. By default, all Python projects share the same location to store packages. This can cause problems if two projects need different versions of the same package, like Django. A virtual environment solves this by creating a separate space for each project’s packages, so they don’t interfere with each other. It’s basically a folder with its own Python setup. Using a virtual environment helps avoid conflicts and keeps your projects clean and organized.

How to Create a Virtual Environment in Python

We use the virtualenv module to create isolated environments. It creates a folder with all necessary executables for the project.

Step 1: Installing virtualenv

$ pip install virtualenv

Step 2: Check Installation

$ virtualenv --version

Step 3: Create a Virtual Environment

$ virtualenv my_env

This creates a directory named my_env containing the isolated environment. To use a specific Python interpreter (e.g., Python 3):

$ virtualenv -p /usr/bin/python3 my_env

Activating a Virtual Environment in Python

Now after creating a virtual environment, you need to activate it. Remember to activate the relevant virtual environment every time you work on the project. This can be done using the following command:

1. On Windows

To activate virtual environment using windows command prompt change directory to your virtual env, Then use the below command

$ cd <envname>$ Scripts\activate

Note: source is a shell command designed for users running on Linux (or any Posix, but whatever, not Windows).

2. On Linux/macOS

$ source virtualenv_name/bin/activate

Once the virtual environment is activated, the name of your virtual environment will appear on the left side of the terminal.

activate virtual environment in Python

This will let you know that the virtual environment is currently active.

Installing Dependencies in Virtual Environment

Once your virtual environment (e.g., venv) is active, you can install project-specific dependencies inside it.

For example, to install Django 1.9:

(virtualenv_name)$ pip install Django==1.9

This installs Django 1.9 inside the virtualenv_name folder, keeping it isolated from the system Python.

Deactivate Python Virtual Environment

Once you are done with the work, you can deactivate the virtual environment by the following command:

(virtualenv_name)$ deactivate

deactivate virtual environment in Python

This returns you to the system’s default Python environment.

Suggested Quiz
6 Questions

Which of these commands installs the virtualenv tool using pip?

  • A

    pip load virtualenv

  • B

    pip get virtualenv

  • C

    pip install virtualenv

  • D

    install virtualenv pip

Explanation:

The correct command is pip install virtualenv.

How do you create a virtual environment named my_env using virtualenv?

  • A

    python my_env.py

  • B

    virtualenv create my_env

  • C

    virtualenv my_env

  • D

    new virtualenv my_env

Explanation:

virtualenv my_env creates a new isolated environment named my_env.

What command activates a virtual environment on Windows?

  • A

    source my_env/bin/activate

  • B

    Scripts\activate

  • C

    activate my_env.sh

  • D

    run my_env/activate

Explanation:

On Windows, use Scripts\activate from within the virtual environment directory.

After activating a virtual environment, how do you install Django version 1.9 into it?

  • A

    install Django=1.9

  • B

    pip install Django==1.9

  • C

    python get Django 1.9

  • D

    pip add Django:1.9

Explanation:

Use pip install Django==1.9 to install a specific version of a package.

How do you deactivate a virtual environment in Python?

  • A

    exit()

  • B

    deactivate()

  • C

    python deactivate

  • D

    deactivate

Explanation:

Just type deactivate in the terminal to exit the virtual environment.

What is a benefit of using a virtual environment in Python?

  • A

    It slows down the interpreter

  • B

    It merges all system libraries

  • C

    It isolates project dependencies

  • D

    It connects multiple projects together

Explanation:

Virtual environments prevent dependency conflicts by isolating libraries per project.

Quiz Completed Successfully
Your Score :   2/6
Accuracy :  0%
Login to View Explanation
1/6 1/6 < Previous Next >

Article Tags :

Explore