Python Virtual Environment
In this tutorial, we will understand Virtual Environment in Python. We will understand the need for a virtual environment and also see how to make use of it in python. We will focus on the steps and commands needed to install the virtual environment in python as well as windows operating system.
What is a Virtual Environment?
Keeping Python user-friendly has been the main idea behind its development. So, just for the ease of developers, a virtual environment has been provided.
The virtual environment is a folder structure that provides the user with everything needed to run an isolated lightweight code.
It also serves as a tool that assists in maintaining dependencies needed in different projects detached from one another. It creates a unique project virtual environment for each project.
It is important to know what dependency is. Dependencies are the software components needed to run the code smoothly without encountering a runtime error.
Dependencies are managed through methods such as venv and pipenv.
Need for Virtual Environment
When a user wants to create two projects on different versions using the Django framework. The virtual environment plays a key role in maintaining the dependencies of both projects.
Python does not apply limitations on the number of a virtual environment to be created. Site packages in python are stored and recovered in the same packages by default. Python is unable to differentiate the versions of the Django framework. It will store the files in the same location. In such instances, the virtual environment plays a crucial role by creating separate environments for both or any number of Django frameworks with different frameworks.
It is a good practice to create a new virtual environment while working on any projects based on python.
How to work with a Virtual Environment
Now, let us understand how a virtual environment can be used in python.
To create a remote python environment, module virtualenv is used.
This module contains the compulsory workable packages needed by a python project.
The following steps are involved to be able to use the virtual environment in Linux as well as Windows operating systems.
For installation in Linux, the $ pip install virtualenv command is used.
To check the version of the installed environment, the $ virtualenv --version command is used.
For installation in Windows, the PS> python -m venv venv command is used.
Here, PS denotes the Power shell prompt in windows.
For the creation of a virtual environment,the $ virtualenv directory_tech command is used.
The directory named directory_tech gets created on running the above command. This is the site where the python package will be installed.
Even the interpreter’s name of the user’s choice can be fed here.
To create a Python 2 virtual environment, the following command is used.
$ virtualenv -p /usr/bin/python2 virtualenv_name
In the windows operating system, the following command is used to create a virtual environment in python version 3.
PS> C:\Users\Name\AppData\Local\Programs\Python\Python3\python -m venv venv
In Python, After the creation of an environment, it needs to be activated. The following command is used to activate.
$ source virtualenv_name/bin/activate
If the environment gets successfully activated, its name will appear on the terminal screen.
In Windows, the following command is used.
Now, the virtual environment is all set to install the dependencies related to the project. It can be installed easily, just like other packages get installed.
The following command is used to install it.
(venv) $ python -m pip install <package-name>
Every python package will be installed in this environment instead of global python site-packages until the user closes the terminal.
In windows, (venv) PS> python -m pip install <package-name> command is used for installation purpose.
After finishing up working with this environment in Linux, one can deactivate through a command (venv) $ deactivate
In windows, the command (venv) PS> deactivate is used to deactivate.
This command brings a user out of the created virtual environment.
To be able to revisit the created environment, one needs to activate the environment again.
In this tutorial, we studied how to set up the virtual environment in Windows and Linux operating systems.