Python Virtual Environment
We should all know about virtualization in the field of computers. A virtual environment makes an isolated clone of a certain system or installation of a package or module or software in a computer. In the case of Python Virtual Environment, an isolated python installation is created that does not interfere with the core python installation of your computer.
In this tutorial we will learn, how can we create Python virtual environments in different ways, that help you to make sense of how does it work.
Grasp more: Python virtual environment in-depth case studies.
We can create Python virtual environments by installing a package from the pip repository. The package allows us some CLI commands for creating Virtual Environments. Run:
$ pip install virtualenv
You may get an error even after installing the virtual environment on your Linux machine, if any error occurs then run again:
$ sudo apt install virtualenv
Now check if the package has been installed or not;
$ virtualenv --version
If the version of the virtual environment is returned, that means the installation is successful.
Creating Virtual Environment using
Way-1: cd the folder where you want to create the virtual container of or virtual environment. Now run:
The above command will create a virtual environment in the current folder because we have specified the path as . (dot) which means the current folder.
Or you can specify the path where you want to install:
$ virtualenv venv #or full path $ virtualenv /usr/mehrab/Desktop/project/venv
All the above commands will create a python virtual environment in the specified path. But by default, it will create the snap of Python version 2. If you want to create a snap of Python version 3, run;
$ virtualenv -p python3 . # or similary specifying the path $ virtualenv -p python3 venv #or $ virtualenv -p python3 /usr/mehrab/Desktop/project/venv
The above commands will create a container of python version3. Now after installing the Virtual Environment, you have to activate it. You may have one or more virtual environments in different directories you specify.
Linux Users: To activate a specific virtual environment run:
$ source path/to/virtual-environment/bin/activate. Or for keeping it simple, go to the virtual environment directory, then run:
$ source bin/activate
Windows Users: Go to the virtual environment directory, then run:
Now you are ready to use your Virtual Environment;
Any time you want to deactivate the environment and get back to core python installation, just type:
Now the environment should be deactivated.
Using the Python3 Core Module
Python2 and Python3 are two major releases of Python. In python3 you can create virtual environments without installing the virtualenv module. But in python2 you can’t.
To create a virtual environment using python3 core, run:
$ python3 -m venv projects
The above command will create a folder called projects if already not exist. Then another folder called venv will be created. And in the venv folder, the virtual environment will be created. To activate and deactivate, follow the same approach mentioned above.
N:B: In the above creation of the virtual environment, the working directory will be ‘projects’ folder. And the python virtual environment installation folder is ‘venv’.