Python 中的 CAN 库错误:[WinError 2] 系统找不到文件

问题描述 投票:0回答:1

我想在我的 PyCharm 虚拟环境中使用 CANlib。但自从我安装了它,当我运行我的代码时,它不断地给出这个错误:

我尝试使用的代码:

from canlib import canlib

num_channels = canlib.getNumberOfChannels()
print(f"Found {num_channels} channels")
for ch in range(num_channels):
    chd = canlib.ChannelData(ch)
    print(f"{ch}. {chd.channel_name} ({chd.card_upc_no} / {chd.card_serial_no})")

代码来自原网站:https://pypi.org/project/canlib/

我搜索了这个错误,发现了一些我认为可能是原因的原因,但我不知道解决方案:

  1. 可能是32位64位dll的问题,但是源代码在其库dllLoader.py中两种情况都有:

     dir = os.getcwd()
     installDir = os.environ.get("KVDLLPATH", "")
     if not installDir:
         if sys.platform.startswith("win"):
             if platform.architecture()[0] == "32bit":
             aKeyName = r"SOFTWARE\KVASER AB\CanlibSDK"
         else:
             aKeyName = r"SOFTWARE\Wow6432Node\KVASER AB\CanlibSDK"
         aReg = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
         aKey = winreg.OpenKey(aReg, aKeyName)
         aValue = winreg.QueryValueEx(aKey, "")
         baseDir = str(aValue[0])
    
         if 8 * struct.calcsize("P") == 32:
             installDir = os.path.join(baseDir, "Bin")
         else:
             installDir = os.path.join(baseDir, "bin_x64")
         installDir = os.path.realpath(installDir)
    
  2. 我还检查了我的解释器路径是否涉及所需的 dll 文件。 (canlib32.dll等)

  3. 我卸载了该库并重新安装了它。

这些都不起作用。 有没有人可以帮助我?

python dll can-bus
1个回答
0
投票

我也遇到了同样的问题,并且为此困惑了一段时间。更改 KVDLLPATH 对我有帮助

import os

# Set the KVDLLPATH environment variable to the directory containing canlib32.dll
os.environ['KVDLLPATH'] = r'C:\Program Files\Kvaser\Drivers'

# Now import canlib
from canlib import canlib
© www.soinside.com 2019 - 2024. All rights reserved.