# Using sys.path # Most operating systems define one or more path variables that indicates the # folders that should be searched for executable files when the user types a # name on the command line. In python, an import command uses the PYTHONPATH # environment variable (set when you downloaded and installed python) to # find all its modules. You can extend the searched folders using sys.path. # In this sample code, I have set sys.path to one of my network drives where # I have saved python modules for this course # (R:\\GOG530ArcPython\\). # Then, any import command that can't find the given name in folders # along PYTHONPATH will check the folders in sys.path. In this example, it # will find a file UTM_Coord.py and Road.py in R:\GOG530ArcPython. # Now the module containing this example (PythonPathTest.py) can use the stuff # classes, functions, etc) in the imported modules without having to be saved # in the same folder as the imported modules. Yay! For a much more authoritative # discussion of the import mechanism, see # https://leemendelowitz.github.io/blog/how-does-python-find-packages.html # A simple example of how you can use sys.path # To adjust for your own use, substitute 'R:\\GOG530ArcPython\\' with a folder # on your machine that holds your own python files (modules). Then, save this # file to some other folder and try importing your own modules in the folder # assigned to sys.path. import sys # needed so that we can access sys.path sys.path = ['R:\\GOG530ArcPython\\'] # additional location or locations to search for modules import UTM_Coord as uc # I have modules in this folder including import Road # Road.py and UTM_coord.py p0 = uc.UTM_Coord(580000, 4720000, 18, 'n') # I can now access any of the classes p1 = uc.UTM_Coord(580100, 4720100, 18, 'n') # or functions within the modules print 'easting diff between p0 and p1 is ' + str(p0.easting - p1.easting)