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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:30:10+00:00 2026-06-08T08:30:10+00:00

Words The structure is as follows: a module test contains two submodules test.foo and

  • 0

Words

The structure is as follows: a module test contains two submodules test.foo and test.bar.

test.foo has a function inc() that uses test.bar.bar() so based on the python documentation from . import bar is the proper way to include that, and this works as expected.

test.bar however, also has a function inc2 that uses test.foo.foo, but when from . import foo is used, both of these modules break.

What is the correct method for achieving this? I’ve found little in the python docs or searching.

Code

test/_init_.py

#empty

test/foo.py

from . import bar

def foo():
    print("I do foo")

def inc():
    print(bar.bar())

test/bar.py

from . import foo

def bar():
    print("I do bar")

def inc2():
    print(foo.foo())

Error 1

>>> import test.foo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "test/foo.py", line 1, in <module>
    from . import bar
  File "test/bar.py", line 1, in <module>
    from . import foo
ImportError: cannot import name foo

Error 2

>>> import test.bar
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "test/bar.py", line 1, in <module>
    from . import foo
  File "test/foo.py", line 1, in <module>
    from . import bar
ImportError: cannot import name bar
  • 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-08T08:30:12+00:00Added an answer on June 8, 2026 at 8:30 am

    The solution is to factor out code needed by both modules into a third module which is imported by both. For instance, put the foo function into a third module.

    There are many previous StackOverflow questions about this, e.g., Circular import dependency in Python . See also http://effbot.org/zone/import-confusion.htm#circular-imports .

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

Sidebar

Related Questions

I have a hierarchical list of objects. Assume that the structure is as follows:
I have a MySQL table that contains many rows. The table structure is as
I have a string that has the following structure: ABC_ABC_PQR_XYZ Where PQR has the
My current directory structure is as follows: C:\xampp\htdocs\PHP_Upload_Image_MKDIR In other words, the following directories
What would be the best data structure to store all the words of a
Problem in words: For my application, I have a class that reads from a
I am having a structure that is actually a simple byte with more functionality.
I have a Ring structure implemented as follows (based on a cookbook recipe I
I am currently working on a little application: The structure follows in some way
The program has two javascript functions to generate a table ( generateTable ) and

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.