Select to view content in your preferred language

Specify Python Version for a Python Toolbox (.pyt)

2866
8
09-24-2014 12:11 PM
BrendanDwyer
Frequent Contributor

I'm writing a python toolbox (.pyt) and almost every machine it's going to go on has been upgraded from ArcDesktop 10.0 to 10.2.x.  There are two python directories on the C: drive, one for 2.6 and one for 2.7 (named Python26 and Python27).  Every once in a while the python toolbox will have the red X over it signifying a syntax error, even though I haven't edited it.  The error states:

AttributeError: 'module' object has no attribute 'da'

I'm pretty sure the problem is the toolbox is referring to the older version of python that's still on the computer (and it can't fine the Data Access (da) module in the newer version).  Is there any way in the python code itself to specify to use the 2.7 version?  I don't have any sort of elevated rights on the machines this will go on.

Thanks,

Brendan

0 Kudos
8 Replies
AlexanderNohe1
Honored Contributor

Hello Brendan,

I assume that you are getting this error on the 10.0 machines?

The issue here may be that at version 10.0 of ArcMap that the data access module did not yet exist.  Therefore, you are trying to use something that was not present in these previous versions.

Accessing data using cursors - Version 10.0

ArcGIS Desktop

Accessing data using cursors - Version 10.2.x

ArcGIS Help (10.2, 10.2.1, and 10.2.2)

You would not be able to access the newer data access module from the older version of ArcMap in a python toolbox.  What you may want to consider doing is modifying the version of Python toolboxes that are distributed to the 10.0 machines rather than writing a python toolbox for all machines.

Finally, if you are getting this error intermittently on the 10.2.x machines, I would suggest running a repair of ArcMap.  This should update any missing Python Paths to the new 2.7 version.

In addition, to answer your question, I do not believe you can specify the version within ArcMap, it should default to the version that it was installed with.  You can check which version it is using with the following code snippet:

import sys

sys.version

Does this help?

curtvprice
MVP Alum

Python toolboxes (pyt) were new at 10.1 - so I even if they somehow were recognized, I would not expect them to work with 10.0. Brendan is right about arcpy.da - if you want to write a script that will run on both 10.0 and 10.1, you code to use the supported flavor of cursors in each version, or use the classic (slow) version which will work in both.

I read something interesting today that Python 3.4 (when we install it for use with Pro) includes a special python launcher for windows that allows you to specify which version of python should be used with a shebang line (#!/bin/python, etc) in your code. (This emulates the way this problem is handled in Unix!) That will be nice.

BrendanDwyer
Frequent Contributor

Alexander, Curtis,

Thanks for getting back to me.  The error is on machines with 10.2 installed, and I don't plan to use it on any machines with 10.0.  I think it's really odd that the toolbox will reference different versions.  Alexander, can you tell me more about updating python paths?  I think that's what I'm going to need to do.  Also, I think I need to uninstall the older version of python.

btw Curtis, good to hear from you.  I used to work at the St Pete USGS office under Mr. Wertz.

0 Kudos
curtvprice
MVP Alum

Hey Brendan.

I agree if you have no reason to keep the Python 2.6 around it should go. Since 10.1 the python is included in the Windows ArcGIS package so when you uninstall ArcGIS the python goes with it.

A debugging check to help nail down what's going on is have your script print

  • sys.version
  • sys.executable
  • arcpy.__file__
BrendanDwyer
Frequent Contributor

If I go into the python window in arcmap or catalog and print the sys.version, it returns that the version is 2.7.5.  But on these systems that had 10.0 installed previously, a toolbox with the da module will work sometimes, and other times have a red x over it, with a syntax error stating that it can't import name da.  Why does it work sometimes and not others?  If I don't have admin rights on the machine, am I SOL?

0 Kudos
curtvprice
MVP Alum

Without admin access you can't dink with file associations, or even some paths.  If Python 2.6 is in the windows %PATH% variable, maybe you can modify that before you start ArcMap with a batch script.  But I don't see why Desktop would be going to the wrong Python - a repair install should fix that.

You could also dig into the Python sys.path *within* your toolbox and see if your sys.path is confused as to arcpy and python versions:

import sys

arcpy.AddMessage("\n".join(sys.path))

But honestly I think this is kind of a waste of time as the real fix is to find a sys admin to help you:

a) remove the python 2.6 (esp if you do not need it!) and

b) Do a desktop repair install to make sure ArcMap/ArcCatalog will talk to the correct Python

BrendanDwyer
Frequent Contributor

I think I figured out what was going on.  IT guys swore up and down that no previous versions of Arc were on the machines on which we're having the problem.  But the python 26 folder and registry entries are there.  Finally noticed that Military Analyst 10 was loaded on the machine.  My guess is that the extension loads some python 2.6 libraries and the system gets 'confused' as to which one the toolbox should run.  Testing now by uninstalling the extension.  If that doesn't work, I'm going to have them build a new machine from scratch w/o the extension ever loaded.

0 Kudos
BrendanDwyer
Frequent Contributor

One more note: Talked to an esri developer the other day and described this problem.  He said the pyt files are still really buggy and esri hope's to have a bunch of issues worked out by 10.3.  He couldn't say what the cause of the problem is exactly, but my take-away was to create any new tools using script tools added to old fashioned tbx toolboxes.

0 Kudos