What is virtual environment
A virtual environments is a tool that help us to creating isolated python virtual environments for different projects. Similarly, This tool keep their required dependencies in virtual space. So, you doesn’t need to install project’s dependencies system-wide.
You can have a multiple environments and multiple set of packages without conflicts among them. In addition, Different projects requirements can be complete at the same time.
In this paragraph, I’m going to discuss why we need to use virtual environment. Sometimes, your application will need a specific version of a library because, the application may require that a particular bug has been fixed or the application may be written using an obsolete version of the library’s interface at that time. Virtual environment creates a folder which contains all the necessary packages that a Python project would need.
For instance, If your first application needs version 1.5 of a particular module but your second application needs version 2.3 then the requirements are conflict with each other. So, you need to installing version 1.0 or 2.0 at that point one application unable to run.
In conclusion, you must be create virtual environment for every Python based project you work on. So, the dependencies of every project are isolated from the system and each other.
Get The Virtual Environment.
If you’re not using Python 3, you’ll want to install the
virtualenv tool with
$ pip install virtualenv
If you are using Python 3, then you have already the venv module with the standard library installed.
Check Virtual Environment Version
Following command shows the current version of virtual environment.
$ virtualenv --version
Creating Virtual Environment
Firstly, You can create a virtual environment using the following command:
$ virtualenv my_name
If you want to specify Python interpreter of your choice, ex. Python 3 use the following command:
$ virtualenv -p /usr/bin/python3 virtualenv_name
To create a virtual environment for Python 2.7 use the following command
$ virtualenv -p /usr/bin/python2.7 virtualenv_name
After that, you just need to activate it.
You need to activate virtual environment every time when you work on the project. This can be done using the following command:
$ source virtualenv_name/bin/activate
Or , For windows user use following command.
Now, your virtual environment is activated the name of your virtual environment will appear on left side of terminal that means the virtual environment is currently active. After that, you can install all necessary dependencies of your project in this virtual environment.
If you want to install Django 2.0 for your project, you can install it just like you install other packages.
(virtualenv_name)$ pip install Django==2.0
This Django 2.0 package will be placed in virtualenv_name folder and will be isolated from the complete system.
when you are done with your work, you can deactivate the virtual environment by the following command:
Now you will be back on your default Python installation system.
Virtual environment wrapper provides a set of commands which are use to makes virtual environments much more pleasant and it also places all your virtual environments in one particular place.
Create a virtual environment:
This command will creates the project_folder folder inside ~/Envs.
$ mkvirtualenv project_folder
After creating virtual environment you need to just type Work on:
$ workon project_folder
either, you can make a project, which creates the virtual environment, and also a project directory inside $WORKON_HOME.
$ mkproject project_folder
virtualenvwrapper provides tab-completion on environment names. This is a very useful when you have a lot of environments and have trouble remembering their names.
workon command deactivates environment you are currently in, so you can quickly switch between your both environments.
Deactivate your virtual environment :
If you want to delete the virtual environment use the following command:
$ rmvirtualenv venv
This command will show you list of all the environments.
This command navigate into the directory of the currently activated virtual environment.
Like the above, but directly into site-packages directory. Shows contents of site-packages directory.
We hope you are learning all the things related to our post, Thank you .