Creating Virtual Environment in Python — Without disturbing Conda Configuration

What they are and how to use them!

Aashish Chaubey
Analytics Vidhya

--

Photo by Bram Van Oost on Unsplash

Hello everyone! I hope you all are having a good time coding!

Well to tell you a little about myself, I’ve made a “small career shift” from Full Stack Development and trying to pursue Data Science. I’m starting from scratch and want to reach the summit while understanding all the particulars of the subject. During this assimilation, I’d glad if you folks point out any mistake or suggest better ways of doing stuff.

In this article, I am going to talk about:
1. The need for creating Virtual Environments
2. Different libraries for maintaining multiple versions of Virtual Environments
3. Using `pyenv` for the setting multiple environments

Note: I assume that you have Anaconda installed (Although it is not required!). This is just to show how we can keep our Conda configuration intact while playing with multiple versions of packages used in Python (3.x.x+).

I currently have Python 3.7.3 installed on my system which comes with the standard Anaconda installation (which is current 4.7.12 while I write this).
You can download Anaconda from here. I suggest downloading the Python 3.7 version.

Now we are all set, let’s get started!

The need for creating Virtual Environments

Every programmer has different preferences when it comes to different programming environments. Light theme vs dark ones, tabs vs spaces, different font styles, and different library preferences. Well, all those are correct and unique for that project/application he is working on.
Recently, I started with acquiring knowledge in Deep Learning and I came across the mighty Keras library. I’d heard a lot of it in office when my colleagues discussed things about it (Well, getting involved in their talks incited me to pursue this area). To install Keras on my machine, I learned I’d have to downgrade my version of Python, which is standard Python which comes shipped with Anaconda installation — Python 3.7.3 because Keras is not compatible with this version. The best was Python 3.6 which I could have to make it compatible. This is how I started figuring how would I install this version without disturbing my standard configuration of Python 3.7.3 as a lot of my stuff was using this version and I didn’t want to break them, not even by chance!

I quickly started reading about Virtual Environments in Python. Not very much to my surprise, I came across a manifold of libraries, each serving its purpose, its use case, given the large community support that Python has!
There were many, to name a few popular ones:

i. pyenv
ii. virtualenv
iii. venv
iv. pew
v. pipenv

pyenv

Let’s look at one of the ways of creating and using a virtual environment: pyenv — A simple python version management tool.
It allows you to change the global Python version, install multiple Python versions, set an application workbench with a specific Python version and create and manage multiple Python environments. Let’s start!

As mentioned on the GitHub doc of pyenv —

pyenv does…

  • Let you change the global Python version on a per-user basis.
  • Provide support for per-project Python versions.
  • Allow you to override the Python version with an environment variable.
  • Search commands from multiple versions of Python at a time. This may be helpful to test across Python versions with tox.

In contrast with pythonbrew and pythonz, pyenv does not…

  • Depend on Python itself. pyenv was made from pure shell scripts. There is no bootstrap problem with Python.
  • Need to be loaded into your shell. Instead, pyenv’s shim approach works by adding a directory to your $PATH.
  • Manage virtualenv. Of course, you can create virtualenv yourself, or pyenv-virtualenv to automate the process.

Usage

Just like most of the other good CLI packages, pyenv also comes with several good and intuitive commands.

This is the link to the pyenv GitHub page. Let’s take a look at some of its useful commands.

Install the package:

There can be many different ways to install the CLI on your systems but I am writing down the easiest one on different commonly used OS.

  • Using brew to install on the Mac systems: `brew install pyenv`
  • Installing on Ubuntu Systems:

— Install the dependencies:

sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \
libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \
xz-utils tk-dev libffi-dev liblzma-dev

— Install pyenv CLI:

curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash
  • Installing it on the Windows Systems:

— Check this package pyenv-win here, it's updated and seems stable, I haven’t personally tried it though since I predominantly work on Linux Systems.

pip install pyenv-win

Guys trying out, please let me know your experience how it goes and I would also love to know your thoughts.

Install a new version of python:

pyenv sits on top of the System Python which can be accessed from the pyenv shell . It is installed in the home directory, mine is installed here

/Users/aashish/.pyenv

All the versions of the Python which we install are in fact installed in the versions folder created within that directory.

# To List out the Python versions$ pyenv versions
* system (set by /home/aashish/.pyenv/version)
2.7.15
3.6.8

To change the global settings:

# Change the global settings of Python from pyenv$ pyenv global 3.6.8
system
2.7.15
* 3.6.8 (set by /home/aashish/.pyenv/version)

To create local settings of Python, say you want that specifically to try out in an unusual environment:

# Change the local settings$ pyenv local 2.7.15
$ python -V
system
* 2.7.15 (set by /home/aashish/.python-version)
3.6.8
# This is also applied to the subdirectory that you create within the new project folder you are trying this in

Virtual Environments

pyenv makes use of a plugin called pyenv-virtualenv . In my opinion, this plugin simplifies the work of making a virtual environment like nothing else in Python. It manages virtual environments across varying versions of Python.

# pyenv virtualenv <python_version> <environment_name>
# <environment_name>: name of the seperate virtual environments
$ pyenv virtualenv 3.6.8 flask-demo

In order to activate your environment

$ pyenv local flask-demo

If you did not configure eval "$(pyenv virtualenv-init -)" to run in your shell, you can manually activate/deactivate your Python versions with this:

$ pyenv activate flask-demo
$ pyenv deactivate

Further Reading

If this didn’t satisfy your curiosity, and you still want to know more about the virtual environment, I highly recommend this article.

Other than that, keep exploring and feel free to put any suggestions in comments to improve the article for the others.

And finally the most essential and helpful article I found while improving my understanding of the subject

Follow Me

Guys, if you liked reading this article and want to read more and follow my journey along into being a well informed Data Scientist, please follow me here on Medium, Twitter and LinkedIn. Also please do not forget to leave your appreciation or comments for this article!

--

--

Aashish Chaubey
Analytics Vidhya

Data Science and Machine Learning enthusiast || Passionate Computer Science Engineer || Love Analyzing || https://aashishchaubey.com