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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:04:06+00:00 2026-06-11T07:04:06+00:00

Possible Duplicate: Cyclic module dependencies and relative imports in Python Consider the following example

  • 0

Possible Duplicate:
Cyclic module dependencies and relative imports in Python

Consider the following example of cyclic imports in python:

main.py:

from pkg import foo

pkg/__init.py__:

# empty

pkg/foo.py:

from pkg import bar

pkg/bar.py:

from pkg import foo

Running main.py will cause an exception:

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

Changing bar.py to:

# from pkg import foo
import pkg.foo 

will make the example work.

Why does this happen? Shouldn’t *import package.module” and “from package import module” be equivalent (except for the extra name binding in the latter)?

  • 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-11T07:04:07+00:00Added an answer on June 11, 2026 at 7:04 am

    First of all you have to understand that:

    • The sys.modules variable play the role of a cache in the import mechanize which mean that if we import a module the first time, an entry containing the name of this module is added to sys.modules, so that when we try to import this same module next time, we will only get the already cached module from sys.modules and we will not execute the module again.

    • The difference between import pkg.foo and from pkg import foo is that the second is equivalent too: import pkg.foo followed by getattr(pkg, 'foo'). (check my answer here for more detail)

    Now what happen in your first example is the following:

    1- In main.py: we start by executing the line from pkg import foo, so first the entry pkg.foo is added to sys.modules (i.e. 'pkg.foo' in sys.modules == True) than we try to import foo.

    2- In foo.py: While importing foo we end up executing this line: from pkg import bar and again the entry pkg.bar is added to sys.modules; and than we start importing bar, what is important to note here is that we are still executing foo.py, so we are kind off still executing this line from pkg import foo from main.py.

    3- In bar.py: while now importing bar.py we end up executing the line: from pkg import foo, but remember we have already an entry in sys.modules for pkg.foo which mean that executing foo.py will be skipped and the import mechanize will get us instead the sys.modules['pkg.foo'] entry, and now we will excute getattr(pkg, 'foo') as explained above, but remember ‘pkg.foo’ didn’t finish importing; which mean that there is no attribute foo which explain the error.

    Now if you put in bar.py only import pkg.foo this will not have the line that cause us the problem last time which is: getattr(pkg, 'foo').

    HTH,

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

Sidebar

Related Questions

Possible Duplicate: Circular (or cyclic) imports in Python I have class B that imports
Possible Duplicate: Python: Circular (or cyclic) imports I'm new to Python, and I'm having
Possible Duplicate: Unload a module in Python After importing Numpy, lets say I want
Possible Duplicate: Singleton: How should it be used Following on from Ewan Makepeace 's
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Python : how to append new elements in a list of list?
Possible Duplicate: How to call a JavaScript function from PHP? I have a php
Possible Duplicate: Extracting dollar amounts from existing sql data? I have a column in
Possible Duplicate: Unable to get a list of installed Python modules How do I
Possible Duplicate: Matlab gives wrong answer Can anyone explain to me why the following

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.