Table of contents
1.
Introduction
2.
What is a Virtual Environment?
3.
Why do we need a virtual environment?
4.
When and where to use a virtual environment?
5.
Create Virtual Environment in Python
6.
Activating a Virtual Environment in Python
6.1.
For Windows
6.2.
For macOS & Linux:
7.
Installing Dependencies in Virtual Environment Python
7.1.
pip install requests
8.
Deactivate Python Virtual Environment
9.
Frequently Asked Questions
9.1.
Can I have multiple virtual environments for the same project?
9.2.
How do I delete a virtual environment?
9.3.
Can I use a virtual environment created on one operating system on another operating system?
10.
Conclusion
Last Updated: Jun 21, 2024
Easy

How to Create a Virtual Environment in Python?

Author Ravi Khorwal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Python is a popular programming language used for various applications, from web development to data analysis. When working on Python projects, it's important to have a clean & organized environment to ensure that dependencies & packages don't conflict with each other. This is where virtual environments could be pretty useful for us. 

How to Create a Virtual Environment in Python?

In this article, we'll learn what virtual environments are, why they are needed, & how to create & use them in Python.

What is a Virtual Environment?

A virtual environment in Python is an isolated environment where you can install packages & dependencies specific to a project without interfering with the global Python installation or other projects. It's like having a separate box for each project, containing its own Python interpreter, libraries, & scripts.

When you create a virtual environment, it creates a new directory that contains a copy of the Python executable, along with some supporting files & directories. This allows you to have different versions of Python & packages for each project, ensuring that they don't conflict with each other.

Why do we need a virtual environment?

Using a virtual environment is important for several reasons:

  1. Dependency Management: Different projects may require different versions of the same package. With virtual environments, you can have separate installations of packages for each project, avoiding conflicts between them.
     
  2. Reproducibility: By using virtual environments, you can easily share your project with others or deploy it to a different machine. The virtual environment ensures that all the necessary dependencies are installed & the project runs smoothly.
     
  3. Isolation: Virtual environments provide isolation from the global Python installation. This means that any changes made within the virtual environment, such as installing or updating packages, won't affect the global Python setup or other projects.
     
  4. Cleaner System: By using virtual environments, you keep your system's global Python installation clean & free from project-specific dependencies. This helps maintain a more organized & manageable Python environment on your machine

When and where to use a virtual environment?

It's recommended to use a virtual environment in the following situations:

  1. Starting a new Python project: Whenever you start a new Python project, it's a good practice to create a virtual environment specifically for that project. This ensures that the project has its own isolated environment with its own dependencies.
     
  2. Working on multiple projects simultaneously: If you have multiple Python projects on your machine, using virtual environments allows you to keep the dependencies & packages separate for each project. This prevents conflicts between different projects' requirements.
     
  3. Collaborating with others: When collaborating with other developers on a project, using a virtual environment ensures that everyone has the same environment setup. It makes it easier to share the project & ensures that it runs consistently across different machines.
     
  4. Deploying a project: When deploying a Python project to a production environment, using a virtual environment helps in managing the project's dependencies. You can easily package the virtual environment along with the project to ensure that all the necessary packages are available in the deployment environment.

Create Virtual Environment in Python

To create a virtual environment in Python, you can use the venv module which comes bundled with Python 3.3 & above. Here's how you can create a virtual environment:

Open a terminal or command prompt & navigate to the directory where you want to create your project.

Run the following command to create a virtual environment:

Python -m venv myenv


Replace myenv with the name you want to give to your virtual environment. This command will create a new directory named myenv in your current directory.

Wait for the command to finish executing. It will create the necessary files & directories inside the myenv directory.

That's it! You have successfully created a virtual environment using Python's venv module.

Activating a Virtual Environment in Python

After creating a virtual environment, you need to activate it to start using it. The activation process varies slightly depending on your operating system.

For Windows

Open a command prompt & navigate to your project directory.

Run the following command:

myenv\Scripts\activate


Replace myenv with the name of your virtual environment.

For macOS & Linux:

Open a terminal & navigate to your project directory.

Run the following command:

source myenv/bin/activate


Replace myenv with the name of your virtual environment.

Once you run the activation command, you will notice that the name of your virtual environment appears in parentheses at the beginning of your command prompt or terminal. 

This indicates that the virtual environment is currently active.

For example:
(myenv) C:\Users\YourName\ProjectDirectory>


Now, any Python commands you run will use the Python interpreter & packages installed within the virtual environment.

Installing Dependencies in Virtual Environment Python

Once you have activated your virtual environment, you can install packages & dependencies specific to your project using pip, the package installer for Python.

To install a package in your virtual environment, simply run the following command:

pip install package_name


Replace package_name with the name of the package you want to install.

For example, let's say you want to install the requests library in your virtual environment. You would run the following command:

pip install requests

pip will download & install the package along with its dependencies within the virtual environment.

You can also install multiple packages at once by separating them with spaces:

pip install package1 package2 package3


It's a good practice to keep track of the packages & their versions used in your project. You can do this by creating a requirements.txt file. To generate a requirements.txt file, run the following command:

pip freeze > requirements.txt


This will create a file named requirements.txt in your current directory, listing all the packages installed in your virtual environment along with their versions.

To install packages from a requirements.txt file, you can use the following command:

pip install -r requirements.txt


This command will install all the packages listed in the requirements.txt file into your virtual environment.

Deactivate Python Virtual Environment

When you're done working on your project or want to switch to a different virtual environment, you can deactivate the currently active virtual environment.

To deactivate a virtual environment, simply run the following command:

deactivate


This command works the same way on all operating systems (Windows, macOS, and Linux).

After running the deactivate command, you will notice that the virtual environment name is no longer displayed in your command prompt or terminal. This indicates that the virtual environment has been deactivated, and you're back to using the global Python installation.

For example:

(myenv) C:\Users\YourName\ProjectDirectory> deactivate
C:\Users\YourName\ProjectDirectory>


Once deactivated, any Python commands you run will use the global Python installation instead of the virtual environment's Python interpreter and packages.

It's important to remember to deactivate your virtual environment when you're finished working on your project to avoid accidentally installing packages or running scripts in the wrong environment.

Note: You can always reactivate the virtual environment by navigating to your project directory and running the activation command specific to your operating system, as mentioned in the previous section.

Frequently Asked Questions

Can I have multiple virtual environments for the same project?

Yes, you can create multiple virtual environments for the same project. This can be useful when you want to test your project with different versions of Python or dependencies.

How do I delete a virtual environment?

To delete a virtual environment, simply delete the directory that was created when you created the virtual environment. For example, if your virtual environment is named "myenv", you can delete it by running the command: rm -rf myenv.

Can I use a virtual environment created on one operating system on another operating system?

No, virtual environments are specific to the operating system and Python version they were created on. If you want to use the same project on a different operating system, you'll need to create a new virtual environment on that system and install the required packages.

Conclusion

In this article, we learned about virtual environments in Python and why they are important for managing project dependencies. We discussed how to create a virtual environment using the venv module, activate and deactivate it, and install packages within the virtual environment using pip. We also discussed the importance of using a requirements.txt file to keep track of project dependencies. With the help of virtual environments, you can ensure that your Python projects have a clean and isolated environment, making it easier to develop, collaborate, and deploy your applications.

You can refer to our guided paths on Code 360. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.

Live masterclass