--- title: "What happens when you import a module? - Reuven M. Lerner" tags: PyConTW2024, 2024-organize, 2024-共筆 --- # What happens when you import a module? - Reuven M. Lerner {%hackmd NY3XkI1xQ1C9TrHQhoy9Vw %} <iframe src=https://app.sli.do/event/aVxTTnZRL4fYJcHGtVrBPC height=450 width=100%></iframe> > Collaborative writing start from below > 從這裡開始共筆 會議錄音、逐字稿、摘要與翻譯:https://s.dwave.cc/mkmvpjQ ## Simple case of ImportError when cyclic import happend - `main.py` ```python import a import b if __name__ == '__main__': print("Hello world") ``` - `a.py` ```python from b import y x = y ``` - `b.py` ```python from a import x y = 1 ``` Command ```shell $ 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 講者於演講後有更新或勘誤投影片的部份