Solved! Go to Solution.
Hello Caleb1987,
Although I DO import arcpy into both aaa.py and bbb.py, you've made me realize why I'm getting the error I think.
That is, when I run bbb.py which imports both arcpy and aaa.py, and attempt to execute aaa.func1(), the reason there is a problem with aaa.func1() not recognizing 'arcpy' is that it never gets imported into aaa.func1() since it's only imported in aaa.py when name == __main__ (i.e. when executing aaa.py directly).
So I do get this. But what is the solution then?
Should I add "import arcpy" to each function within aaa.py ? This does solve the problem but it doesn't seem like the right thing to do. Not sure if this will create any issues (performance or otherwise).
def func1(): # do something if __name__ == '__main__': import arcpy # this only gets imported when name is __main__ func1()
import arcpy # this way arcpy is ALWAYS imported def func1(): # do something if __name__ == '__main__': func1()
# aaa.py import arcpy def func1(): # some code print 'Name = %s' %__name__ arcpy.AddMessage('Name = %s' %__name__) return func1()
>>> Name = '__main__'
# bbb.py import aaa aaa.func1()
>>> Name = 'aaa'
Hello Caleb1987,
Although I DO import arcpy into both aaa.py and bbb.py, you've made me realize why I'm getting the error I think.
That is, when I run bbb.py which imports both arcpy and aaa.py, and attempt to execute aaa.func1(), the reason there is a problem with aaa.func1() not recognizing 'arcpy' is that it never gets imported into aaa.func1() since it's only imported in aaa.py when name == __main__ (i.e. when executing aaa.py directly).
So I do get this. But what is the solution then?
Should I add "import arcpy" to each function within aaa.py ? This does solve the problem but it doesn't seem like the right thing to do. Not sure if this will create any issues (performance or otherwise).
def func1(): # do something if __name__ == '__main__': import arcpy # this only gets imported when name is __main__ func1()
import arcpy # this way arcpy is ALWAYS imported def func1(): # do something if __name__ == '__main__': func1()