Skip to content
Fix Code Error

Python error “ImportError: No module named”

March 13, 2021 by Code Error
Posted By: Eduardo

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?

Solution

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… ?
Answered By: John Fouhy

Related Articles

  • How to avoid "module not found" error while calling scrapy…
  • Error 'Map', but got one of type 'Null' flutter web with…
  • What's the difference between eval, exec, and compile?
  • Can't install via pip because of egg_info error
  • error LNK2005: ✘✘✘ already defined in…
  • coercing to Unicode: need string or buffer, NoneType found…
  • Ukkonen's suffix tree algorithm in plain English
  • Usage of __slots__?
  • How should I deal with "package '✘✘✘' is not available (for…
  • Relative imports for the billionth time

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

&& (AND) and || (OR) in IF statements

Next Post:

Convert a Unix timestamp to time in JavaScript

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Get code errors & solutions at akashmittal.com
© 2022 Fix Code Error