Select to view content in your preferred language

"Object has no Attribute"the second time through arcpy script

3556
12
01-24-2012 12:23 PM
MarkBentley
Emerging Contributor
I created a toolbox that contains a few tools from the Arc Hydro Groundwater Tool Suite that I like to use in my modeling.  The tools import alright and I can run the script once, but when I try to run the same script a second time with the same inputs I get a message saying the following:

"<type 'exceptions.AttributeError'>: 'NoneType' object has no attribute"

If I close Arc Map and run the script again, the script will run one time. If a try to run it twice I get the same error message as above.  This cycle has proven itself to repeat indefinitely.  What could be causing this, do I need to clear something out at the end of the toolbox import?

import arcpy
arcpy.ImportToolbox("D:\GIS_Geodatabase\ES12.mdb\mytools", "mytools")
arcpy.mytools.ExportPackageWEL("WEL","CELLGRP","CBFlags","StressPeriods","DISVars","Params","D:\MODFLOW_WorkingFiles\EASTSHOR_MODFLOW\EASTSHOR.wel")
Tags (2)
0 Kudos
12 Replies
StacyRendall1
Frequent Contributor
Mark,

It's a long shot, but you could try adding a try...except clause to your script - it may give you more information... Like so:
import arcpy
try:
    arcpy.ImportToolbox("D:\GIS_Geodatabase\ES12.mdb\mytools", "mytools")
    arcpy.mytools.ExportPackageWEL("WEL","CELLGRP","CBFlags","StressPeriods","DISVars","Params","D:\MODFLOW_WorkingFiles\EASTSHOR_MODFLOW\EASTSHOR.wel")
except:
    print arcpy.GetMessages(2)

This may return a little more information about the error.

Can you also post the exact error that Python returns including line number (plus ensure the script you post is laid out identically to the one you are using); this information often is very useful, as it indicates which part of the script is failing, and possibly why.

Something does just come to mind that if you import the same toolbox multiple times then it must have a different name; perhaps you could make it "mytools_1" the first time, "mytools_2" the second time, etc...? Depending on how you use your code this is probably not be easy to do...
0 Kudos
MarkBentley
Emerging Contributor
Hi Stacy,
Thanks for your help on this.  The error I am receiving is as follows:

<type 'exceptions.AttributeError'>: 'NoneType' object has no attribute
'ExportPackageWEL_mytools'
Failed to execute (EastShorePermit).



I inserted the "try/except" clause like you said.  The error no longer shows, however it just skips over the section where the trouble occurs. The script I am using is pretty long, but the trouble really only occurs in the very last block of code:

import arcpy

#Clears the rows in "Wells_New" and adds 1 new row
arcpy.AddMessage("The rows in 'Wells_New' feature class are being cleared")
arcpy.DeleteRows_management("Wells_New")
rows = arcpy.InsertCursor("Wells_New")
myrow = 1
while myrow <= 1:
    row = rows.newRow()
    myrow = myrow + 1
    rows.insertRow(row)
del row
del rows
arcpy.AddMessage("The rows have been cleared")

#Linking script parameters entered in ArcGIS to
#Python Parameters in this script
arcpy.AddMessage("Linking script parameters entered in ArcGIS to Python Parameters in this script")
pR = arcpy.GetParameterAsText(0)
sT = arcpy.GetParameterAsText(1)
sB = arcpy.GetParameterAsText(2)
K = arcpy.GetParameterAsText(3)
X = arcpy.GetParameterAsText(4)
Y = arcpy.GetParameterAsText(5)
arcpy.AddMessage("The parameters you entered have been linked to python parameters in the script")

#Write the parameters to 'Wells_New'
arcpy.AddMessage("Write the parameters to 'Wells_New'")
rows = arcpy.UpdateCursor("Wells_New")
for row in rows:
    row.PumpingRate = pR
    row.ScreenTop = sT
    row.ScreenBot = sB
    row.LayerNumber = K
    row.X = X
    row.Y = Y
    rows.updateRow(row)
del row
del rows
arcpy.AddMessage("The parameters you entered are being written to the 'Wells_New' feature class")

#Converts the Coordinates to a point
#Uses the SearchCursor to extract the X, Y values from "Wells_New"
arcpy.AddMessage("Creating Point from coordinates")
rows = arcpy.SearchCursor("Wells_New")
for row in rows:
    xval = row.X
    yval = row.Y
del row
del rows

#Creating Point from coordinates        
point = arcpy.Point(xval, yval)
pointGeo = arcpy.PointGeometry(point)
arcpy
arcpy.AddMessage("A point has been created based on the coordinates you provided")

