What is this Virtual Environments in Python and why anyone ever needs them?

Pınar Ece Aktan
8 min readJun 4, 2019

--

In this article, first I try to convince all my lazy programmer friends to use virtual environments. Then, I will show how to create virtual environments using conda virtual environment and pip virtualenv. And I will show how to use this created virtualenv as an interpreter in PyCharm and Jupyter.

Grab a beer and enjoy it.🍺

This is a programmer he doesn’t use virtual environments he is tense and uncomfy

For all of you are new to a programming language or Python you may be unfamiliar with the concept of virtual environments and it can be kinda scary to hear what the hell the virtual environment is.

The virtual environment is basically a room open for your specific coding project. Instead of using os-wide defined Python or Python packages, it aims to isolate your Python and its dependent packages from all projects that are hosted by your computer.

In my case, I didn’t wake up in one morning praying God I Solemnly swear for today and for all of the days coming I will use virtual environments. Hell no! I learned it from the hard way when I used two different versions of Tensorflow for two different projects that build upon my base Python. So when I try to run project#1 and upgrade tf, project#2 was down and so on. So that day cost me hours and I was in a huge hurry. And that was the day I started to fully understand and obey the logic of virtual environments.

So to sum up, let us imagine that one of your projects using let's say Numpy 1.16.4 but one another project that was written a few months ago highly depends on a deprecated feature on Numpy 1.15.4. if you try to use Numpy in your base python you will likely fail to run at least one of them. And it will take some painful hours to figure out why (I don’t say it will take hours bc I don’t trust your coding capabilities and I'm a bad-hearted witch I say it bc if you still insist not using virtualenvs its likely that you don’t even crash into the issues with package dependencies). And also Numpy is a package that everyone relies to and its relevantly easy to understand when it comes to the package incompatibility.

So if you open two different virtual environments for this project of yours and install different Numpy packages you are ready to go and likely to write a less error-some code.

Also, there is a thing with the project requirements. As a programmer you aware that your code is not meant to work in your local forever. Your colleagues will use it, your tester friend will test it, maybe the server machine will run it. Let’s imagine you use base python for all of your packages and magically, godly no package incompatibility triggered. What do you think will happen when you try to summarize your requirements by “pip freeze > requirements.txt”. All your packages that ever used by any project whether relevant or irrelevant will be listed there, and any person who wants to run your project had to install all of these irrelevant packages and most likely they will fail to install all. So if you open a virtual environment and use isolated packages for each project, your life will be better, your coders' life will be better, life will be better.

So for now I assume I’ve convinced you to use virtual environments, now it’s time to get our hands dirty.

  1. Create virtual-env using conda

Anaconda is a high level and comfy to use all in one package manager for Python and other programming languages that I mostly use for python. As a programmer, you may use conda python or os defined(usr/bin/python) python it's up to you. As my observations, people use Microsoft Windows and Mac os highly uses anaconda bc it gives more UNIX-like experience and easy to manage. But If I had a Microsoft os I would install ubuntu asap anyways. In my local computer I use conda 4.6.14, so keep that in mind.

So to start with conda environments open up a terminal shell. For mac users just search type ‘terminal’ using command+space, for ubuntu users any shortcut leads you to terminal these days and for windows users, I frankly don’t know but maybe conda navigator has a shell tool.

Type conda env list into terminal, it will list all of your virtual environments created so far.

These are my conda environments, if you never ever created a conda virtual env before you will only see base * /anaconda3 here. And /anaconda3/envs is the path to all created virtual environments in my system.

Now I will create a virtual environment specifying the Python version and its name.

Here I created an environment named env-medium and it will build upon python 3.7.2.

Now, again I will list my environments.

See env-medium is there.

Ok, What if next time I want to install a package into my one specific environment. You should first activate your environment using

“conda activate env-medium”

or in earlier conda versions you could write “source activate env-medium”.

See I was using base python and now I'm using env-medium, look at the left corner where (base) and (env-medium).

Now you created and activated your environment.🎉🍾

2. Create virtualenv using pip virtual-env

Pip is the famous package manager for Python that everyone knows about. It's kinda low-level comparing to conda and inherits much more packages than conda. If you use ubuntu I would highly recommend you to use pip rather than conda, and if you use mac-os you should probably better use conda. But time passes, the user experience gets more alike. Anyways let’s begin.

Unlike conda you can create your virtual environments in hypothetically any-where in your computer. But I highly recommend you to create an envs folder like conda does and put all of your ever-created environments here.

Lets open a shell window and begin.

If you want your environments to be restricted to your user you can create envs folder in /home/username (this is in ubuntu)or /Users/username (this is in macos) in a name with envs. I use mac os so I will create an env folder in /Users/username folder such as:

Now install virtual-env package using pip into your base.

And create your pip virtualenv

Now to list all your environments created by virtual-env you can list your directories under envs/

Now we will activate our environment.

Now that is not alias created for virtualenv yet, we should care for giving right paths when doing “source /my-env/bin/activate”.

Now we created our environment using pip virtual-env 🤩

Also, there is a pip guide for virtual env here.

3. Using conda/pip virtual environment as an interpreter in PyCharm

Now you have created a virtual env and you want to use it as an interpreter. I will show these steps using screenshots.

In pyCharm first few steps are similar whether you use conda or pip virtual-env.

  1. Open pyCharm and go to Preferrences or Settings.
  2. Go to project: your project name, my project name is test.

3. Click Project Interpreter

4. Click add in this 3 points in the rightmost

5. Map your environment as the interpreter.

5.a. Map your conda environment as an interpreter

5.a.1. Now if you want to map your conda environment click “Conda Environment” and choose “Existing” bc you already created it.

5.a.2. Find your environment in your os, if you forget your conda environments path you may simply open a terminal and type “conda env list” and it will appear to you. But mostly conda environments whether under root(“/”) or users’ home directory.

This is a valid path for my environment and yours will be similar too.

5.b. Map your pip virtual env as an interpreter

5.b.1. Now if you want to map your pip virtual environment click “Virtual Environment” and choose “Existing” bc you already created it.

5.a.2. Find your environment in your os, you are likely to remember bc you decided where to install them, we discussed it, remember? It's in this article, you probably used /home/username/envs kinda path.

This is the valid path for me, yours is similar.

Press enter and wait pyCharm to update your indexes.

Indexing

You are all set in pyCharm.

4. Using conda virtual environment as an interpreter in Jupyter.

4.1 Open up anaconda navigator. And click “environments”

4.2 Choose and environment you want to use for jupyter

4.3 Click to the triangle in the same line with your env.

If Open with IPython seems disabled in grayish, you must switch to Home tab in navigator and install Jupyter Notebook.

Here is my env, I already installed it so it says Launch. Once you install jupyter notebook you’re all set. Go back to the environments tab and try again, this time you’ll see Open with IPython in black. Click it and you’re all set.

Congratulations you are using the virtual environments 🥰🎉🍾🥂

This is a convinced programmer, uses virtual envs every time, the head is on clouds, no drama.

--

--

Pınar Ece Aktan

AI Research Engineer, loves Python and writes tech stuff