Mostly Linux & Python syntax notes and hyperlinks.

Thursday, March 15, 2012

python: ImportError: No module named slythy.toves

Why can't python see my module when it's right there?

I was copying code from one installation to another and thought I had everything.
But, the code ran on one system but not the other.

I finally found the difference: an empty (all comments) file called __init.py__

That is, if in twas/brillig.py you have "from slythy.toves import gyre"
and in twas/slythy/toves.py you define the gyre class,
you need a file called __init.py__ in the twas/slythy/ directory, or you'll get an error like:

    ImportError: No module named slythy.toves
    Python Error: Undefined Python module brillig


There are definite disadvantages to learning python piece-by-piece as you go along.  I was checking environment variables and everything before finally finding out about __init.py__


No comments:

Post a Comment