Skip to content

What is and Why to use a Python Virtual Environment

One day, a group of friends were having an interesting discussion over a beer, in one of the city`s most popular pub, known for its tasty craft beer and delicious burgers.They were all having a passion for Python; some were beginners, some intermediate or pros and some with just a lot of curiosity for the domain. I must say…it was a big table with lots of beers and lots of stories. One particular side story got my attention..it was about ”what are those virtual environments and why we should use them as python developers?”. So let me tell you what valuable knowledge got shared thanks to the beers…and of course…the voice volumes rise…

Python comes into multiple versions. For instance, if you upgrade your computer to a new Python version because you need its new features, it might make your older codes not run properly or not run at all. Say what? Yes! Switching from a python version to another might give you a lot of extra work, code to be rewritten, operations to be stopped (e.g.: if you have sites, online ML platforms) and so on.

One way to overcome that is by using virtual environments. So, your computer is the environment that you are using to code, install packages, and additional software for your tasks. Your computer has a global python version and specific python packages that you are using. But if you need two or three python versions, you can just keep one on your laptop, another one in your colleague’s computer and so on. Well…it is possible but not effective at all.

The solution for this is to create virtual environments into your own computer or laptop. In one environment, you keep Version 2.7 of Python that is running a Script A based on Python 2.7 and in another environment you keep Python 3.6 on which your 2.7 Python Script A is not running but you need the newer version for a new Script B. In the end you can spend time to convert all your code or use version control packages, but if you have a deadline to meet and working on different versions of Python script that needs to be updated at the same time, and different version of packages V1 for Project 1 and V2 for Project 2, you’ll use a Python virtual environment to isolate the projects from each other. Sounds like loneliness but it rhymes with effectiveness. 

How is this done? First of all, there are several methods and the most known ones are Virtualenv (pip install Virtualenv) or Anaconda Virtual Environment. The last from the list is the most popular and works great with Windows.

Let’s do it! Assuming you have your anaconda installed we just use the following commands (I used Anaconda Prompt).

1. Create Virtual Environment with a specific Python Version:

conda create -n Milnesium_Venv1 python=3.6

You first wait for the environment to be solved and then accept the installation of some packages that are being offered in the command.

2.Activate the Virtual Environment:

activate Milnesium_Venv1

instead of base the prompt it will display the name of your env

(base) C:\Users\Environments>

( Milnesium_Venv1) C:\Users\Environments>

Now you are in Milnesium_Venv1 and you can install packages that will be used only for the project that you are working on in this environment and it will not affect your other work.

You can pip install anypackage and version of it in this new project. 🙂 In the same way you get rid of the unneeded packages. 

3. To deactivate the environment use command:

deactivate

4. To run your python scripts, if not working directly from the source folder and not using an IDE, you activate the specific env and then run the script.py there and it will work

5. What if you need to create a similar environment as the one that your colleague is using? He has 10 packages installed:

alabaster==0.7.11

anaconda-client==1.7.2

anaconda-navigator==1.9.6

anaconda-project==0.8.2

appdirs==1.4.3

asn1crypto==0.24.0

astroid==2.0.4

astropy==3.0.4

atomicwrites==1.2.1

attrs==18.2.0

Do you have to go through all of them? Pip install one by one? No!

a. You just use below command and all the packages are being stored in the txt file:

pip freeze –local > requirments.txt

b. To get them installed from the txt file into my/ my other environment you just use command:

pip install -r reuirements.txt

Great stuff, so we can use environments to isolate our projects from changes that are necessary in other projects.  We create the environments, activate, deactivate and personalize them as per our needs. To delete them we only use conda remove env -n Milnesium_Venv1.

Hope you enjoyed the article!

Do you find it useful? Are you already working with Python Virtual Environments?Looking forward to hear your views about this!

Florin Jurchis, Founder at Milnesium