© Real Python

Virtual Environments for Python

Fardeen Khan
Code Panthers
Published in
3 min readSep 15, 2019

--

Managing packages for all the many Python projects you have has never been easier with Virtual Environments: Avoid conflicting dependencies and isolate your project’s need for a cleaner build. In this article, let's explore the bid on virtual envs, basic installation, and some popular tools based on it.

Packages and Libraries

For all people who have used Python or any other programming language in general, the value of libraries and packages or modules, whatever they call it, is very important. Because not everyone one of us likes to write code from scratch when a hundred other people might have done so in a very pristine fashion.

© Fusion Charts

Python packages are one such great example. Imagine yourself writing code to interact with some API on the net and parsing the result and displaying it as GUI to the client, all without any packages. Sure it can be done like that, but will you do that?

So what with this virtual stuff?

So packages have different versions and while you might be working on different projects you might end with these different versions, you can’t have different files for the same packages in the same folder. That's because packages are generally stored and retrieved from the same directory and there's no default way provided for identifying files for different versions. So this is bound to mess your projects up a lot.

Unless you use a Virtual Environment, which is a tool for you to create and manage dependencies in a completely isolated manner. This helps you keep different versions of the same packages, or use conflicting ones but in different environments. That's all possible so quickly because they are just stored and retrieved from different directories with your choice of version for the python interpreter.

Less talking, More doing

To create a Virtual Environment in python, we use a package called virtualenv that we can install through the pip command:

pip install virtualenv

Note: If you dont have Python installed on your system headover to this link and download (v3.7 is recommended as of writing this article). The pip command will be available with it too. If you still have an issue, you can refer to this other article

The next step is to create a virtual environment. So open the terminal or cmd and set its path to the folder of your choice where you will mostly keep your project. You can do this elsewhere too:

virtualenv env-name

The output is somewhat like this

Using base prefix ‘c:\\programdata\\anaconda3’
No LICENSE.txt / LICENSE found in source
New python executable in C:\Users\khanf\OneDrive\Desktop\test\virt\Scripts\python.exe
Installing setuptools, pip, wheel…
done.

So it setups a new python interpreter, the same on your system in this new folder along with some pre-specified packages.

Now we have to activate it, which means we will use this version of python and pip tool rather than the default one:

For Linux and macOS

source venv/bin/activate

For Windows

env-name\Scripts\activate

Now you can see the name of your virtual env appearing on the left of your terminal/cmd:

(env-name) C:\Users\khanf\OneDrive\Desktop\test>

Now you are fully capable of using the pip command and installing whatever packages you want as all packages and changes are made. At the same time, this environment is active and will be limited to it only. And yeah you can create any no of these too.

Deactivating a virtual environment is simple. Just type deactivate:

deactivate

There is another package called virtualenvwrapper which is a helper in managing all these environments created using virtualenv, but that's for another article.

Thanks for reading this.

--

--