NGA Advanced Python Programming for GIS, GLGI 3001-1

Import and Some Syntactic Sugar

PrintPrint

Now that we are familiar with how to step into the code and look at our variables, lets warm up a bit and briefly revisit a few Python features that you are already familiar with but for which there exist some forms or details that you may not yet know. We will start with the Python “import” command and introduce a few Python constructs that you may be new to you on the way.  

It is highly recommended that you try these examples yourself, experiment with them to get a better understanding, and use the debugger to step into the process to explore and see the variables values. 

Import

What happens here is that the module (either a module from the standard library, a module that is part of another package you installed, or simply another .py file in your project directory) is loaded, unless it has already been loaded before, and the name of the module becomes part of the namespace of the script that contains the import command. As a result, you can now access all variables, functions, or classes defined in the imported module, by writing

<module name>.<variable or function name>

e.g.,

arcpy.Describe(…) 

You can also use the import command like this instead:

import arcpy as ap 

This form introduces a new alias for the module name, typically to save some typing when the module name is rather long, and instead of writing

arcpy.Describe(…)  

you would now use

ap.Describe(…) 

in your code. Note that the use of ‘…’ is to indicate parameters, code between two important lines of code that would otherwise be distracting or is not necessary to show, or a continuation of list items

Another approach of using “import” is to directly add content of a module (again either variables, functions, or classes) to the namespace of the importing Python script. This is done by using the form "from … import …" as in the following example: 

from arcpy import Describe, Point , …  

... 

Describe(…) 
The difference is that now you can use the imported names directly in our code without having to use the module name (or an alias) as a prefix as it is done in line 5 of the example code. However, be aware that if you are importing multiple modules, this can easily lead to name conflicts if, for instance, two modules contain functions with the same name. It can also make your code a little more difficult to read since
  arcpy.Describe(...) 

helps you or another programmer recognize that you’re using something defined in arcpy and not in another library or the main code of your script.

You can also use

from arcpy import *

to import all variable, function and class names from a module into the namespace of your script if you don’t want to list all those you actually need. However, this can increase the likelihood of a name conflict. 

Keep in mind that readability matters and using the <module name>.<variable or function name> helps the reader know where the function is coming from. Modules may have similar function names and this convention helps describe which package you are using. Your IDE might flag the function as ambiguous if it finds any same named function declarations that could be from multiple modules. 

Lesson content developed by Jan Wallgrun and James O’Brien