怎么在Python中导入库函数&IMPORT CAN"

本教程将介绍如何在Python中导入库函数&IMPORT CAN"的处理方法,这篇教程是从别的地方看到的,然后加了一些国外程序员的疑问与解答,希望能对你有所帮助,好了,下面开始学习吧。

怎么在Python中导入库函数&IMPORT CAN" 教程 第1张

问题描述

我正在使用Ubuntu。我想要向总线发送垃圾邮件,并使用Wireshark查看数据包。我试图运行如下所示的Python代码,但它抛出了以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'can'

我的代码:

import time, can
bustype = 'socketcan'
channel = 'vcan0'
def producer(id):
 # :param id: Spam the bus with messages including the data id.
 bus = can.interface.Bus(channel=channel, bustype=bustype)
 for i in range(10):
  msg = can.Message(arbitration_id=0xc0ffee, data=[id, i, 0, 1, 3, 1, 4, 1], extended_id=False)
  bus.send(msg)
 # Issue #3: Need to keep running to ensure the writing threads stay alive. 
 time.sleep(1)
producer(10)

推荐答案

Ubuntu18.04及更高版本

在Ubuntu 18.04及更高版本中,默认的Ubuntu存储库提供了python-can和python3-can。打开终端并输入:

sudo apt updatesudo apt install python3-can # for Python 3.x

sudo apt updatesudo apt install python python-can # for Python 2.x 

若要安装Canard(用于与控制器区域网络(CAN)交互的库),您必须使用pip。

python3 -m pip install --user CANard # for Python 3.x

python -m pip install --user CANard # for Python 2.x

好了关于怎么在Python中导入库函数&IMPORT CAN&QUOT;的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。