0

I'm new in python. I have two py files like this: A.py

class LLL(object):
    pass

ak=LLL()
print(ak)

def f1():
    from B import tt

    print("hello")

if __name__=="__main__":
    f1()

and B.py

from A import ak
def tt():
    print("sss")

print(tt)

when i run A.py,i get the console below:

<__main__.LLL object at 0x0000000000B69BE0>
<A.LLL object at 0x0000000001461668>
<function tt at 0x000000000124AD90>
hello

My question is why ak create twice?In B.py,i want to get the ak created in A.py,not create a new ak,it looks like import B will run all A's code,how can i fix it?

frh10
  • 1
  • 2
  • The problem isn't that the imports are cyclic, it's that you're importing the same file you're running, which results it in being run twice (once as a script, and once as an import). – BrenBarn Nov 10 '16 at 03:43
  • Possible duplicate of [Circular (or cyclic) imports in Python](http://stackoverflow.com/questions/744373/circular-or-cyclic-imports-in-python) – Ken Y-N Nov 10 '16 at 03:46

0 Answers0