Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python error "ImportError: No module named"

Python is installed in a local directory.

My directory tree looks like this:

(local directory)/site-packages/toolkit/interface.py 

My code is in here:

(local directory)/site-packages/toolkit/examples/mountain.py 

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?

like image 627
Eduardo Avatar asked Dec 03 '08 21:12

Eduardo


People also ask

How do I fix the ImportError No module named error in Python?

To get rid of this error “ImportError: No module named”, you just need to create __init__.py in the appropriate directory and everything will work fine.

How do I fix Python module not found?

Python's ImportError ( ModuleNotFoundError ) indicates that you tried to import a module that Python doesn't find. It can usually be eliminated by adding a file named __init__.py to the directory and then adding this directory to $PYTHONPATH .

What is ImportError in Python?

ImportError is raised when a module, or member of a module, cannot be imported. There are a two conditions where an ImportError might be raised. If a module does not exist.


1 Answers

Based on your comments to orip's post, I guess this is what happened:

  1. You edited __init__.py on windows.
  2. 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).
  3. You used WinSCP to copy the file to your unix box.
  4. WinSCP thought: "This has something that's not basic text; I'll put a .bin extension to indicate binary data."
  5. The missing __init__.py (now called __init__.py.bin) means python doesn't understand toolkit as a package.
  6. You create __init__.py in the appropriate directory and everything works... ?
like image 89
John Fouhy Avatar answered Sep 19 '22 12:09

John Fouhy