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.)
-
What's wrong with just
import ptdraft.nibUpdate:
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
ptdraftto PYTHONPATHYou said that
import nibworked with you, that probably means that you addedptdraftitself (not its parent) to PYTHONPATH.vartec : +1 Why don't ppl just try before asking?cool-RR : 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.hasen j : 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.cool-RR : 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.hasen j : hmmm .. I guess you could ask that as a new question about setting up PYTHONPATH. -
For some reason, now simply
import nibworks (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.nibdid not work. -
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
0 comments:
Post a Comment