ENVIRON 859 - Advanced GIS

Getting Started With PythonWin

Getting Started With PythonWin♦ Integrated Development Environments (IDEs)Which IDE to use? ♦ Installing PythonWin -- Oh, and Python Environments, Conda, and PipPython EnvironmentsIntroducing Conda & cloning your ArcGIS Pro environment. Installing PythonWinCreating a shortcut to PythonWin♦ Navigating PythonWin♦ Debugging scripts with PythonWin♦ What’s next♦ A quick peek at Spyder► Installing Spyder►Using Spyder

♦ Integrated Development Environments (IDEs)

Python scripts are simply text files with a .py extension; you can open them and read them, even write them, in Notepad, Wordpad, Notepad++, etc. However, working with Python scripts is more easily done in an integrated development environment (IDE) as these IDEs offer tools to auto format scripts as you write them, to debug them, and even to execute Python statements or chunks of scripts interactively.

Which IDE to use?

Several IDEs exist for Python. IDLE is packaged with Python and is the default one if you chose to edit a script tool from within ArcMap. The benefit of IDLE over many other Python IDEs is that it is cross platform, meaning it can be run on Windows as well as Linux machines. Beyond that, however, it's not that great. In fact, I don't know anyone who uses that.

PythonWin (aka "pywin32") was recommended by ESRI for those writing Python scripts on Windows machines since ESRI adopted Python as its scripting language in the early 2000s. It's a little dated now, but is still reliable and is what we'll use today. Other popular IDEs include Spyder, which works well with ArcGIS Pro, PyCharm, and Rodeo - for those who like an R-studio like interface. Installation can be tricky, however, and as the old adage goes: your mileage may vary.

For simplicity sake then, we'll stick with PythonWin, but time permitting, we'll take a quick glance at Spyder


♦ Installing PythonWin -- Oh, and Python Environments, Conda, and Pip

PythonWin is a free software package that has to be installed separately from ArcGIS. A version of PythonWin that works with ArcGIS Desktop is installed on the NSOE machines, but PythonWin is linked to a specific installation of Python and thus this version only works with Python 2.x. We'll want to have a version that works with 3.x/ArcGIS Pro, so we'll take this opportunity to introduce the world of Python environments, packages, and package managers.

Python Environments

While ArcGIS Pro installs a single version of Python 3.x, we can create many separate Python environments in which to work. You can think of a Python environment as a virtual copy of the software. We may need separate environments because the various add-on packages we may want to install might conflict with each other. It's just a side effect of using 3rd party packages developed independently, but allowing for multiple programming environments is a nice workaround for this.

ArcGIS Pro comes with a locked base environment, but we can clone this environment and make changes:

♦ Exercise 1: Viewing ArcGIS's Python Environments

 

Introducing Conda & cloning your ArcGIS Pro environment.

Conda is a Python package manager, similar to ArcGIS Pro's package manage. Conda has a command line interface (boo!), BUT it's currently a bit more stable (yeah!) - and it's good to know these things in case you later use Python without ArcGIS. (Read more about Conda and ArcGIS here)

♦ Exercise 2: Cloning ArcGIS's Python Environments using Conda

For more information on Conda commands see: https://conda.io/docs/user-guide/getting-started.html

 

Installing PythonWin

Now that we have our own personal python environment, we can make whatever changes we want with it. If we run into issues, we can destroy it (conda remove --all --name my_env) and recreate a new one. For now, we'll modify it by installing PythonWin, and this is done with more "conda" commands.

♦ Exercise 3: Installing PythonWin

 

Creating a shortcut to PythonWin

While we just installed this package, access to it is buried deep inside your Python environment. To make it easer to use, we'll create a shortcut to the PythonWin app.

♦ Exercise 4: Creating a shortcut to Pythonwin

You can now copy this shortcut any where on your local or virtual machine and it will open up PythonWin, setting the folder in which the shortcut lives as the default workspace.

Ok, if we all survived that, let's take a quite tour of PythonWin!

 


 

♦ Navigating PythonWin

Create a new folder somewhere on your machine and copy the PythonWin shortcut to that folder.

  1. Open PythonWin

    When PythonWin opens you are presented with the program’s menu and tool bar as well as the interactive window. The interactive window allows you to type Python commands and execute them. These commands are typed at the prompt (where you see >>>).

    image1

  2. Run a Python command from the prompt in the interactive window

    Type the following at the Python prompt and then hit Enter.

    image2

    Now hold the control key and click on the up arrow key on your keyboard. You’ll see the previous command you just typed appear at the prompt again. This can be very handy…

  3. Create a new Python script

    Select New from the File menu or click the "new" button in the toolbar. In the box that appears, select Python Script and click OK. A new Script window will appear. In this window, type the same command as you typed in the interactive window.

    image4

  4. Save the python script

    Select Save from the File menu or click the Save tool in the toolbar. Save your script as "HelloWorld.py" in your folder, then close PythonWin.

  5. Open the python script in PythonWin

    In Windows Explorer click the HelloWorld.py file and drag it on top of the PythonWin shortcut. You should be right back to where you left off when you closed your script.

  6. Run your script

    With your HelloWorld.py script active, click the tool that looks like a little guy running. This runs your script and sends any output to the interactive window. However, before it runs, PythonWin asks whether you want to run your script in a debugging mode or not. For now, select the default of No debugging and click OK.

