Python error "ImportError: No module named"
Python is installed in a local directory.
My directory tree looks like this:
My code is in here:
To run the example, I write
python mountain.py, and in the code I have:
from toolkit.interface import interface
And I get the error:
Traceback (most recent call last): File "mountain.py", line 28, in ? from toolkit.interface import interface ImportError: No module named toolkit.interface
I have already checked
sys.path and there I have the directory
/site-packages. Also, I have the file
__init__.py.bin in the toolkit folder to indicate to Python that this is a package. I also have a
__init__.py.bin in the examples directory.
I do not know why Python cannot find the file when it is in
sys.path. Any ideas? Can it be a permissions problem? Do I need some execution permission?
Based on your comments to orip's post, I guess this is what happened:
- You edited
- 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 that's not basic text; I'll put a .bin extension to indicate binary data."
- The missing
__init__.py.bin) means python doesn't understand toolkit as a package.
- You create
__init__.pyin the appropriate directory and everything works... ?
Read more... Read less...
To make import walk through your directories every directory must have a
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)
1) Change directory (cd) to the directory above the directory where your files are. In this case, you're trying to run the
mountain.py file, and trying to call the
toolkit.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 the
2) When you are in the
tookit directory, enter this line of code on your command line:
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 doesn't just look in your current directory, but in all the directories that are in your current directory).
3) After you've 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.
On *nix, also make sure that PYTHONPATH is configured correctly, especially that it has this format:
.: at the beginning, so that it can search on the current directory, too.)
It may also be in other locations, depending on the version:
.:/usr/lib/python .:/usr/lib/python2.6 .:/usr/lib/python2.7 and etc.
I solved my own problem, and I will write a summary of the things that were wrong and the solution:
The file needs to be called exactly
__init__.py. If the extension is different such as in my case
.py.bin then Python cannot move through the directories and then it cannot find the modules. To edit the files you need to use a Linux editor, such as vi or nano. If you use a Windows editor this will write some hidden characters.
Another problem that was affecting it was that I had another Python version installed by the root, so if someone is working with a local installation of python, be sure that the Python installation that is running the programs is the local Python. To check this, just do
which python, and see if the executable is the one that is in your local directory. If not, change the path, but be sure that the local Python directory is before than the other Python.
an easy solution is to install the module using
python -m pip install <library-name> instead of
pip install <library-name>
you may use sudo in case of admin restrictions