Relative Imports

P

Pat O'Hara

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 Python 2.5 on WinXP, and I'm trying to do a relative import.
Here's the package structure

A/
__init__.py
aneededmodule.py
[some more modules]
B/
__init__.py
anothermodule.py

anothermodule.py needs to use aneededmodule.py; package A's __init__.py
looks like this:

from aneededmodule import somestuff

My problem is that when anothermodule tries to import ..aneededmodule or
...somestuff (because somestuff was imported into __init__), I get a
ValueError: Attempted relative import in non-package.

What's my problem? This seems like something very trivial, but I've
never had to use python for a project of this size before, so I've never
dealt with this.

Thanks for your help,
-Pat
 
S

Scott David Daniels

Pat said:
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 Python 2.5 on WinXP, and I'm trying to do a relative import.
Here's the package structure

A/
__init__.py
aneededmodule.py
[some more modules]
B/
__init__.py
anothermodule.py

anothermodule.py needs to use aneededmodule.py; package A's __init__.py
looks like this:

from aneededmodule import somestuff

My problem is that when anothermodule tries to import ..aneededmodule or
..somestuff (because somestuff was imported into __init__), I get a
ValueError: Attempted relative import in non-package.

What's my problem? This seems like something very trivial, but I've
never had to use python for a project of this size before, so I've never
dealt with this.

Thanks for your help,
-Pat
My guess (without seeing your code or error messages; shame on you) is
that you are running A/B/anothermodule.py; not -m A.B.anothermodule
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top