簡體   English   中英

"AttributeError: 'module' object has no attribute" - 有人可以解釋這個錯誤消息的含義嗎?

[英]"AttributeError: 'module' object has no attribute" - Can someone explain the meaning of this error message?

我正在嘗試讓 ODB 庫正常工作。 https://python-obd.readthedocs.io/en/latest/的文檔中,它列出了以下代碼:

import obd

connection = obd.OBD("/dev/ttyUSB0") # connects to USB or RF port

cmd = obd.commands.SPEED # select an OBD command (sensor)

response = connection.query(cmd) # send the command, and parse the response

print(response.value) # returns unit-bearing values thanks to Pint
print(response.value.to("mph")) # user-friendly unit conversions

當我把它放在一個名為 test.py 的文件中並運行它時:

python2 test.py

我收到以下錯誤消息:

Traceback (most recent call last):
  File "test.py", line 1, in <module>
    import obd
  File "/home/ubuntu/obd.py", line 3, in <module>
AttributeError: 'module' object has no attribute 'OBD'

Stackoverflow 對此錯誤消息進行了多次迭代,但都沒有清楚地解釋問題,只是針對這些庫給出了具體的解決方案。

我想很明顯我是 Python 的新手,而且我在解釋這個錯誤消息時遇到了麻煩,即使在編寫了幾個小的 Python 程序幾個小時之后也是如此。 我當然也對為什么錯誤消息對新手來說如此不直觀以及我可以在哪里獲得我可能遺漏的常識感興趣,我想這是一個很好的發現它的案例。

到目前為止,我已經弄清楚以下內容:

<module>指的是我試圖運行的 python 腳本的名稱。

我最初犯的錯誤是將我的test.py文件命名為obd.py 這與import obd沖突,因為它試圖導入文件本身 - 即使在我刪除它之后,原因是當我第一次嘗試運行它時,它創建了一個名為obd.pyc的文件 - 即使 obd obd.py不再存在, import OBD找到最初創建的obd.pyc並嘗試導入它 - 當然不包含我試圖使用的庫中的 OBD object。

這不是我正在尋找的詳細答案,所以請添加更詳細的解釋 - 或者如果可以的話,添加 Python 編譯工作原理的鏈接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM