New Contributor III
since ‎07-14-2014
7 Posts created
0 Kudos given
1 Solutions
9 Kudos received

My Ideas

(0 Idea Submissions)

Latest Contributions by AndrewOrtego


Since we get a lot of different Python-related calls in Support, I’ve collected the six most common requests into the scenarios below. Use these scenarios to help you decide which workflow applies to your current problem.1. You’re getting started with Python for the first time and aren't sure how to start learning it.

You’re in luck! I wrote a blog about this very problem about a year ago! That blog contains tutorials that help anyone learn to code in Python and links to many free online resources--these resources are organized by learning styles, so there’s something for everyone.Seven easy ways to start learning Python and ArcPy2. Your script returns the error message: “ImportError: No module named arcpy."

This is the most frequent issue Esri Support encounters when troubleshooting ArcPy. The error message is produced when the ArcPy site package is installed incorrectly, but it’s very easy to fix. There are a number of causes that can interplay to produce this error, so it is best to refer to Knowledge Base article 43846, Problem: Unable to import arcpy, for more details when troubleshooting this type of issue.

If that article doesn’t provide a solution, I recommend speaking with your IT team to determine if there’s any reason to expect that you wouldn’t have full read or write permissions to the directories you’re accessing. If you’re working on a remote machine, attempt to troubleshoot directly on the machine installed with ArcGIS for Desktop.3. The script you wrote works everywhere except in the Windows Task Scheduler.

Since Esri didn’t create the Windows Task Scheduler (WTS), this is a tricky one to troubleshoot with Esri Support. However, the Knowledge Base article, Windows Task Scheduler will not run a Python script, will help you verify whether the issue is with the WTS, the Python installation, or your script.4. You want to start creating a script, or you have started creating a script and need to move forward, but are getting stuck.

In this case, you’ve got a goal in mind, you can make it work without Python, but you need to code. Where do you go? The most important step is verifying that you have a desirable outcome in mind. With a goal in mind, you can break your problem down into logical steps.

For example, let’s say you want to write a script that will clip one feature to another. First, try it in ArcMap. Does your data give you the expected results? Are you positive that this is the workflow you need? If so, then it’s time to review the documentation for the Clip tool and get the Python syntax that helps you do this. Check out the ArcGIS for Desktop Help page for this information, conveniently found here:Clip Tool Reference

Notice that the Syntax section tells you the name of the ArcPy function associated with the Geoprocessing tool, as well as the parameters needed (plus optional parameters) to make the function run properly. Keep an eye on those data types-- they can be tricky sometimes!

The Code Sample section is very useful for those of us who like to learn by example, so take a look at that section before you start coding; it may help demonstrate how to use the function even better than the Syntax section.5. Your script is not producing the expected results, and/or you suspect an ArcPy function is causing a problem.

I see this problem all the time, and I’ve come up with a solution that never fails to solve it. The following steps should help you learn more about the source of the issue.a. Whether you’re getting an error message, or your output is incorrect, you’ll benefit from isolating the code that is causing a problem. Make a script that demonstrates the “smallest testable case”.b. Troubleshooting a single ArcPy function is a lot easier than troubleshooting a 2,000-line script. Put the suspected function into its own .py file (and maybe even create some small test data), and verify that you’re getting the expected output from that function.c. Add some other code, if needed, but don’t forget that the purpose of this test is to isolate the issue down to a single line in your larger script. Any problem can be simplified, and taking a step back, instead of pushing forward, will save you a lot of debug time.6. Your code is not working, and you feel comfortable using Python to isolate the issue.

Here are some more advanced tips for troubleshooting your script:a. Test the function both in and out of the ArcMap or ArcCatalog Python console. This will indicate whether or not an integrated development environment (IDE) is properly accessing the Python.exe file installed with ArcGIS for Desktop. ArcMap and ArcCatalog should always be able to run ArcPy functions. If these programs cannot run ArcPy functions, a reinstallation might be necessary.b. Test the function with different data. This indicates whether the issue is data-specific or not.c. Check the online documentation specific to the version of ArcGIS for Desktop you are using. With each new release, some updates are made to the ArcPy site package that change the behavior of ArcPy’s functions.d. If you suspect that a function is incorrectly written, run the function in ArcMap or ArcCatalog, and use the Geoprocessing Results window to copy the Python version of that geoprocessing tool into the Windows Clipboard. Paste the results into your favorite IDE, and compare your code to the Esri-made Python snippet. It might be a little messier than your code, but comparing the two could give you a hint as to where the problem can be found.

I hope that helps! Remember, coding is a skill, and it can be really confusing sometimes. Be patient with yourself, and you’ll soon have all these troubleshooting steps integrated into each script-writing session you embark upon. Don’t be afraid to search the internet for solutions that others have come up with, and of course, feel free to give us a call here at Esri Support if you are current on maintenance. Good luck, and happy coding!
Andrew O. - Desktop Support Analyst -->

Learn by Reading

1. Learn Python the Hard Way (LPTHW)

Don't be scared by the title – Learn Python the Hard Way (LPTHW) is probably the most popular online tutorial for learning Python. In fact, many “Pythonistas” at Esri started learning Python with this website! This exercise-oriented guide reinforces its teachings through repetition and practical usage of basic Python functionality, even if you have no prior experience. I recommend completing the first 32 exercises to equip yourself with the knowledge you need.

2. Think Python: How to Think Like a Computer Scientist

I recommend this site for those users who are used to a more “textbook” approach to learning. The book is produced by O’Reilly and is now free to the public. The approach is different than LPTHW, and some people may find it to be less fun to learn from, but it’s packed with useful information. The chapters up to, and including, “Files”, will be the most useful for ArcPy users, but reading subsequent chapters will help you understand a little bit about what’s going on “under the hood” in a Python module such as ArcPy.

Learn by Watching

3. The New Boston

The videos in this series are perfect for their format. They are concise (each video runs about five minutes), thorough, and show practical coding examples that you can follow along with or practice on your own. In the first 31 videos, this series covers just about all of the concepts you will need for getting started.

4. Khan Academy

Khan Academy has made free education available to everyone for a variety of topics, and their Python course was one of the first “classes” offered. The curriculum is similar to what you can expect from a Computer Science class, so it may seem a little challenging at first. However, if you stick with it, you'll be thinking like a programmer.

Learn by Doing

5. Code Academy

This is an interactive Python course that teaches the basic concepts of Python by having you complete coding exercises. All the work is done online –you don't even need Python installed on your computer to complete it! The site prompts you to complete small lessons and challenges, and is great for those users who don't always have a consistent amount of time to contribute to studying. I would recommend completing the first 15 lessons if you want to understand more about Python.

Learn ArcPy

6. Esri Python Training

Esri's training site is the best way to see Python in action, apply it to ArcPy and GIS workflows, and hear some great questions from users like you and I. The courses are designed for users with any level of experience with Python and ArcPy, so you'll come back to this site time and again to learn new tricks or brush up on your skillset.

7. Python Scripting for ArcGIS

This is one of the only educational textbooks out there for learning the basics of ArcPy, and it helped me a lot when I started using ArcPy for the first time. The first four chapters are devoted to learning the ins-and-outs of basic Python functionality, and the rest of the book focuses on specific scripts. You'll get a cohesive and complete introduction to ArcPy as you work your way through this book.

In addition, this Support Services Blog post talks about learning ArcPy, and very nicely outlines all the resources that are out there. Once you've finished reading my blog entry, you can concentrate on the one above!Andrew O. – Desktop Support Analyst
Activity Feed
Contact Me
Online Status
Date Last Visited
‎11-11-2020 02:24 AM