Virtual Environments

In the Python world, virtual environments are a way to keep distinct environments that contain the requirements particular to each application, tool or script that you can be working on. These are very useful when working with environments that require specific libraries to function. Take for example that you are working on a project that is written for Cisco ACI APIC version 1.1j and you are also working in sustaining your project in the release 1.0(2)m. Instead of having to change the version of the REST API, you could simply contain two separate virtual environments that each have different versions of the Cobra SDK with your application.

Virtual environments contain all the requirements for the function of the application, including the version of Python itself that is required. In this document we will cover how to create virtual environments in separate operating systems to help you in setting up an environment that contains all the requirements for Python to develop applications to interface with Cisco ACI and NX-OS.

Apple Macintosh OSX

Virtual environments in Mac are very similar to those in Linux distributions. The first step to install virtual environments is going to be have PIP and SETUPTOOLS. If you have read our document on development environments we cover setting you your computer for this.

Once PIP is installed in the system then we can create a virtual environment for the project that you want to work on.

Install virtualenv

user@learnpy.cisco.com:
pip install virtualenv
Note!

If you have followed the instructions in creating a development environment and you are using homebrew, this command will work. If you are doing something else you might have to run the command as root via the sudo command.

Another tool that will simplify using virutal environments is virtualenvwrapper. This tool makes it easier to work with multiple virtual environments at the same time. Even if you have a single environment, it does simplify activating an environment.

user@learnpy.cisco.com:
pip install virtualenvwrapper

Before you can utilize virtualenv wrapper, you have to define some parameters. On your Mac we need to change the file .profile to set an environment variable and then also read the virtualenv wrapper shell script that was installed.

For this case I have shown two configuraiton options. The first WORKON_HOME is the place that you wish to place the environments that you wish to work on. The second line points to the SHELL script that sets the environment variables.

The two lines that you have to add are:

user@learnpy.cisco.com:

export WORKON_HOME=~/Sites
source /usr/local/bin/virtualenvwrapper.sh

You can use any editor you wish or you can append the two lines with the following command:

user@learnpy.cisco.com:
cat <<EOF >> ~/.profile
export WORKON_HOME=~/Envs
export PROJECT_HOME=~/Sites
source /usr/local/bin/virtualenvwrapper.sh
EOF

Once completed you can close your Terminal window and restart so that it re-reads profile or you can just execute that with . .profile. If you issue the command env on your mac, it will show now a set of environment variables around virtualenv wrapper to work on.

Note!

I have done the structure such that the projects home and the environments home are in separate locations. This will simplify your repository work ( e.g. GIT ) going forward. You can do it such that they are all in the same location if you wish.

Creating a Virtual Environment

There are two ways that you can create the environment. As a project or as a virtual environment. One is set such that

user@learnpy.cisco.com:

mkvirtualenv project1

When this command has completed then you can see that is has created the virtual environment under ~/Envs. Your prompt will include a ( project1 ) to indicate you are running inside that virtual environment. And finally you can see that the python interpreter for the virtual environemt is now located under the Envs directory.

$ mkvirtualenv project1
New python executable in project1/bin/python2.7
Also creating executable in project1/bin/python
Installing setuptools, pip, wheel...done.
(project1) $ ls Envs/
get_env_details		preactivate
initialize		predeactivate
postactivate		premkproject
postdeactivate		premkvirtualenv
postmkproject		prermvirtualenv
postmkvirtualenv	project1
postrmvirtualenv
(project1)$
(project1)$ which python
~/Envs/project1/bin/python
user@learnpy.cisco.com:

mkproject project2

When you use the command mkproject, virtualenv wrapper goes a little further and does some additional work for you. It also creates a directory under the previously defined project directory and then makes changes the directory when invoked via the command workon

$ mkproject project2
New python executable in project2/bin/python2.7
Also creating executable in project2/bin/python
Installing setuptools, pip, wheel...done.
Creating ~/Sites/project2
Setting project for project2 to ~/Sites/project2
(project2) project2 $ ls ~/Envs/
get_env_details		preactivate
initialize		predeactivate
postactivate		premkproject
postdeactivate		premkvirtualenv
postmkproject		prermvirtualenv
postmkvirtualenv	project1
postrmvirtualenv	project2

The two commands that are important for this are going to be workon and deactivate. You can switch between different work environments with workon

user@learnpy.cisco.com:

workon project1

And switch back and forth between them easily.

(project2) rmuller$ workon project1
(project1) rmuller$ workon project2
(project2) rmuller$

If you wish to exit a virtualenvironment you can do so with the deactivate command

.
(project2)$ deactivate
$

Installing python packages

Now that you have a virtual environment, you can install software in these that will be placed inside the virtual environments and not in the system structure. Let's take the example to install Flask inside a virtual environment.

user@learnpy.cisco.com:

workon project2
pip install flask

And now flask is installed in the virtualenvironment of project2. If you want to validate this take the following file contents and paste into a file called myfirstflask.py in the project2 directory.

user@learnpy.cisco.com:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    app.run()

And then execute the code with:

user@learnpy.cisco.com:

python myfirstflask.py

Python should return back with the following:

$ python myfirstflask.py
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

You can point your browser to that URL and get back a working web application.