Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 40463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:01:39+00:00 2026-05-10T15:01:39+00:00

Imagine this directory structure: app/ __init__.py sub1/ __init__.py mod1.py sub2/ __init__.py mod2.py I’m coding

  • 0

Imagine this directory structure:

app/    __init__.py    sub1/       __init__.py       mod1.py    sub2/       __init__.py       mod2.py 

I’m coding mod1, and I need to import something from mod2. How should I do it?

I tried from ..sub2 import mod2, but I’m getting an "Attempted relative import in non-package".

I googled around, but I found only "sys.path manipulation" hacks. Isn’t there a clean way?


All my __init__.py‘s are currently empty

I’m trying to do this because sub2 contains classes that are shared across sub packages (sub1, subX, etc.).

The behaviour I’m looking for is the same as described in PEP 366 (thanks John B).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T15:01:40+00:00Added an answer on May 10, 2026 at 3:01 pm

    The problem is that you’re running the module as ‘__main__’ by passing the mod1.py as an argument to the interpreter.

    From PEP 328:

    Relative imports use a module’s __name__ attribute to determine that module’s position in the package hierarchy. If the module’s name does not contain any package information (e.g. it is set to ‘__main__’) then relative imports are resolved as if the module were a top level module, regardless of where the module is actually located on the file system.

    In Python 2.6, they’re adding the ability to reference modules relative to the main module. PEP 366 describes the change.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 131k
  • Answers 131k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Because the # is unescaped, the anchor #commentForm is actually… May 12, 2026 at 6:20 am
  • Editorial Team
    Editorial Team added an answer Two silly questions: are you sure your certificiate is installed… May 12, 2026 at 6:20 am
  • Editorial Team
    Editorial Team added an answer I'd probably opt for something like this: public abstract class… May 12, 2026 at 6:20 am

Related Questions

Imagine this directory structure: app/ __init__.py sub1/ __init__.py mod1.py sub2/ __init__.py mod2.py I'm coding
I have a problem migrating from Delphi 2006 to Delphi 2009. A Project which
This is something I've come across twice in the past month and I'm not
I am currently working on a project where a programmer who fancied themselves a
I'm creating a small application on an embedded device that has a boa web

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.