[GIS] Using PyQGIS, ArcPy, NumPy, SciPy Together in Windows Command Prompt

anacondaarcpycommand linepyqgispython

I ran across this slight issue when I was looking to access python installations from ArcGIS, QGIS, Anaconda (cited because SciPy & NumPy come with it), and version 2.7 vs 3. Sometimes, I find it easier to just pop into Windows' Command Prompt to do a little bit of quick work instead of opening up a GIS program or python IDE in order to access the interactive prompt.

Instead of navigating to each folder, setting shortcuts, or switching out the PATH environment variable, I wanted a different way to access each python installation via the Command Prompt.

Edit

GIS professionals who use python for their work end up having to access each installation manually in order to access the features held within. Instead of waiting on something like ArcMap to load, it is generally much quicker to hit your winkey-r + cmd and type python into your Command Prompt to start up the python console. If you want to use ArcPy to reproject a shapefile, you need to open your python console, which is linked to ArcPy. If you want to use a tool from PyQGIS because you don't have the appropriate ESRI license, you need to find the python.exe that is linked to those modules. This can be a pain when you're trying to do work in ArcPy, PyQGIS, NumPy, or SciPy without having all the libraries installed cohesively as one python installation.

Typically, you're bound by your Path variable to which python installation is your default while in the Command Prompt. This question and the answers following look to address how GIS professionals might be able to utilize multiple python installations together in a single environment. Python IDEs target a single python installtion and require changing settings in order to utilize different ones. For instance, my PyScripter install targets my ArcGIS installation of Python. So, if I want to utilize my PyQGIS modules, I need to open up QGIS or change the settings in PyScripter.

When writing modules/full scripts, it makes sense to target one individually. That way you might get some sort of code completion happening. However, when you're just utilizing some tools in each installation to manipulate a data set, it would be much nicer if you can quickly flip python consoles/installations within a single screen aka the Command Prompt.

To those voting to close/off-topic…

Although this obviously applies to anything the python language is attached to, for the poor GIS user who can't afford to buy ESRI extensions for data analysis tools, it serves as a way for them to simplify their life. As some of the answers have shown, being able to customize this process more, e.g. add arguments to .cmd files, could potentially be of great use if someone is running a custom process. There is potential for a more savvy GIS/computer user to be able to set up custom .cmd files to quickly run processes without having to go through the fuss of dealing with different IDEs/GIS software installs etc..

The only downside to running a python console within the Command Prompt is the loss of code completion. However, if you run something enough, such as arcpy.AddField_management(), the code completion just gets in the way.

Best Answer

I'm going to offer an alternative that I think is a bit cleaner. It's inspired by a combination of Josef's suggestion and a common pattern on *nix systems.

  1. Start by creating a directory. This directory will be used to hold "alias" files for each install. So something like C:\Python\aliases or C:\PythonAliases will work.

  2. Next create a script for each install. Each script essentially aliases one of the Python installs you need to reference by simply calling its executable. For example, the contents would look like this for the QGIS install:

    @"C:\Program Files\QGIS Dufour\bin\Python.exe" %*
    

    Save it with a descriptive name into the folder you created, such as pyqgis.cmd or pyqgis.bat.

    The %* passes all arguments that were given to the script into the Python executable instead. So you can do this:

    C:\>C:\Python\aliases\pyqgis.cmd -c "print('Hello, world!')"
    Hello, world!
    

    (The @ symbol just prevents Command Prompt from unnecessarily echoing the command.)

  3. Add the directory containing these scripts to PATH. Now you can call them without the full path:

    C:\>pyqgis -c "print('Hello, world!')"
    Hello, world!
    

This has several advantages, in my opinion:

  • No managing of several system wide environment variables. (Because let's face it, the Windows interface for doing that is annoying at best.) You just modify PATH once. Getting new installs onto PATH or removing old ones is simple file management.
  • All your aliases are listed in one place. Figuring out what names you have available is a simple directory list away. (This would be especially helpful if you can't remember the name.)
  • Quoting problems are less likely to arise
  • This reflects an already existing pattern, and so is more easily understood/recognized. It's not uncommon to see aliases like python2 and python3 in the bin directory on a *nix machine. While this isn't exactly the same, I think it's similar enough that it would make some sense to someone knowing that this is done on *nix systems.

I'm not entirely satisfied with this set up, but I don't think you can do much better on Windows.

Related Question