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 1054521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:25:13+00:00 2026-05-16T17:25:13+00:00

Usage of relative imports in Python has one drawback; you will not be able

  • 0

Usage of relative imports in Python has one drawback; you will not be able to run the modules as standalones anymore, because you will get an exception:

ValueError: Attempted relative import in non-package

Code

# /test.py: just a sample file importing foo module
import foo
...

# /foo/foo.py:
from . import bar
...
if __name__ == "__main__":
   pass

# /foo/bar.py: a submodule of foo, used by foo.py
from . import foo
...
if __name__ == "__main__":
   pass

How should I modify the sample code in order to be able to execute all of the following? test.py, foo.py and bar.py.

I’m looking for a solution that works with Python 2.6+ (including 3.x).

  • 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-05-16T17:25:14+00:00Added an answer on May 16, 2026 at 5:25 pm

    First, I assume you realize what you’ve written would lead to a circular import issue, because foo imports bar and viceversa; try adding

    from foo import bar
    

    to test.py, and you’ll see it fails. The example must be changed in order to work.

    So, what you’re asking is really to fallback to absolute import when relative import fails; in fact, if you’re executing foo.py or bar.py as the main module, the other modules will just lie at the root level, and if they share the name with another module on the system which one will be picked depends on the order in sys.path. Since the current dir is usually the first, local modules will be picked if available – i.e., if you’ve got an ‘os.py’ file in the current working dir, it’ll be picked instead of the builtin one.

    A possibile suggestion is:

    foo.py

    try:
        from . import bar
    except ValueError:
        import bar
    
    if __name__ == "__main__":
        pass
    

    bar.py:

    if __name__ == "__main__":
        pass
    

    By the way calling scripts from the proper position is usually way better.

    python -m foo.bar
    

    Is probably the best way to go. This runs the module as a script.

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

Sidebar

Related Questions

I am trying to find the relative memory usage of each running application and
I don't understand usage for these: msleep and msleep_interruptible ... I know one interruptible
Example usage that works on Ubuntu and other forms of Linux but not on
To maximize CPU usage (I run things on a Debian Lenny in EC2) I
Will usage of LINQ increase day by day or is it that some organizations
My memory usage increases over time and restarting Django is not kind to users.
The usage case is that one application generates an event and sends out a
Mapkit usage, here is my scenario: When the user chooses one of the pushpins
I know usage of Zend_Db_Select is optional, but I was wondering if there was
This question shows the usage of **var = new Object(); and that's something totally

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.