473,396 Members | 1,833 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Relative import problem

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
Apr 19 '07 #1
2 1450
On Apr 19, 11:54 am, "Jorgen Bodde" <jorgen.maill...@gmail.comwrote:
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/Coo.../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

Apr 19 '07 #2
On Apr 19, 6:54 pm, "Jorgen Bodde" <jorgen.maill...@gmail.comwrote:
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

Apr 19 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: geoffblanduk_nospam | last post by:
Using Apache Tomcat 5.0.28 I'm having a problem with the xsl:import command. I have several Tomcat running on the same server (they could be different releases of code so can't share resources)....
0
by: Anders J. Munch | last post by:
Now 2.5 is out, and we have a syntax for explicit relative imports (PEP 328, http://www.python.org/dev/peps/pep-0328/, in case anyone wasn't paying attention). The long-term plan is that the...
1
by: Alexey Borzenkov | last post by:
After reading PEP-0328 I wanted to give relative imports a try: # somepkg/__init__.py <empty> # somepkg/test1.py from __future__ import absolute_import from . import test2 if __name__ ==...
0
by: Alan Isaac | last post by:
What is the recommended packaging of demo scripts or test scripts for a package that has modules that use relative imports? Example: Suppose I have the package structure: package/...
1
by: Pat O'Hara | last post by:
Hey guys, I know this is a really stupid question, but I've tried googling and nothing came up. I also tried IRC, but it was too crowded and I didn't get much useful information. I'm using...
0
by: Kay Schluehr | last post by:
Since their introduction in Python 2.5 I only reviewed the new "relative import" notation briefly by reading the "What's new in Python 2.5" article. Now I wanted checkout if I get comfortable with...
10
by: test | last post by:
basic noob question here. i am trying to reference a package, i have the structure: mypack/ __init__.py test.py subdir1/ __init__.py mod1.py
4
by: DG | last post by:
Alright, I have searched and searched and read many conversations on the topic of relative and absolute imports and am still not getting the whole thing through my skull. Highlights of what I've...
0
by: Stef Mientki | last post by:
hello, I'm running Python 2.5 and want my programs to run at least under Windows and Linux (preferable also Mac). So I guess I should always use relative paths. From most modules I can call a...
0
by: Gabriel Genellina | last post by:
En Sat, 18 Oct 2008 05:52:04 -0300, Stef Mientki <stef.mientki@gmail.com> escribió: Why don't you let the caller tell you its own location, using __file__? The above code is too much magic for...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.