How to use virtual environment in python

2 mins read

In this article we will explain how to use virtual environment in python on Windows, Mac and Linux. First let’s understand basic questions regarding virtual environment.

  1. What is virtual environment in python?
  2. Why do we need a virtual environment?
  3. How to install virtual environment?
  4. How to use virtual environment?
  5. How to activate virtual environment?
  6. How to deactivate virtual environment?

What is virtual environment in python?

A virtual environment in Python is a self-contained directory that contains a specific version of the Python interpreter and any necessary libraries and dependencies required by a particular project.

The main purpose of creating a virtual environment is to keep the dependencies required by different projects separate from each other, so that the packages installed for one project do not interfere with the packages installed for another project. This is particularly useful when working on multiple projects that require different versions of the same library or package.

Why do we need a virtual environment?

  1. Isolation of dependencies: Different Python projects may require different versions of the same library or package, which can lead to conflicts between dependencies. By creating a virtual environment for each project, you can isolate the dependencies required by each project and avoid these conflicts.
  2. Reproducibility: Virtual environments allow you to recreate the same environment on different machines, ensuring that the project will run the same way regardless of the machine it’s on. This is particularly useful when working on collaborative projects or when deploying projects to production.
  3. Sandboxing: Virtual environments provide a safe and isolated sandbox for experimenting with new packages or making changes to existing packages. This can help prevent unintended changes to system-wide packages or dependencies.
  4. Easier installation and upgrading of packages: Installing or upgrading packages in a virtual environment does not affect system-wide packages, and can be done without requiring administrative privileges.

Overall, virtual environments help ensure that Python projects are portable, reproducible, and maintainable, while also providing a safe and isolated space for experimentation and development.

How to install virtual environment?

Installing of virtual environment for windows, Mac and Linux are almost similar. Let’s start one by one.

How to install virtual environment on windows, Mac and Linux?

To install virtual environment, please follow the steps one by one:

pip install virtualenv

Now check it’s installed or not, by just checking it’s version:

virtualenv --version

The above command will print the version of your installed virtual environment. Here is output from window and similar output will be there for Mac/Linux:
virtualenv 20.21.0 from C:\Python310\lib\site-packages\virtualenv__init__.py

How to create a virtual environment?

Create a directory. Open command shell with Admin and come to that newly created directory and write command:

virtualenv first

This will create a virtual environment directory in your current location. In our case the name of virtual environment directory here is “first”.

Please note: Till now the steps are same for Windows, Mac and Linux. But now to activate and deactivate the commands will be different.

How to activate virtual environment in windows?

<path_to_virtual_environment>\Scripts\activate

This will print like this:

(first) <path_to_your_directory>

Now you can install all your dependencies for your project. This dependencies will not available for other projects.

How to activate virtual environment on Linux or Mac?

<path_to_virtual_environment>/bin/activate
OR
source <path_to_virtual_environment>/bin/activate

How to deactivate virtual environment?

Just write a command deactivate to exit from your virtual environment. This will remain same for Mac and Linux also.


Conclusion

After going through above steps you will be able to use virtual environment on Windows, Mac and Linux. Virtual environments can be created using built-in tools such as venv, virtualenv, or conda. Once created, a virtual environment can be activated to ensure that all commands executed within that environment use the packages and dependencies installed in that environment.

Back

Dharmendra is a blogger, author, Expert in IT Services and admin of DJTechnews. Good experience in software development. Love to write articles to share knowledge and experience with others. He has deep knowledge of multiple technologies and is always ready to explore new research and developments.

Leave a Comment

Stay Connected with us