Relative import problem

J

Jorgen Bodde

Hi all,

I want to structure my app so that I have two dirs like;

obj/{object files}

gui/{gui files}

Here comes the catch. From the GUI dir, I would like to access the obj
submodule path. I need to go one dir back.. I read there was something
like from .. import x in python 2.5 so that I could access my obj dir
from a lower level, but I have problems getting this to work.

Example;

In the main dir I have;

main.py:
--------
import gui

gui.gui.startme()
--------

In the obj dir I have;

obj/obj.py
---------

def start():
print 'Started OBJ'
---------

In the GUI dir I have

gui/gui.py
---------
from .. import obj

def start():
obj.obj.start()
---------

This does not work. It gives me;

D:\personal\src\GuitarPortfolio\tmp>python start.py
Traceback (most recent call last):
File "start.py", line 4, in <module>
gui.gui.start()
AttributeError: 'module' object has no attribute 'gui'

Am I shooting myself in the foot by trying to structure? Or are there
better more elegant ways?

Regards,
- Jorgen
 
K

kyosohma

Hi all,

I want to structure my app so that I have two dirs like;

obj/{object files}

gui/{gui files}

Here comes the catch. From the GUI dir, I would like to access the obj
submodule path. I need to go one dir back.. I read there was something
like from .. import x in python 2.5 so that I could access my obj dir
from a lower level, but I have problems getting this to work.

Example;

In the main dir I have;

main.py:
--------
import gui

gui.gui.startme()
--------

In the obj dir I have;

obj/obj.py
---------

def start():
print 'Started OBJ'
---------

In the GUI dir I have

gui/gui.py
---------
from .. import obj

def start():
obj.obj.start()
---------

This does not work. It gives me;

D:\personal\src\GuitarPortfolio\tmp>python start.py
Traceback (most recent call last):
File "start.py", line 4, in <module>
gui.gui.start()
AttributeError: 'module' object has no attribute 'gui'

Am I shooting myself in the foot by trying to structure? Or are there
better more elegant ways?

Regards,
- Jorgen

I'm not finding much info on this subject. But here's the most
interesting links I've found as of yet:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/456362
http://www.thescripts.com/forum/thread549516.html
http://www.thescripts.com/forum/thread42319.html
http://docs.python.org/tut/node8.html

Dunno if any of these will help you though. Sorry.

Mike
 
M

Martin

Hi all,

I want to structure my app so that I have two dirs like;

obj/{object files}

gui/{gui files}

Here comes the catch. From the GUI dir, I would like to access the obj
submodule path. I need to go one dir back.. I read there was something
like from .. import x in python 2.5 so that I could access my obj dir
from a lower level, but I have problems getting this to work.

Example;

In the main dir I have;

main.py:
--------
import gui

gui.gui.startme()
--------

In the obj dir I have;

obj/obj.py
---------

def start():
print 'Started OBJ'
---------

In the GUI dir I have

gui/gui.py
---------
from .. import obj

def start():
obj.obj.start()
---------

This does not work. It gives me;

D:\personal\src\GuitarPortfolio\tmp>python start.py
Traceback (most recent call last):
File "start.py", line 4, in <module>
gui.gui.start()
AttributeError: 'module' object has no attribute 'gui'

Am I shooting myself in the foot by trying to structure? Or are there
better more elegant ways?

Regards,
- Jorgen

You need to add the path to where your files are located since they
are not in any of the "standard" path directories where Python looks.
To do this, you can add the following lines in your files:

import sys
sys.path.append(/path/to/obj)
sys.path.append(/path/to/gui)

import obj
import gui

#etc..

Hope this helps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,169
Latest member
ArturoOlne
Top