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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:13:24+00:00 2026-06-05T18:13:24+00:00

I have a package like this package/ __init__.py subpackage1/ __init__.py moduleA.py moduleB.py moduleC.py moduleD.py

  • 0

I have a package like this

package/
    __init__.py
    subpackage1/
        __init__.py
        moduleA.py
        moduleB.py
        moduleC.py
        moduleD.py
    subpackage2/
       __init__.py
       moduleX.py
       moduleY.py
       moduleZ.py

In moduleB.py, I am importing

from moduleA import bar

In moduleA, I am importing

from moduleB import foo

I am getting ImportError.

ImportError: cannot import name foo

What could be the problem here ? and to avoid this problem, what should I do ? and what should I write in _init_.py pf package, subpackage1, subpackage2 ?

_init_.py of subpackage1

from moduleA import *
from moduleB import *
from moudleC import *
from moudleD import *

_init_.py of subpackage2

from moduleX import *
from moduleY import *
from moduleZ import *

_init_.py of package

from subpackage1 import *
from subpackage2 import *

Is there some problem with my _init_.py files ?

EDIT:
I have changed imports

moduleB

from .moduleA import bar

moduleA

from .moduleB import foo

Still, I am getting the same import error.

ImportError: cannot import name foo

EDIT:

moduleB

def Bar():
    def __init__(self):
        self.foo = Foo()
        self.val = 10
        .
        .

moduleA

def Foo():
    def __init__(self):
        self.bar = Bar()
        self.val = 5
        .
        .   

I want to do this. And I insist on keeping both classes in different files. How should I import ?

  • 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-05T18:13:26+00:00Added an answer on June 5, 2026 at 6:13 pm

    It actually appears to be a problem with circular imports.

    Your moduleB says “from moduleA import bar”, which tries to load moduleA, but the first thing it encounters in moduleA is “from moduleB import foo”, which sends it back to moduleB. So you have an unresolvable bit of circular recursion there.

    Typically (but not always) a circular import is an indicator that you need to rethink or re-architect how you’re doing things. There are, however, a few possible work-arounds out there.

    One is to move the import statement to the bottom of your python file (assuming you’re using foo or bar inside of another function, so its not getting called instantly when the file loads)

    e.g.

    #ModuleB.py 
    class Bar(object):   
      def __init__(self):
        self.foo = Foo()
        self.val = 10
        .
        .
    # at bottom of file
    from moduleA import Foo
    

    another alternative is to place the import statement inside a function, called the “lazy import” pattern:

    #ModuleB.py 
    class Bar(object):   
      def __init__(self):
        from moduleA import Foo
        self.foo = Foo()
        self.val = 10
    

    As to your question about the __init__.py files. I see no reason why you wouldn’t leave them empty. The empty __init__.py files simply tell python “this directory is a python package” and permit imports.

    Typically you would have a file in your package directory that “runs” your program by importing and utilizing modules from the subpackages. So assuming such file (e.g., package/main.py) exists, your imports would look like the following, with just empty __init__.py files.

    #package/main.py
    from subpackage1.moduleA import bar  # you can now call bar() directly
    from subpackage1 import moduleB  # you can now call foo like: moduleB.foo()
    from subpackage2.moduleX import jah 
    

    What you’re doing above essentially takes all of the functions and attributes of all of the Modules in each subpackage and makes them available directly on the subpackage as if they were the subpackage’s functions and attributes (so you could import subpackage1 and call subpackage1.bar() and subpackage.foo() instead of subpackage.moduleA.bar(), etc.), but I don’t get the impression thats what you were trying to do, necessarily, and there’s probably no reason to do it in this case.

    If you need to use something in subpackage2 in a module in subpackage1, see the answers to this question. Or google how to add directories to your python path.

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

Sidebar

Related Questions

If I have a directory structure like this: package/ __init__.py functions.py #contains do() classes.py
I have a package of plug-in style modules. It looks like this: /Plugins /Plugins/__init__.py
I have a base class like this: package MyClass; use vars qw/$ME list of
I have a Controller defined like this: package controllers import play.api._ import play.api.mvc._ import
I have this folder structure: package/ __init__.py misc/ __init__.py tools.py subpackage/ __init__.py submodule.py I
Say I have a package called Foo organized this way: Foo\ __init__.py foo.py bar.py
I have a directory structure that looks like this: project/ __init__.py foo/ __init.py__ first.py
I'm considering a package implementation set up like this: wordproc __init__.py _generic.py gedit.py oofice.py
Say i have this this structure. MyApp ├── main.py └── package ├── __init__.py ├──
I have a JSON string that looks like this: { package1: { type: envelope,

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.