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

Jason Cunliffe Jason Cunliffe" <jasonic@nomadics.org
Thu, 19 Sep 2002 20:57:46 -0400


Hello

I've posted here on this before, and naturally received supportive answers. But
it just keeps coming up, especially after upgrading/reinstalling Python. Since
this is Edu-Sig, and we're on the topic of novice obstacles, I'd love
hear your fresh opinions...

1. I downloaded installed Leo, a new Python package. A simple process and it
runs great..yay! Leo even lets me open a familiar Python shell directly
[Idle/Tk] where I can successfully explore Leo's Python API examples:
Chapter 7: Scripting Leo with Python
http://personalpages.tds.net/~edream/scripting.html


2. The manual mentions I can invoke Leo from Python. So I launch Idle and try:

Python 2.2.1 (#34, Apr  9 2002, 19:34:33) [MSC 32 bit (Intel)] on win32
>>> import leo
>>> import leo
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in ?
    import leo
ImportError: No module named leo

>>> find leo
SyntaxError: invalid syntax

>>> find(leo)
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in ?
    find(leo)
NameError: name 'find' is not defined

3. arghh.. Clearly the problem is a simple one. My Python IDLE knows not where
'Leo' module is, but Leo does know where Python is. I've been through this
Python import pain enough times so I know how to fix it:

>>> import sys
>>> sys.path
['C:\\PYTHON22\\Tools\\idle', 'C:\\Python22', 'C:\\PYTHON22\\DLLs',
'C:\\PYTHON22\\lib', 'C:\\PYTHON22\\lib\\lib-tk',
'C:\\PYTHON22\\lib\\site-packages']
>>> sys.path.append('c:\\program files\\leo')
>>> import leo
>>> leo
<module 'leo' from 'c:\program files\leo\leo.pyc'>
>>>
ok :-)


4. Next time I launch Python from Idle, I have to go through this again, until I
learn
how to manage import paths via shell, in scripts and with persistent additions
to sys.path also.


5. For a novice I believe this is waay too obstructive. Its geeky, and
non-intuitive. I am *not* proposing to dumb down Python. But I am proposing to
give timely novice crucial help, where needed. The 'import leo' exercise
involves a very sophisticated Python application, but exactly the same is true
if I write the simplest 'helloworld.py' and save it almost anywhere on my
computer.


6. What to do? What do I propose?

I suggest that ImportError is #1 all-time Python novice gotcha. For god sakes
help people..

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.

Sophisticated Python Pros could disable these novice messages with a single
line:
novice-prompts = OFF
User logins, machine IPs and other contexts could be used also to invoke,
disable or switch the messages [LongFormTutorial, French, Korean, etc..]


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.

Since searching for 'helloworld' could be time consuming and error prone, the
"novice-help-prompt-config.py" could limit or prioritize the automated find
path. Just like any operating system find tool.

You may argue that students should learn to use find and be more responsible for
files. Of course they should, but it takes time and out-of-the-box Python is
unfriendly and terse. I argue, that to help true beginners, *Python* should
learn to "use 'find' and be responsible for files"!

Suggestion C. LONG-TERM & STRATEGIC HELP

Assuming students may want to avoid the error next time they launch Python, what
can we do? Well one idea is keep an error history, and parse it see if there are
repeats.
Another is provide help to reduce the problem directly by showing how.
For example show how to make this persistent:

sys.path.append('C:\\python\\edu-sig\\helloworld\\])

And next time student logs in and launches Python, the prompt can show what was
added to sys.path.

We cannot anticipate everything.. and don't need to.
That's why this is titled the "Top 5 All-Time Novice Obstacles"
Python could be much smarter and friendlier to novices. The tools and experience
are not lacking.


./Jason