I have some scripts that are constantly setting env.workspace of arcpy. Usually, I have a caller script(script1.py) that may call some other script (script2.py). I've created a file globals.py with the paths that never change:
import arcpy.env as env
rootPath="S:/workspace/MSc_git/ArcGis/"
layersPath=rootPath+'Layers/'
# Set the workspace environment to local file geodatabase
env.workspace=rootPath+'estagio_db.gdb'
In script1.py I have something like this:
import arcpy
import globals as g
from arcpy import da
arcpy.AddMessage("Importing arcpy outside ArcMap took %s seconds." % (time.clock() - t_start))
from cProfile import run
def main():
....
And I need to use env.workspace and sometimes the layerspath inscript2.py How can I do this (should I import gloabl.py as well?)? should I pass it by argument when calling a function from script2.py in script1.py or is there any other way to accomplish this?
I know we should avoid global vars, but in this case, and since I need to centralize its definition I don't know how to do it well...
1 Answer 1
I think a global in this case is fine, as you are using arcpy and by setting arcpy.workspace
you're setting a giant global in arcpy anyway (which is bad library design but I digress)
Personally I would be passing in the values you need into script2 functions as it will make your code cleaner as you are reducing the reliance on global vars.