Circular import issues for Python 1-2 modules

Source: Internet
Author: User

run.py File:

Import M1 # first time importing

# Verify solution One:

‘‘‘

Importing M1

Importing M2

‘‘‘

# Print (m1.x)

# Print (M1.Y)

# Verify solution Two:

Import M1 # first time importing

‘‘‘

Importing M1

‘‘‘

# Print (m1.x)

# Print (M1.F1)

M1.F1 ()

‘‘‘

Importing M2

M1.F1---->Y:M2

M2.F2--->x:m1

‘‘‘

m1.py File:

# Error Demonstration

‘‘‘

print (' importing M1 ')

From m2 Import y # import m2 for the first time

X= ' M1 '

‘‘‘

Solution One: Put the loop-imported statement behind the name definition

Print (' importing M1 ')

x=' M1 '

from M2 import y # first time importing M2

# Solution Two: Put the loop import statement inside the function

Print (' importing M1 ')

def F1 ():

from M2 import y,f2 # first time importing M2

Print (' m1.f1---->y: ', y)

F2 ()

x=' M1 '

m2.py File:

# Error Demonstration

‘‘‘

print (' importing m2 ')

From M1 Import x

Y= ' m2 '

‘‘‘

# Solution One: Put the loop-imported statement behind the name definition

Print (' importing m2 ')

y=' m2 '

from M1 import x

# Solution Two: Put the loop import statement inside the function

Print (' importing m2 ')

def F2 ():

from M1 import x

Print (' m2.f2--->x: ', X)

y=' m2 '

Circular import problem and solution a map

Cyclic import problem and solution two-match diagram

Circular import issues for Python 1-2 modules

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.