#Writes that point to the "SHAPE*" field in the "Wells_New" table.
arcpy.AddMessage("The point that was created is being assigned as the location of the new well")
rows = arcpy.UpdateCursor("Wells_New")
for row in  rows:
    row.SHAPE = pointGeo
    rows.updateRow(row)
del row
del rows
arcpy.AddMessage("The location of the new well has been assigned")

#Determine the IJ index for the well
#Used to help determine value of IJK
arcpy.AddMessage("Determining 'IJ' indice for the new well")
arcpy.SelectLayerByLocation_management("Cell2D","INTERSECT","Wells_New")
rows = arcpy.SearchCursor("Cell2D")
for row in rows:
    ijdex = row.IJ
del row
del rows
arcpy.SelectLayerByAttribute_management("Cell2D", "CLEAR_SELECTION")
arcpy.AddMessage("The 'IJ' indice is " + str(ijdex))

#Determine the values of I and J seperately
#Used in determining IJK
arcpy.AddMessage("Determining the values of I and J seperately")
rows = arcpy.SearchCursor("CellIndex", '[IJ]='+ str(ijdex))
for row in rows:
    Ival = row.I
    Jval = row.J
del row
del rows
arcpy.AddMessage("The value of the 'I' indice is " + str(Ival))
arcpy.AddMessage("The value of the 'J' indice is " + str(Jval))

#Number of Cells in the I, J, and K directions.
#Used in calculation of IJK
numi = 67
numj = 36
numk = 3

#Calculate the value of IJK
#Used for ahgw 'WEL' table
arcpy.AddMessage("Calculating the IJK indice")
K = int(K)
IJK = ((K - 1) * numi * numj )+(( Ival - 1 ) * numj ) + Jval
arcpy.AddMessage("The value of 'IJK' is " + str(IJK))

#Create a new blank row in the well table
arcpy.AddMessage("Creating a new blank row in the 'WEL' table")
rows = arcpy.InsertCursor("WEL")
myrow = 1
while myrow <= 1:
    row = rows.newRow()
    myrow = myrow + 1
    rows.insertRow(row)
del row
del rows
arcpy.AddMessage("A new blank row has been created in the 'WEL' table")

#Write the appropriate values to the "WEL" table
arcpy.AddMessage("Writing values to the 'WEL' table")
arcpy.SelectLayerByAttribute_management("WEL", "NEW_SELECTION", ' is null')
rows = arcpy.UpdateCursor("WEL")
for row in rows:
    row.Q = pR
    row.IJK = IJK
    arcpy.AddMessage("Writing Key Value to 'CELLGRP' field")
    row.CELLGRP = -1
    row.SPID = 1
    row.Qfact = 1
    row.IFACE = 0
    rows.updateRow(row)
del row
del rows
arcpy.SelectLayerByAttribute_management("WEL","CLEAR_SELECTION")
arcpy.AddMessage("Values for new well have been written to the 'WEL' table")

#Import My tool Box to use several AHGW tools
arcpy.AddMessage("Importing a toolbox containing several AHGW tools that will be used")
arcpy.ImportToolbox("D:\GIS_Geodatabase\ES12.mdb\mytools", "mytools")
arcpy.AddMessage("The toolbox has been imported")
arcpy.AddMessage("Exporting the AGHW 'WEL' table to the MODFLOW '.wel' file")
# The code dies somewhere in here or at least I do not see the next 'AddMessage' that I programmed in
arcpy.mytools.ExportPackageWEL("WEL","CELLGRP","CBFlags","StressPeriods","DISVars","Params","D:\MODFLOW_WorkingFiles\EASTSHOR_MODFLOW\EASTSHOR.wel")
arcpy.AddMessage("The Table has been exported")
arcpy.AddMessage("Using AHGW tools to run MODFLOW")
arcpy.mytools.RunMODFLOW("C:\Program Files (x86)\Aquaveo\Arc Hydro Groundwater Toolkit\MF2K\mf2k_ahgw.exe", "D:\MODFLOW_WorkingFiles\EASTSHOR_MODFLOW\EASTSHOR.mfn")
arcpy.AddMessage("MODFLOW has finished")
arcpy.AddMessage("Importing the results of the MODFLOW model that are relevant to analysis")
0 Kudos
StacyRendall1
Frequent Contributor
The error basically says that it can't find ExportPackageWEL in the toolbox you import.

