How to create Virtual Environment in Python
A virtual environment is a tool that helps to keep dependencies required by different projects separate by creating isolated python virtual environments for them. This is one of the most important tools that most of the Python developers use.
So while working on new project always create separate virtual environment.
There are advantages to create separate virtual environment as follows
- Virtual Environment should be used whenever you work on any Python based project
- Virtual Environment creates a folder which contains all the necessary executables to use the packages that a Python project would need
- Easily keep track of dependencies
- If there are multiple projects and with different version of libraries required, You can go for separate virtual environment.
- You can prepare requirement.txt file at the end of development, that will helpful for deployment.
Install Python on local machine
You can download python from its official website https://www.python.org/downloads/
Once the Python installed on your machine, you can use PIP command to install libraries on your environment but before that you need to install libraries regarding virtual environment.
Install Libraries of virtual environment
- base package to mange virtual environment
pip install virtualenv
2. User friendly commands for managing environment
pip install virtualenvwrapper
3. for windows operating system
pip install virtualenvwrapper-win
Once above libraries install on machine, now you can set to create virtual environment.
- Making virtual environment
mkvirtualenv Env_Customer_Data_Analysis
2. To view list of all the virtual env for the current user
lsvirtualenv
3. To work on specific virtual environment
workon Env_Customer_Data_Analysis
4. Install libraries in selected virtual environment
pip install pandas
pip install numpy
5. To exit from virtual environment
deactivate
You will exit from current virtual environment
6. To remove virtual environment
rmvirtualenv Env_Customer_Data_Analysis
7. You can use this environment in jupyter notebook as shown below
It will use libraries installed on this virtual environment
8. How can setup virtual environment in Eclipse for Python development
In eclipse, go to Windows->Preferences -> Python Interepeter
Click on New to add Python interpreters
Click on browse and select below path of Python.exe from specific virtual environment and click open and OK
C:\Users\shedgep\Envs\Env_Customer_Data_Analysis\Scripts
It will add environment libraries path to SYSTEM path and then click OK to set the interpreter
9. Once the development is done, you can create requirement.txt file for dependencies and installed libraries using below command
pip freeze > requirements.txt
It will create file in c:\users\<current user> path
10. You can use requirement.txt file to deploy the specific libraries on other environment using pip
if requirement.txt file contains
numpy==1.19.1
pandas==1.1.0
python-dateutil==2.8.1
pytz==2020.1
six==1.15.0
run below command to deploy the libraries
pip install -r requirement.txt
This is the way you can setup virtual environment in python environment and use with different tools for development.
Cheers!!