image4

 

  1. Modify your PythonWin settings

    Select Options from the View menu. In the box that appears, click the Editor tab and change Line Numbers from 0 to 30.

    image5

    Doing this exposes line numbers in your script window. This can be very helpful as our scripts get longer and longer.

  2. Next, in the View/Toolbars menu, select Debugging. This will add the debugging toolbar to your script. Well use this later.

image6

 

Note: Like ArcGIS preferences these preferences will not be saved from machine to machine or if your local profile is deleted. However if you are on your virtual machine, everything will be fine!

 

You should now have a rough idea of how to create, open, edit, and run Python scripts using the PythonWin IDE. There’s much more that PythonWin can do for us, of course, and we’ll see that as the tutorial moves along, starting with the next exercise where we examine a simple case of debugging a Python script.


♦ Debugging scripts with PythonWin

Nearly every script you write will have at least one pesky error that you’ll need to fix before it will perform exactly as you want it to. Errors come in two forms: Syntax errors arise from typos or other miscues in your script that prevent Python from understanding what you are saying. Syntax errors are usually easy to spot and often easy to fix. Logical errors occur when your script runs, at least part way, but does not give you the output you expect. These can be harder to fix and may require painstaking analysis of your scripts flow and introspection of variable values at specific points along your script’s execution. Debugging is one of the more challenging aspects of writing a script, but fortunately PythonWin has several tools that allow you to identify and fix errors. In Exercise 2 we examine a simple script that has some errors in it, and we use PythonWin debugging tools to identify and fix them.


♦ Exercise 5 - Using PythonWin to debug a script

  1. Open up the PythonWin(if its not open already). Make sure that the debugging toolbar is visible.

  2. Create a new script, copy the code below into it, and save the script as Exercise5.py

    Before running anything, take a look at the script and you’ll see text printed in different colors and some words in bold. One advantage of using an IDE over a text editor to write scripts is an IDE’s ability to auto-format your script. For example, strings are shown in one color (beige), numbers in another (teal), and Python commands or other reserved words in a bold blue.

    Next, see if you can decipher what this script does. It has four variables (name, gender, age, and class), each assigned an initial value. Then, the script issues two print statements to print the values of these variables to the interactive window.

  3. Check the script for syntax errors. Just to the right of the Run icon in the toolbar is the "Check" tool (image7). Click this to see whether you script has any syntax errors. You’ll get a report at the very bottom of the PythonWin window.

    image8

    If a syntax error is present, PythonWin places the cursor to where it thinks the error occurs. In our case it’s the very first line.

  4. Set the first line to be a comment. The first line in our script is not Python. It is just some text we include to identify what the script does, and so we want Python to just ignore it. To do so, place a pound sign in front of it. You’ll see that the text turns green, part of PythonWin’s auto-formatting for a comment.

    image9

  5. Check the syntax again. You'll now see that there's a syntax error in Line 6. The problem here is that we have used a reserved word ("class") as a variable name. We know this because "class" is in bold blue.

  6. Change the class variable name in line 6 to class_name

    image10

  7. Update the reference to the class variable in line 9 to the revised variable name class_name. image11

  1. Check the syntax again. You should have no more syntax errors as shown by the following message at the bottom of the PythonWin application window:

    image12

  1. Run your script. Go ahead and try running your script using the Run button (with no debugging). You will get an error message in the interactive window. ​ image13

    In this case, the error message states the error occurs in line 8. Some error messages are not as kind and don't reveal exactly where the error occurs. For this we need debugging tools.

  2. Step through your script. In the debugging toolbar are three tools that allow you to step through your script a line at a time. image14

  3. Check variable values during run time. In the interactive window, you can run Python statements that include the variables that have been assigned from running your scripts. Try the following:

image15

  1. In line 5, put quotations marks around the value assigned to the age variable. image16

  2. Run the model again. It should work, and print out the following to the Interactive window. image17

  3. Close PythonWin. Notice you didn’t need to save your script. It is saved each time you run your script.


♦ What’s next

After completing this tutorial, you should now be able to know enough about Python and PythonWin to do some experimentation on your own with creating variables of different data types and performing operations on them. Next up, we focus more on writing scripts using these objects and their functions.



♦ A quick peek at Spyder

► Installing Spyder

►Using Spyder