8

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.

  • 2
    I wonder if this would be more appropriate on SuperUser. This really doesn't have anything to do with GIS, beyond tangentially involving a runtime used commonly in GIS. Certainly, I think you'd be more likely to get the best answer on SuperUser than you would here. – jpmc26 Aug 11 '15 at 17:09
  • 1
    @jpmc26. Possibly, but I think it is GIS appropriate since I specifically compared QGIS and ArcGIS and using multiple python installations together in GIS can be common. It did expand out a little further, but since a lot of answers are coded answers here, it seems fitting. – Branco Aug 11 '15 at 17:13
  • There are commonly questions on this side of things concerning installations and configurations of GIS software. This applies the same where we are invoking 2 different python installations from 2 different GIS software components via the Command Prompt. Albeit, it can easily be expanded into other things Command Prompt, but the original intent is to configure quick access to QGIS/ESRI python installations without hassle. – Branco Aug 12 '15 at 12:18
9

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.

  • I like this process. I, like most typical Windows users, don't venture too much into the system variables, path, and other stuff unless something tells me to. I was just trying to jump between QGIS python and Anaconda last night and figured my little shortcut. This is probably the better long-term solution. – Branco Aug 11 '15 at 17:18
7

I decided to play around with the System Variables part of the Environment Variables settings to see if I can make it easier to access each python installation while in the Command Prompt. I found I could add in a new System Variable and call it from the Command Prompt to access whatever python installation I wanted.

Some examples:

Variable = PyQGIS Value = "C:\Program Files\QGIS Dufour\bin\Python.exe"

I left the double quotes in there so I wouldn't have to quote the spacing in the Command Prompt to keep the whole string together as a single argument

Variable = Anaconda Value = AnacondaDirectory\Python.exe

How I access my installs of scipy/numpy/etc

Variable = Python3 Value = C:\Python34\Python.exe

Python 3 includes a launcher that allows you to specify version... >>py -3 for python 3... info here

To call a System Variable in the Command Prompt:

When you're in the Command Prompt, you can call your variable by typing %VariableName% into the command prompt. For each python install, it will navigate to the {Directory}\Python.exe to open the interactive interpreter. If you didn't quote a Value that has spaces, you need to add quotes around your variable call, ex. "%VariableValueWithSpaces%". Without the quotes, the line gets split by the spaces so your program won't start.

I am sure there are probably easier ways to do things, but I found this to be a useful bit of knowledge if you're especially fond of using the Command Prompt in Windows. I like the ability to access individual python installations just by providing a different variable name.

  • Alternatively, what I do: have a .cmd file for each installation that sets the path and possibly other things in path. So e.g. have python34.cmd in your path that contains @set PATH=C:\Python34\;%PATH%. Then just entering python34 will make this python available. – Josef Aug 11 '15 at 15:08
  • Do you mean have it as another system variable? Or append the location of python34.cmd to your Path variable? – Branco Aug 11 '15 at 15:12
  • I mean you create a cmd file that will prepend the location of python.exe to your path. You put this cmd file anywhere in your path. You can create a new folder and prepend it to the path variable. You can then run python34 from every shell, because it is in the path. Python34.cmd will then change the path so the python version you want is the first thing in path for the current shell session (and all child sessions) only. – Josef Aug 11 '15 at 15:14
  • Ok, got it... That is also a pretty good idea especially if you are adding extra settings/changes to the .cmd file. – Branco Aug 11 '15 at 15:16

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.