当我尝试使用导入的 can 包运行代码时
import can
from can.interfaces.vector import canlib
from datetime import datetime
我收到错误:
ModuleNotFoundError: No module named 'can.interfaces'; 'can' is not a package.
我的解释器路径是全局的并且包安装在C:\Python\Python310\Lib\site-packages中。什么可能导致问题?我使用的是 Windows 10。
编辑
我将文件名从 can.py 更改为其他名称并且它可以工作(感谢 FlyingTeller),但现在在尝试导入 can 模块时遇到另一个错误。
Traceback (most recent call last):
File "c:\__work__\DownloadFiles_Karol\nweasdawd.py", line 2, in <module>
from can.interfaces.vector import canlib
File "C:\Python\Python310\lib\site-packages\can\interfaces\vector\__init__.py", line 4, in <module>
from .canlib import VectorBus, VectorChannelConfig
File "C:\Python\Python310\lib\site-packages\can\interfaces\vector\canlib.py", line 39, in <module>
from can import BusABC, Message, CanInterfaceNotImplementedError, CanInitializationError
ImportError: cannot import name 'BusABC' from 'can' (C:\Python\Python310\lib\site-packages\can\__init__.py)
您已将文件命名为
can
,因此它会隐藏您想要的导入。将其重命名为其他名称
我也有同样的问题。我卸载了 can 和 python-can。
然后我只重新安装了 python-can。
结果他们尝试安装到同一个文件夹中。