cannot import name 'mydb' from partially initialized module 'connection' in Python

前端 未结 7 1559
南笙
南笙 2021-02-19 03:15

Python 3.8 error

ImportError: cannot import name \'mydb\' from partially initialized module \'connection\' 
(most likely due to a circular import) (C:\\U
sers\         


        
7条回答
  •  悲哀的现实
    2021-02-19 03:30

    The order of the imports matters:

    Example:

    # A.py
    # empty file
    
    # B.py
    import A
    
    # file1.py
    import A # A gets imported before B can import A
    import B # B tries to re-import A but A is already imported
    

    change the order to:

    # file1.py
    import B
    import A
    

提交回复
热议问题