Category Archives: Python

{Basic}Creating a virtual environment in python (local development)

(Disclaimer) I need to state upfront that I am new to python programming and would not advice anyone to use any of the stuff I post here in production without doing further research.

This year i have started to play around with python, as part of my learning I have decided to make notes which I will share here on the blog.

I am currently running a linux machine(Ubuntu 14.04 LTS) and I have 2 version of python installed:

  • Python 2.7.6 – Path /usr/bin/python
  • Python 3.4.3 – path /usr/bin/python3

So why use virtual environments in python in the first?

“Have you ever heard of virtualenv ? The chances are that if you are a beginner then you might not have heard about it but if you are a seasoned programmer than it’s a vital part of your toolset. So what is virtualenv really ? Virtualenv is a tool which allows us to make isolated python environments. How does making isolated python environments help us ? Imagine you have an application that needs version 2 of a LibraryBar, but another application requires version 2. How can you use and develop both these applications? If you install everything into /usr/lib/python2.7/site-packages (or whatever your platform’s standard location is), it’s easy to end up in a situation where you unintentionally upgrade an application that shouldn’t be upgraded.” – python tips

Now that we have established why python virtual environments, lets start doing the setup. You will need to install virtualenv through pip if you don’t already have it.

$ pip install virtualenv

Now that we have installed virtualenv lets create our test environment.

lets create a folder called “python_dev” in our home directory:

$ cd ~ # go to home directory
$ mkdir python_dev
$ cd python_dev # go into directory

Let’s create the virtual environment for python3

$ virtualenv -p /usr/bin/python3 .testenv

Alternatively you can create it using this command:

$ virtualenv -p $( which python3 ) .testenv

if you perform an “ls -a” in the directory you will see you newly created environment “.testenv” folder, go ahead and “cd” into the directory you should be able to see the following folders “bin, include,lib “and a file called “pip-selfcheck.json”.

Is our new environment ready to run?
Not quite, we need to activate it.

$ source .testenv/bin/activate

Once you execute, you will notice your terminal changing into something like this “(.testenv) pegasus@machine:~/python_dev$” and if you execute “python” in the terminal you get the following text.

python
Python 3.4.3 (default, Nov 17 2016, 01:08:31)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

There you go, now you have a test environment isolated from your system, you can install all kinds of libraries without worries about affecting the core libraries used by your operating system.

Disclaimer: I am new to python and I am creating these python blog entries as part of my learning curve (Think of it as anecdotes),Please do not use any of the stuff you read here on production without doing further research.