views:

15578

answers:

5

Hello, I am very new at python and I am getting this error:

Traceback (most recent call last):
  File "mountain.py", line 28, in ?
    from toolkit.interface import interface
ImportError: No module named toolkit.interface

Python is installed in a local directory:

My directorie tree is 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 do python mountain.py, and in the code I have:

from toolkit.interface import interface

And i get the error that I wrote, I have already checked the sys.path and in the sys.path 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 why Python cannot find the file when is in the sys.path, any ideas?. Can be a permissions problem?, Do I need execution permission?.

Thanks.

+1  A: 

To mark a directory as a package you need a file named __init__.py, does this help?

orip
I already have a file called __init__.py.bin, If I change the name to __init__.py, then I get this error:/__init__.py", line 1 "utilities", "demo"] ^SyntaxError: invalid syntax
Eduardo
What's in __init__.py? Post that as part of your question, please.
S.Lott
There is nothing, it is empty, It was with the package that I download, do I need to write something in the file?.
Eduardo
@S.Lott: you don't need to put anything in your __init__.py right?
igorgue
@Eduardo. Your __init__.py gets an error. And you say it's empty. That's difficult to reconcile. And it can't be called __init__.py.bin -- Python would ignore this file. Typically, it can have nothing in it.
S.Lott
You were right Lott I edited the file with a windows editor and it wrote some hide characters, now I have create new files with vi.
Eduardo
@Eduardo: You may want to get Programmer's Notepad or Komodo Edit or something that edits pure text with no hidden special characters. They may be easier to use than vi.
S.Lott
Thanks for the advice I usually use nano for edition, the reason why I used a windows editor is because I am working in a cluster, and I use WinSCP to move files and sometimes I use that editor for change files.
Eduardo
+2  A: 

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.

igorgue
+4  A: 

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... ?
John Fouhy
ah i love detective work
Claudiu
+2  A: 

Hello, now the problem is solved, I will write a resume of the things that were grong and the solution:

The file needs to be called __init__.py, exactly that, if the extension is different such as 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, nano..., if you use a windows editor this will write some hidden characters.

Another problem that was affecting was that I had another python version installed by the root, so if someone is working with a locally installation of python, be sure that the python 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.

Thanks to all for the help

Eduardo
A: 

Yup. You need the directory to contain the init.py file, which is the file that initializes the package. Here, have a look at this.

The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later.

miya