I am working on a Python package that has a mixture of command line tools and ArcToolbox tools. For Python development it is really useful to use VirtualEnv, especially when working with installers. Is there a way to have either ArcMap or ArcCatalog recognize a current VirtualEnv session in Python, so that when a custom tools is executed it is using the VirtualEnv Python paths and packages instead of the default Python paths and packages at c:\Python26\ArcGIS10.0?


That's not built into the application, but I assume running the batch file that sets up the virtualenv in a cmd.exe session and then running arcmap.exe from the same DOS box should get you your virtualenv. ArcMap and friends will modify the path slightly to include the install's arcgisscripting and arcpy.

Edit: you will need to explicitly call the activate_this script from within the Python window in desktop apps to get this to work. If I have a VirtualEnv in c:\Users\jason\myenv I will issue execfile(r'C:\Users\jason\myenv\Scripts\activate_this.py', {'__file__': r'C:\Users\jason\myenv\Scripts\activate_this.py'}) from the Python window in an ArcGIS desktop app to get it running.

  • That doesn't appear to work. I created a VirtualEnv workspace, activated it, then installed a package that I want to test inside of ArcCatalog. So far so good. If I open a Python shell inside this VirtualEnv session and import the newly installed packages - the import works. If I then launch ArcCatalog and try to call one of these packages from a toolbox script - it does not work. If I look at sys.path from the Python shell inside of ArcCatalog, it is showing the default Python directory for ArcGIS 10.0 (C:\Python26\ArcGIS10.0). – SagebrushGIS Mar 22 '11 at 17:24
  • 4
    Look into using the activate_this.py script also in the same Scripts directory of your VirtualEnv from the Python window in ArcCatalog and friends. I was able to execute execfile(r'C:\Users\jason\myenv\Scripts\activate_this.py', {'__file__': r'C:\Users\jason\myenv\Scripts\activate_this.py'}) and get my VirtualEnv bootstrapped from within ArcMap. – Jason Scheirer Mar 22 '11 at 19:43
  • 1
    Awesome! That did the trick. – SagebrushGIS Mar 22 '11 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.