Python, ArcMap and Virtualenv

Published on July 23, 2017

At version of ArcMap 10.4.1 a number of more interesting Python modules are included such as Pandas. For anyone with an interest in Python the programming environment suddenly becomes interesting in it's own right. Despite ESRI moving in positive direction, leveraging some the Open Source nature of Python it's not a perfect environment. Many of the packages are out of date and given they are part of ArcMap rather than the other way around simply upgrading them in situ.

Though it would be easy to do (pip has also been included as part of the Python installation) this may not be the most appropriate solution for two reasons.

Firstly, upgrading the packages may affect the underlying functions of the application. Secondly, the delivery of the software at my place of work does not expect alteration of the software, which is packaged for delivery and removal and does not expect significant alterations to the software installation - in the case of ArcMap this includes the Python environment.

A compromise which also promotes Python best practise is to use virtualenv. This permits sand-boxing of the Python environment, permitting a user to experiment with updated and additional packages are not included with ArcMap by default.

Assuming pip has been installed and is operating normally (see this post) virtualenv is simple to install and requires no additional dependencies. It's as simple as running:

pip install virtualenv

Once installed it can be seen sitting alongside the other Python modules:

While virtualenv is adequate to create Python virtual environments on their own it is recommeded that the virtualenvwrapper module is installed. A Windows version of virtualenvwrapper-win is advised which can be installed using pip in exactly the same way.

As my requirement was to try to keep the ESRI environment stock I didn't install the virtualenvwrapper-win package alongside virtualenv. Instead having temporarily added virtualenv to the ArcMap Python installation I used it on it's own to create a new virtual environment.


Use pip to install virtualenv in the ArcMap Python environment

cd <ArcMap Python path>\Lib\site-packag

python -m pip install virtualenv

I created a directory called .virtualenv in my User Profile directory before changing direcotory to the virtualenv.exe to create a new virtual environment. NOTE The %USERPROFILE% used below is an envrionment variable that relates to the users profile directory. Typically this environment is preserved where a user changes PC so it's a safe bet to add files here. Furthermore in a locked-down environment where a user may not have rights to change they would usually have rights to make changes to their own profile.


mkdir .virtualenv

Move to the location the virtualenv.exe has been installed

cd <ArcMap Python path>\Scripts

Create a virtualenv (specifically for managing the future creations of subsequent virtual environments). This is explain further below!

>virtualenv %USERPROFILE%\.virtualenv\ve

Having created a virtual environment we can now set this one up to be the main virtual environment to in future create any subsequent virtual environments. The reason I did it this way is because I use virtualenvwrapper-win. This is another, useful Python module that provides additional funcationality for the administration and use of virtualenv. However as it requires a number of dependent modules I didn't want these in the ArcMap Python environment which I wanted to be as stock as possible to ensure that it wouldn't affect the software installation.

cd %USERPROFILE%\.virtualenv\ve


As I have activated the virtual environment I can use the version of pip that comes with it to insure that any installed Python modules are places within it. You can tell the environment has been activated as the command prompt will change to include the name of the virtual environment selected as a prefix ie.


pip install virtualenvwrapper-win

As virtualenvwrapper-win requires dependencies environment variable should be applied correctly and in my case the use of the --proxy parameter was not enough so I made sure to set the HTTP_PROXY and HTTPS_PROXY environment variables.

At this point the Scripts folder within the 've' virtual environment can be added to the PATH environment variable. This ensures that the command line commands can be used without needing to change directory to the directory in which they have been installed - ie. Windows will check the PATH variable to check specified paths for an application command.

This will permit use of the virtualenvwrapper-win commands directly from the command prompt and at this point the virtualenv module can be removed from the stock Python pip envrionment applied by ArcMap reverting it back to stock.

cd <ArcMap Python path>\Lib\site-package

python -m pip uninstall virtualenv

There is plenty of information online about setting up and using virtualenvwrapper and the command itself also includes a help section.

mkvirtualenv -h

workon -h

The only other piece of configuration I used was to set a WORKON_HOME environment variable to define the default location of any new virtual environments that are created using virtualenvwrapper commands. This ensures I don't need to specify the path each time and makes sure they're all put in the same place. I set this to %USERPROFILE%\.virtualenv

Yes you can specify another location via an override if you need to...

Using a . at the start of the directory is in keeping with the standard used of virtualenvwrapper in Linux environments but to create this directoy in Windows it needs to be created at the command prompt. Otherwise an error message appears. Otherwise just create it without the period.

This then permitted me to uninstall the original virtualenv from the ArcMap Python install reverting it back to stock.

At this point the fun can begin and I am in a postion to create any number of virtual environments for any reason. This next blog post shows why I did this in order to leverage some functionality within a Pandas module that wasn't included within the version of Pandas supplied with ArcMap 10.4.1


Additional comments have been disabled for this entry

An entry posted on July 23, 2017.

Categories: Open Source and Python

Tags: arcmap , esri , python , virtualenv , windows and work