views:

2821

answers:

3

I am running Python 2.5.

This is my folder tree:

ptdraft/
  nib.py
  simulations/
    life/
      life.py

(I also have __init__.py in each folder, omitted here for readability)

How do I import the nib module from inside the life module? I am hoping it is possible to do without tinkering with sys.path.

(Note: The main module being ran is in the ptdraft folder.)

+2  A: 

What's wrong with just import ptdraft.nib

Update:

It seems that the problem is not related to the module being in a parent directory or anything like that.

You need to add the directory that contains ptdraft to PYTHONPATH

You said that import nib worked with you, that probably means that you added ptdraft itself (not its parent) to PYTHONPATH.

hasen j
+1 Why don't ppl just try before asking?
vartec
I did try that, it gives:`ImportError: No module named ptdraft.nib`
cool-RR
Read the update, I understand. But now I ask: Is it okay that my PYTHONPATH is set up like that? I didn't manually set it up, I'm working with Eclipse.
cool-RR
Whether it's ok or not depends mostly on you. If you're going to publish this though, I'd say it's not quite ok. I really don't know how to setup the PYTHONPATH in eclipse.
hasen j
I don't understand, hasen. Regardless of IDE, I have a folder with a bunch of Python files in it. If I double click the main one from explorer, it's supposed to run properly, isn't it? So who determines the PYTHONPATH in that case? Because the `import nib` version works when I double click it.
cool-RR
hmmm .. I guess you could ask that as a new question about setting up PYTHONPATH.
hasen j
A: 

For some reason, now simply import nib works (I am sure I tried that at some point, I tried a lot of different things. Maybe I changed something that I don't remember.)

As I commented, import ptdraft.nib did not work.

cool-RR
+5  A: 

You could use relative imports (python >= 2.5):

from ... import nib

(What’s New in Python 2.5) PEP 328: Absolute and Relative Imports

EDIT: added another dot '.' to go up two packages

f3lix