[Edu-sig] Top 5 All Time Novice Obstacles => #1 ImportError: No module named xyz

Guido van Rossum guido@python.org
Fri, 20 Sep 2002 09:47:11 -0400


> Suggestion A. HELPFUL ERROR PROMPT
> 
> Instead of
> 
> >>> import helloworld
> Traceback (most recent call last):
>   File "<pyshell#0>", line 1, in ?
>     import leo
> ImportError: No module named helloworld
> 
> How about
> 
> >>> import helloworld
> Traceback (most recent call last):
>   File "<pyshell#0>", line 1, in ?
>     import leo
> ImportError: No module named helloworld
> 
> *** Python needs to know where "helloworld" is located.
>     import sys
>     sys.path # Shows which directories Python knows to search and load from.
>     You must add the path where "helloworld" is located, then try import again
>     sys.path.append('C:\\path-to-hello=world\\')
> 
> I am sure you can improve upon this help prompt. Python programmers
> know perfectly well what to do. It in the tutorials and docs. But
> why not serve it up for beginners.
> A "novice-help-prompt-config.py" file could be bundled with Python
> to allow people to control and modify it. For example a teacher
> could add urls to docs, on-line courseware notes. Also localize the
> paths advice better to reflect local operating system locations more
> cleanly.

I expect that even for novices (unless they type perfectly), this long
message gets old quickly -- the much more common case surely is a typo
in the module name.

The solution you suggest above also isn't always right.  More often
than not, moving the file into the path is te better solution.
There's also the issue that when the module is an extension, there's
no .py file (a common newbie issue is frustration about not being able
to find, e.g. "math.py" when "import math" fails).

And it's possible that the path is entirely screwed up -- if "import
math" doesn't work, your setup is hosed and it will take an expert to
unhose it.

Perhaps we could tweak IDLE to add a single line to the ImportError
message, something like "type help('import') for help on importing
modules" and the help section for the import keyword could contain a
treatise on a variety of import issues.

I'd also suggest that Leo could have installed its package in the
python standard library directory rather than installing itself in a
totally separate directory.

> Suggestion B. ACTION ERROR PROMPT
> 
> This version says don't just suggest, do something.
> In other words run 'import sys' and 'sys.path'.
> Then ask the user if Python should search the system for possible
> locations of 'helloworld'
> If Python finds any, return a visible command list and ask which one
> to invoke, always using the official  sys.path.append() syntax so
> they can learn the syntax pattern quickly.

Much too complicated.

--Guido van Rossum (home page: http://www.python.org/~guido/)