Explore Topics

Python Virtual Environment - venv

Last Updated : 18 Apr, 2025 - Asked By Ashok

python  virtual environment 

The virtual environment venv module (Lib/venv/) supports creating "virtual environments", independent packages installed in a separate directory. A virtual environment is created on top of an existing Python installation (Virtual Environments base).

 

Why Virtual Environment:

A Python virtual environment is very important. It prevents package version or Dependency conflicts between the projects

Suppose there are 2 projects: Project A and Project B. Project A needs version 1 package, and Project B needs version 2 package. If projects A and B are running without a virtual environment, there may be a possibility of conflict between the 2 versions of the project. It allows testing with different versions of projects

 

How to install a Python virtual environment

Python has a built-in venv module to install Python Packages

        python -m venv firstProject

        or

        python3 -m venv firstProject

The first Project folder structure will look like this:

        bin  include  lib  lib64  pyvenv.cfg

 

How to activate a Python virtual environment

To use the virtual environment created, you have to activate it. To activate the virtual environment created, type the following command,

    source firstProject/bin/activate

If the virtual environment is activated, you will get a response like this,

    (myfirstproject) izzu@izzumon:~/Documents/fastapi_interview/myfirstproject$

 

Install Packages using PIP

If Python virtual environment is activated, you can install the packages using PIP using the following command,

    pip install

 

To List installed packages inside a Virtual Environment

      pip list

 

How to deactivate a Python virtual environment?

Use the below command to deactivate a Python virtual environment:

     deactivate

When the Python virtual environment is deactivated, installed packages inside the environment cannot be used until activated. But you can reactivate it and use it.

To reactivate the Python virtual environment, use the same command that was used to activate the environment.

 

Comments

Other Resources

Quiz Image
Quiz

Test your knowledge with interactive quizzes.

Interview Questions Image
Interview Questions

Prepare for interviews with curated question sets.

Q&A Image
Q&A

Ask your coding-related doubts and get answers.

Certification Image
Certification

Earn certifications to enhance your resume.

internships Image
Internships

Hands-on projects to improve your skills.

Quiz Image
Quiz

Test your knowledge with interactive quizzes.

Interview Questions Image
Interview Questions

Prepare for interviews with curated question sets.

blog Image
Blogs

Add your technical blogs and read technical topics.

Certification Image
Certification

Earn certifications to enhance your resume.

Q&A Image
Q&A

Hands-on projects to improve your skills.