Virtual Environment is to create an python environment which allows to use different python modules & versions, without messing up the system.
let’s understand virtual environment with it’s need in the project development. In the world of python projects it’s obvious to use different python libraries (wrappers) to get our work done, but It won’t be happy ending all the time. Most of the time we face environment issues where our python application won’t run on new machines(computer systems). This is because of the dependence issue of python libraries in the new machine (in which we try to run our python application). For better understanding, suppose in the development face in our python application we have used python pandas (Python data analysis library) version 0.18.0 function which was not there in pandas 0.17.1. In new machine pandas version 0.17.1 we installed. So the python application won’t run on new machine because of the version difference.
To over come this, we need to use an created python environment which contains everything that a Python project (application) needs in order to run in an organised, isolated fashion.
So Using virtual Environment is the recommended way for working with Python projects, regardless of how many you might be busy with.
In ubuntu creating virtual environment is much easy by using virtualenv (a tool to create isolated Python environments)
Virtualenv helps solve project dependency conflicts by creating isolated environments which can contain all the goodies Python programmers need to develop their projects. A virtual environment created using this tool includes a fresh copy of the Python binary itself as well as a copy of the entire Python standard library.
$ sudo pip install virtualenv
As of now we successfully installed virtualenv. Now let’s create a folder (Environment) where we will install python data mining packages .
Create virtual environment:
$ virtualenv dataaspirant_venv
virtualenv dataaspirant_venv will create a folder in the current directory which will contain the Python executable files, and a copy of the pip library which you can use to install other packages. The name of the virtual environment (in this case, it was dataaspirant_venv) can be anything; omitting the name will place the files in the current directory instead.
This creates a copy of Python in whichever directory you ran the command in, placing it in a folder named dataaspirant_venv
Using the virtual environment:
To use the virtual environment, it needs to be activated
$ source dataaspirant_venv/bin/activate
The name of the current virtual environment will now appear on the left of the prompt (e.g. (dataaspirant_venv)Your-Computer:your_project UserName$) to let you know that it’s active. From now on, any package that you install using pip will be placed in the dataaspirant_venv folder, isolated from the global Python installation.
Python Data mining packages Installation:
So let’s start installing data mining python pacakges one by one
Numpy installation Command:
$ pip install numpy
Scipy Installation Command:
$ pip install scipy
Matplotlib Installation Command:
$ pip install matplotlib
Ipython Installation Command:
$ pip install ipython[all]
Pandas Installation Command:
$ pip install pandas
Statsmodel Installation Command:
$ pip install statsmodels
Scikit-learn Installation Command:
$ pip install scikit-learn
Running Script File:
when virtualenv is in active mode you simple go the directiory where your python script file there and run your script file in general way.
$ python script_file.py
Deactivate the virtual environment:
If you done working in the virtual environment for the moment, you can deactivate it.
This puts you back to the system’s default Python interpreter with all its installed libraries.
To delete a virtual environment, just delete its folder. (In this case, it would be rm -rf dataaspirant_venv.)
[ 2 ] https://en.wikipedia.org/wiki/Virtual_environment_software/
I hope you liked todays post. If you have any questions then feel free to comment below. If you want me to write on one specific topic then do tell it to me in the comments below.
If you want share your experience or opinions you can say.