Virtual Environments – A Python Best Practice

“Be careful who teaches you.”

Following the typical setup instructions for Python could cause unforeseen problems in the long run.  If you’re not careful, you may expose Python and it’s related modules and packages to version changes or upgrades that may be incompatible with your application and cause it to crash.

Creating a virtual environment to isolate your application can prevent these incidental changes or upgrades from unknowingly affecting your application.  A typical Python setup instruction is comprised of the following steps:

  • Download the latest version of Python from Python.org
  • After the download is complete, run and follow the prompts while accepting the defaults to complete your installation.
  • Verify your installation

DON’T STOP THERE!

After you have verified your installation, you will want to set up a virtual environment before starting to code your application.  The reasons for and how to create a virtual environment follow.

Why we need a virtual environment

Upon visiting the Python downloads page, you may have noticed that two (2) versions of Python are available as of this writing:  2.7.13 and 3.6.2.  Some applications require legacy Python versions because the supporting modules have not been ported to support later releases and simply upgrading to a newer or different version of Python could cause some applications to crash.

… NEVER install libraries directly at the system level.  Linux for example relies on Python for many different tasks and operations, and if you fiddle with the system installation of Python, you risk compromising the integrity of the whole system.  … always, always create a virtual environment when you start a new project.

~ Fabrizio Romano, author of “Learning Python” (Packt)

It is possible for both (or many) different versions of Python to co-exist on your machine.  To immunize your Python application from potentially incompatible upgrades or to write code for a specific version of Python, it is a best practice to use Virtual Environments using the virtualenv tool.  More detailed information can be found on the virtualenv web page.

Creating a Virtual Environment

The following instructions pertain specifically to Windows.  If you are running another system (Linux or OSx), visit the Virtualenv web page for more information.

We are presently using Anaconda by Continuum Analytics Inc. where Conda serves as the package and environment manager.  Anaconda is a significantly more advanced and comprehensive Python data science platform used primarily for our larger scale application development purposes.  Refer to the conda Test Drive page to learn how to manage environments using conda.

After you have verified that Python is setup correctly on your machine, from the command line, install virtualenv using the command:

  • pip install virtualenv

PipVirtualenv

As pictured above, the “pip” command will download and install the virtualenv tool on your system.  When complete, use virtualenv to create a virtual environment for your application.  Essentially you are creating an isolated Python subdirectory where your application code will reside.

Before running virtualenv, you may first want to confirm the location(s) of your Python installations using the “where” command.  The picture below shows that three (3) different versions co-exist on the machine used to create this post.

CmdWherePython

Now we can create a subdirectory for our EDI application and setup the virtual environment as pictured below:

EDIVirtualEnv

  • Create a subdirectory for your project or application
    • md ProjectName
  • Change to the new location
    • cd ProjectName
  • Run the virtualenv tool to setup your Virtual Environment
  • virtualenv -p “Your Path\python.exe” YourVirtualEnvironmentName

Before you can start using it, the virtual environment must be activated using the activate command:

  • YourVirtualEnvironmentName\Scripts\activate

To make sure all is working as expected, run Python from the command prompt.  When you are finished, exit Python and simply type “deactivate” to leave the virtual environment as pictured below:

ActivateTestPythonDeactivate

Best Practice Coding

Use this setup strategy to protect your application from incidental and potentially incompatible python or package version upgrades.  If you are new to Python it is easier to make a habit of using virtual environments now, before you start coding, as opposed to later when you begin working on multiple projects.

Anaconda3 4.4.0Virtual environments make it easy to create Python and package specific versions of your application.  If your Python efforts are more ambitious and you are looking for a more comprehensive Python platform, consider downloading the latest version of Anaconda (Anaconda3 4.4.0 as of this writing) where you will also discover how easy it is to create and work with virtual environments using conda.

Learning Python” by Fabrizio Romano (Packt) is one of many excellent books for those looking to get started and learn how to program with Python.  This book serves as a good introduction to the Python language and quickly moves to more practical and advanced topics.  To get your copy visit the “Learning Python” page.

Until Next Time,

vergence-logo-newsquare

Related Resources

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s