Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Relative Imports

Reply
Thread Tools

Relative Imports

 
 
Pat O'Hara
Guest
Posts: n/a
 
      07-17-2007
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
 
Reply With Quote
 
 
 
 
Scott David Daniels
Guest
Posts: n/a
 
      07-18-2007
Pat O'Hara wrote:
> 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

--
--Scott David Daniels
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic imports + relative imports in Python 3 zildjohn01 Python 0 02-22-2011 05:24 PM
Imports System.Data or Imports System.Data.SqlClient? Albert ASP .Net 4 07-10-2008 09:00 AM
help with relative imports John Salerno Python 5 09-19-2006 07:20 PM
Relative imports Chris Python 4 03-05-2005 03:34 PM
Writing apps without using relative imports Randall Smith Python 0 12-03-2004 12:30 AM



Advertisments