差異處

這裏顯示兩個版本的差異處。

連向這個比對檢視

下次修改
前次修改
python:basic:import_class [2016/12/07 11:53]
tony 建立
python:basic:import_class [2023/06/25 09:48] (目前版本)
行 2: 行 2:
 ====== Import Class - '​module'​ object is not callable ====== ====== Import Class - '​module'​ object is not callable ======
 ===== Problem ===== ===== Problem =====
 +這算是初學者問題。範例專案結構如下:​
 +<code bash>
 +example/
 + __init_.py
 + Test.py
 +main.py
 +</​code>​
 +Test.py中有一個class叫Test,而main.py我這樣使用它:​
 +<code python>
 +from example import Test
  
 +test = Test()
 +</​code>​
 +在執行時,會出現TypeError:​ '​module'​ object is not callable。
 ===== How to? ===== ===== How to? =====
 +在這樣的專案結構下,common資料夾為package名稱,Test.py的Test為模組名稱,其內的class Test就是我要的類別名稱。因此正確的匯入應該是:​ 
 +<code python>​ 
 +from common.Test import Test 
 +</​code>​ 
 +假如Test.py中有其它類別叫Test2,則要使用以下方式匯入:​ 
 +<code python>​ 
 +from common.Test import Test2 
 +</​code>​
 ===== Reference ===== ===== Reference =====
   - [[http://​stackoverflow.com/​questions/​4534438/​typeerror-module-object-is-not-callable|typeerror-module-object-is-not-callable]]   - [[http://​stackoverflow.com/​questions/​4534438/​typeerror-module-object-is-not-callable|typeerror-module-object-is-not-callable]]