Try   HackMD

What happens when you import a module? - Reuven M. Lerner

歡迎來到 PyCon TW 2024 共筆

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

共筆入口:https://hackmd.io/@pycontw/2024
手機版請點選上方 按鈕展開議程列表。
Welcome to PyCon TW 2024 Collaborative Writing
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Collaborative Writing Workplace:https://hackmd.io/@pycontw/2024
Using mobile please tap to unfold the agenda.

Collaborative writing start from below
從這裡開始共筆

會議錄音、逐字稿、摘要與翻譯:https://s.dwave.cc/mkmvpjQ

Simple case of ImportError when cyclic import happend

  • main.py
import a
import b
if __name__ == '__main__':
    print("Hello world")
  • a.py
from b import y
x = y
  • b.py
from a import x
y = 1

Command

$ python -V
Python 3.12.4
$ python main.py
raceback (most recent call last):
  File "<File Path>/main.py", line 1, in <module>
    import a
  File "<File Path>/a.py", line 1, in <module>
    from b import y
  File "<File Path>/b.py", line 1, in <module>
    from a import x
ImportError: cannot import name 'x' from partially initialized module 'a' (most likely due to a circular import) (<File Path>/a.py)

Note that the raising error message had the keyword 'initialized' that means the error happen on loading module, which not on assign the module name.


Below is the part that speaker updated the talk/tutorial after speech
講者於演講後有更新或勘誤投影片的部份