Python error ImportError: No module named
Python error ImportError: No module named
Based on your comments to orips post, I guess this is what happened:
- You edited
__init__.py
on windows. - The windows editor added something non-printing, perhaps a carriage-return (end-of-line in Windows is CR/LF; in unix it is LF only), or perhaps a CTRL-Z (windows end-of-file).
- You used WinSCP to copy the file to your unix box.
- WinSCP thought: This has something thats not basic text; Ill put a .bin extension to indicate binary data.
- The missing
__init__.py
(now called__init__.py.bin
) means python doesnt understand toolkit as a package. - You create
__init__.py
in the appropriate directory and everything works… ?
Does
(local directory)/site-packages/toolkit
have a __init__.py
?
To make import walk through your directories every directory must have a __init__.py
file.
Python error ImportError: No module named
I ran into something very similar when I did this exercise in LPTHW; I could never get Python to recognise that I had files in the directory I was calling from. But I was able to get it to work in the end. What I did, and what I recommend, is to try this:
(NOTE: From your initial post, I am assuming you are using an *NIX-based machine and are running things from the command line, so this advice is tailored to that. Since I run Ubuntu, this is what I did)
- Change directory (cd) to the directory above the directory where your files are. In this case, youre trying to run the
mountain.py
file, and trying to call thetoolkit.interface.py
module, which are in separate directories. In this case, you would go to the directory that contains paths to both those files (or in other words, the closest directory that the paths of both those files share). Which in this case is thetoolkit
directory. - When you are in the
tookit
directory, enter this line of code on your command line:export PYTHONPATH=.
This sets your PYTHONPATH to ., which basically means that your PYTHONPATH will now look for any called files within the directory you are currently in, (and more to the point, in the sub-directory branches of the directory you are in. So it doesnt just look in your current directory, but in all the directories that are in your current directory).
- After youve set your PYTHONPATH in the step above, run your module from your current directory (the
toolkit
directory). Python should now find and load the modules you specified.
Hope this helps. I was quite frustrated with this myself.