Script config issue using arcpy vs. arcgisscripting

358
4
Jump to solution
06-27-2012 12:17 PM
LisaNelson
New Contributor II
Hello,

I'm updating some Py scripts to use ArcPy (v.10.0, not 10.1) instead of arcgisscripting and I've run into a 'structural' problem. I have a 'master' script/child script set-up I used with arcgisscripting where the master script imports arcpy, gets/checks parameters, then calls the child script. My questions are 1) is this script structure no longer allowed because the arcpy modules must be imported only once (or aliased to prevent conflicts) and 2) Do I have to convert this type of structure to a single script that has explicitly defined (i.e. def()) functions that use one arcpy module definition?

Master *.py script:
import sys, os, arcpy
#do checks...
#Call child script:
Message = child_Script(
    Input_GDB,
    Input_Feature_Class
    )
#Resume master script 
print Message


Child *.py script:
def child_Script(
     Input_GDB_Name,
     Input_Feature_Class_Name):

    try:
        import sys, string, os, shutil, time, traceback, arcpy #Have to import arcpy again!!!!        
        import arcpy.sa # Doesn't work... module not found    
    return "Finished!!"
    except:
       ...


Thanks for any ideas!

Lisa
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Frequent Contributor
The imports for mine were setup like this.

master
import dataset_conversion, dataset_conversion27_83, dataset_conversion83_CSRS def func():     dataset_conversion.export()     dataset_conversion27_83.conversion()     dataset_conversion83_CSRS.conversion()


first child

import arcpy, datetime, sys def export():     try: ...


I tested this way and it works as well.

Master
import arcpy,testing_arcpy  print testing_arcpy.test()

Child
def test():     import arcpy     import arcpy.sa     arcpy.env.workspace = r"C:\GIS\testing"     return arcpy.env.workspace

View solution in original post

0 Kudos
4 Replies
MathewCoyle
Frequent Contributor
I have imported child scripts that call arcpy without any problems.

Are you sure by this line
import arcpy.sa

You don't mean this?
from arcpy import sa
0 Kudos
LisaNelson
New Contributor II
I have imported child scripts that call arcpy without any problems.

Are you sure by this line
import arcpy.sa

You don't mean this?
from arcpy import sa


Hi - thanks for replying!  Apologies, I shoulld have noted that I tried 'from arcpy import *' and 'from arcpy import sa' syntax too; that gives me a 'cannot import module error'. 
So, are you declaring a child function explicitly using def() and placing the  'from arcpy import *' inside it?
0 Kudos
MathewCoyle
Frequent Contributor
The imports for mine were setup like this.

master
import dataset_conversion, dataset_conversion27_83, dataset_conversion83_CSRS def func():     dataset_conversion.export()     dataset_conversion27_83.conversion()     dataset_conversion83_CSRS.conversion()


first child

import arcpy, datetime, sys def export():     try: ...


I tested this way and it works as well.

Master
import arcpy,testing_arcpy  print testing_arcpy.test()

Child
def test():     import arcpy     import arcpy.sa     arcpy.env.workspace = r"C:\GIS\testing"     return arcpy.env.workspace
0 Kudos
LisaNelson
New Contributor II
OK - that makes sense! I cleaned up my example similar to yours (just not as elegant). It works as long as I explicitly declare geoprocessing function names.

Master:
import sys, os, arcpy as arcpy0
...
Message = childScript(
     params...)
arcpy0.AddMessage("Script Complete:\n" + Message)


Child:
def childScript(
     params...):
     try:
         import sys, string, os, shutil, time, traceback, arcpy
          ...
         outputRaster = arcpy.sa.ExtractByMask(hddRaster, Input_AOA_Feature_Class) 
         print "Finished"


No explicit 'sa' imports needed, although it would make the script more readable. Thanks for your help!
0 Kudos