1

I got a weird problem that I wasn't been able to find answer all over the internet (or I don't know how to ask).

I have module AAA.py

from BBB import BBB
class AAA():
    def test(self):
        print 'AAAA'
        a = BBB()

and module BBB.py

class BBB():
    def __init__(self):
        print 'BBB'

then when I call

a = AAA()
a.test()

everything works as expected and I see output

AAAA
BBB

BUT when I try to import and use class AAA from module BBB.py

from AAA import AAA
class BBB():
    def __init__(self):
        print 'BBB'

I get following error

ImportError: cannot import name AAA

Any suggestions? I cant create circular dependencies in Python? I am using version Python 2.7.6 on Ubuntu

5
  • 3
    shouldn't it be from AAA import AAA ? Jul 15, 2015 at 11:55
  • yes sorry, thats a typo, but thet doesnt solve my problem
    – Wax Cage
    Jul 15, 2015 at 12:04
  • Can you please put full traceback? Jul 15, 2015 at 12:07
  • 1
    @WaxCage did you read stackoverflow.com/questions/10113383/…?
    – bufh
    Jul 15, 2015 at 12:07
  • Is it so that I cant use circular dependencies? In the question you sent me they write that its a sign of bad design, but what if I got some Log class and Environment class, In the Log class I need Environment to know, what level of logging to use and in Environment i want to log... whatever...
    – Wax Cage
    Jul 15, 2015 at 12:14

2 Answers 2

1

Indeed - if AAA.py imports something from BBB.py at top level and vice versa, it doesn't work as intended.

There are two ways you can solve it:

  1. Import the modules from each other. This way they are both present as their namespace and will be filled during the import process.

    So just do import BBB and use BBB.BBB() for instantiating the class:

    import BBB
    class AAA():
        def test(self):
            print 'AAAA'
            a = BBB.BBB()
    
  2. Do the import where you need it:

    class AAA():
        def test(self):
            from BBB import BBB
            print 'AAAA'
            a = BBB()
    

    This way the link between the two modules is "looser" and not so tight.

1

When you want to use a module in another one, you have to import it and so use import your_module. You have to type your_module.foo() if you want to use a method inside it. With the instruction from your_module import attr1, foo1, [...] you are modifying the module's global variables, so that you can use attr1 or the method foo1 as if they were in your module. A concrete example is: if you want to use the math module, you type import math and when you want to use the constant pi you type math.pi, but if you are sure there will be no clash with the other names, you'll type from math import pi and you will use the constant pi as if you declared it in your module.

3
  • 1
    I don't understand how that answers my question?
    – Wax Cage
    Jul 15, 2015 at 12:21
  • 1
    Oh, you were asking for circular import. I didn't get it Jul 15, 2015 at 12:28
  • Well I really didn't understood :D, it would be better to follow my example - I followed your instructions and instead of importing from AAA import AAA (in BBB module) I imported whole AAA module (import AAA) and it started working. Thanks a lot!
    – Wax Cage
    Jul 15, 2015 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.