Relative Imports

Discussion in 'Python' started by Pat O'Hara, Jul 17, 2007.

  1. Pat O'Hara

    Pat O'Hara Guest

    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
    Pat O'Hara, Jul 17, 2007
    #1
    1. Advertising

  2. 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
    Scott David Daniels, Jul 18, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Randall Smith

    Writing apps without using relative imports

    Randall Smith, Dec 3, 2004, in forum: Python
    Replies:
    0
    Views:
    290
    Randall Smith
    Dec 3, 2004
  2. Chris

    Relative imports

    Chris, Mar 5, 2005, in forum: Python
    Replies:
    4
    Views:
    552
    Kent Johnson
    Mar 5, 2005
  3. Albert
    Replies:
    4
    Views:
    10,841
    Albert
    Jul 10, 2008
  4. zildjohn01
    Replies:
    0
    Views:
    636
    zildjohn01
    Feb 22, 2011
  5. Victor Hooi
    Replies:
    1
    Views:
    101
    Devin Jeanpierre
    Nov 25, 2013
Loading...

Share This Page