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

  • SEARCH
  • Home
  • 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 9059999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:06:44+00:00 2026-06-16T15:06:44+00:00

Given a directory structure like this main/ common/ foo.py A/ src/ bar.py How can

  • 0

Given a directory structure like this

main/
    common/
        foo.py
    A/
        src/
            bar.py

How can I use Python’s relative imports to import foo from bar? I’ve got a working solution by adding it to the path, but this is ugly. Is there a way to simply do with a single import in Python 2.7?

This is a more complex version of this question:

Importing modules from parent folder

  • 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. Editorial Team
    Editorial Team
    2026-06-16T15:06:45+00:00Added an answer on June 16, 2026 at 3:06 pm

    The correct relative import would be this:

    from ...common import foo
    

    However, relative imports are only meant to work within one package. If main is a package, then you can use relative imports here. If main is not a package, you cannot.

    Thus, if you’re running a script in /main/ and doing something like import A.src.bar, then that relative import will fail with “Attempted relative import beyond toplevel package”. This is because the relative import is trying to import something outside of the toplevel package A.

    However, if you’re running a script in / and doing something like import main.A.src.bar, then that relative import will succeed because main is now a package. In that case, the following two would be equivalent:

    from ...common import foo
    from main.common import foo
    

    To answer your comment: the meaning of the . doesn’t change depending on where the script was run from, it changes depending on what the package structure is.

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

Sidebar

Related Questions

This is annoying. I have a directory structure like this -lib --some jar files
I have this application that will recurse all folders in a given directory and
How can I get all sub-directories of a given directory without files, . (current
Given virtual directory and port can you find the actual path of a web
I want to create a XMLDom of a Directory structure that looks like the
My directory structure is something like below > Root > -Admin // admin area
I've a quite complex application, this application is structured like so: Main.exe (is a
As the title says, how can I recursively copy a directory structure but only
i am trying to traverse a given directory and create a list of files
I am trying to check for a specific file in a given directory. I

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.