How to use virtual environment in python

2 mins read

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

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

What is a 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 a virtual environment?

Installing virtual environments for Windows, Mac, and Linux are almost similar. Let’s start one by one.

How to install a virtual environment on Windows, Mac, and Linux?

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

pip install virtualenv

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

virtualenv --version

The above command will print the version of your installed virtual environment. Here is the output from the 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 the command shell with Admin come to that newly created directory and write the command:

virtualenv first

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

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

How to activate a 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 dependency will not be available for other projects.

How to activate a virtual environment on Linux or Mac?

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

How to deactivate a virtual environment?

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


Conclusion

After going through the above steps you will be able to use virtual environments 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