I have seen this happen before; Arc actually calls the tool by its name, but what you see in ArcMap/Catalog is just a label, not the actual name (or vice versa, I can't remember). Load the toolbox in ArcMap/Catalog and check that both name and label for that tool are set to ExportPackageWEL.

Let me know if that fixes it!
0 Kudos
MarkBentley
Emerging Contributor
Hi Stacey,

I don't think that is the cause of it because all of the ArcGIS tools as well as the ArcHydroGroundwater tools that I am using have different names and labels.  Also when I tried changing the name of the tools I discovered that I am not allowed to change the name or label of any of the tools that come with ArcGIS or ArcHydro Groundwater. It appears that these attributes are fixed for each tool

I did make an interesting discovery while simulating the script in the "Python Window."  When I put in my code like this:

arcpy.ImportToolbox("D:\GIS_Geodatabase\ES12.mdb\ExportRun" , "ExportRun")
arcpy.ExportRun.ExportRun()


I can run the tool without error once but if I try to run it twice then I get the same error saying:

<type 'exceptions.AttributeError'>: 'NoneType' object has no attribute
'ExportRun_ExportRun'
Failed to execute (debug).


But if I change the name of the Module from "ExportRun" to "ExportRun2," I am once again able to run the tool one time without restarting ArcGIS. This means my code would now read as follows:

arcpy.ImportToolbox("D:\GIS_Geodatabase\ES12.mdb\ExportRun" , "ExportRun2")
arcpy.ExportRun.ExportRun()


It appears that there is a restriction on ways or the number of times that I can use my module.  One Idea that I have is that I might be importing the same tools twice with the same module name.  I could see where this might cause confusion, since there would be two toolsets with the same name.
0 Kudos
StacyRendall1
Frequent Contributor
Hmmm...

I can understand that occurring within the Python window in ArcMap, but it really should not happen when running scripts (everything should get deleted, or there should be a method to unImportToolbox)...

A way around it might be to use ListToolboxes() to find out if it has already been imported, like so (based upon your second post):
import arcpy
if u"mytools" not in arcpy.ListToolboxes():
 arcpy.ImportToolbox("D:\GIS_Geodatabase\ES12.mdb\mytools", "mytools")
 arcpy.AddMessage("The toolbox has been imported")
else:
 arcpy.AddMessage("Toolbox already present, skipping import")
arcpy.mytools.ExportPackageWEL("WEL","CELLGRP","CBFlags","StressPeriods","DISVars","Params","D:\MODFLOW_WorkingFiles\EASTSHOR_MODFLOW\EASTSHOR.wel")


Let me know if that helps... If it doesn't work, try getting rid of the u in front of u"mytools" - it is just specifying that the string is a unicode string, which a lot of the Arc stuff seems to be.
0 Kudos
StacyRendall1
Frequent Contributor
Actually, the best way to get the name (in my previous post u"mytools") would be to import the toolbox, then use ListToolboxes() and find it within the list so that you know what it is actually being called after it is imported (then you should be safe if Arc is using the label or name or whatever)...
0 Kudos
SokratisBarmpounakis
Emerging Contributor
Hello people!

I have very very similar problem to the one you discuss about above.
I am using JPype module inside ArcGIS modelBuilder. JPype is used to use Java classes inside Python code.

I also run without any problems my model (only my scripts are in there, no default tools from toolbox) ONLY THE FIRST TIME and from the second time and afterwards I get the following error:
<type 'exceptions.AttributeError'>: 'NoneType' object has no attribute '__metaclass__'


I really cannot understand what is the cause of this error. Inside my script I start the JVM and call some Java classes. After that I delete all the objects created.
I don't know if Mark or Stacey you have found something new, or have a new idea about what may be causing trouble.

Sokratis
0 Kudos
MarkBentley
Emerging Contributor
Okay I figured out a workaround for this.

arcpy.ImportToolbox("Path to Toolbox")
arcpy.gp.Toolbox = "Path to Toolbox"
arcpy.gp.ToolName()

Using this method I can import the toolbox and run it as many times as I want.  Not sure why it works though.
SokratisBarmpounakis
Emerging Contributor
Okay I figured out a workaround for this.

arcpy.ImportToolbox("Path to Toolbox")
arcpy.gp.Toolbox = "Path to Toolbox"
arcpy.gp.ToolName()

Using this method I can import the toolbox and run it as many times as I want.  Not sure why it works though.


Dear Mark can you somehow relate this thing you found, with my problem perhaps? I don't use the system toolbox, I am just using my own scripts imported into the ArcGIS model builder. You think our problems are somehow related?
0 Kudos