Python Virtual Environments – Why and How ?
You might have heard about Virtual Environments in Python already and may not be using it yet. Or you maybe aware of the advantages, but not sure how to set it up. In this post, we are trying to help you connect the dots.
Why you need Virtual Environments ? How to use it ? Let’s see..
Let’s take an example of 2 Python projects where you have the below requirements.
Project 1: Python version 2.7 and NumPy module version 1.17
Project 2: Python version 3.6 and NumPy module version 1.22
How you are going to achieve this ? And this is just 2 projects, of course you’ll be working on many projects and those will have their own requirements. Virtual Environments is the way..!
———————————————————
Think of Virtual Environments as a individual sections in a wardrobe. You are keeping your dresses in a partition, your spouse’s in a separate partition and kid’s items in another partition. Your kid is a Python project and his/her dresses are the modules/packages required for the project. The partition in wardrobe is the Virtual Environment containing only the required packages without messing with other project’s files. This helps in easy management, as you can have the required packages with respective versions in it’s own Virtual Environment without worrying about the other projects.
Working with Virtual Environments
You’ll need to install virtualenv (pip install virtualenv) if you are using Python 2.x. venv will be pre-installed in Python 3.x. (Let’s consider Python 3.x for the remaining examples)
python -m venv NewEnv
This command will create a New directory named NewEnv and it will have the subdirectories as below..
Scripts – This directory contains all the scripts including activate, deactivate, pip etc…
Include – C headers for the python packages
Lib – Contains the site-packages folder containing all the dependencies
Now you have created the environment. You have to activate the environment for using it. You’ll have the activate (batch) script inside the scripts folder.
C:\Users\bforum\test>NewEnv\Scripts\activate.bat
(NewEnv) C:\Users\bforum\test>
———————————————————
As you can see in the above example, we have run activation for NewEnv and the next line indicates we are in ‘NewEnv’.
When I do a pip list now, it is listing only pip and setup tools in this enviroment.
(NewEnv) C:\Users\bforum\test> pip list
Package Version
———- ——-
pip 19.2.3
setuptools 41.2.0
(NewEnv) C:\Users\bforum\test>
Now I can use pip install ModuleName==Version to install the specific version of modules in this environment. I just installed NumPy and Pandas (and their dependencies). Now the output looks like below.
(NewEnv) C:\Users\bforum\test> pip list
Package Version
————— ——-
numpy 1.22.0
pandas 1.4.1
pip 19.2.3
python-dateutil 2.8.2
pytz 2021.3
setuptools 41.2.0
six 1.16.0
(NewEnv) C:\Users\bforum\test>
———————————————————
You can deactivate the Virtual Environment to come out of it using deactivate (shell) script.
(NewEnv) C:\Users\bforum\test>NewEnv\Scripts\deactivate.bat
C:\Users\bforum\test>
That’s it in this post, hope this helped you. Comments section is open for any feedback/questions.
Hi ,
I could be even better if you give details on how to bundle the requirements.tx with the required packages and the source code as a exe. for ecample using pex module we can bundle the source code and its dependencies as a executable file