如何解决错误“无法从'serial'(未知位置)导入名称'serial'”?

时间:2020-07-24 02:53:35

标签: python pyserial serial-communication

在我的代码中,我尝试使用serial模块与某些设备进行交互:

from serial import serial

ser = serial.Serial('/dev/ttyUSB0')  # open serial port

print(ser.name)         # check which port was really used

ser.write(b'hello')     # write a string

ser.close()

但是我得到这个错误:

cannot import name 'serial' from 'serial' (unknown location)

我在堆栈溢出中搜索了此错误消息,但是找不到解决我问题的答案。

根据发现的内容,我尝试了:

  • pip install serial
  • pip install pyserial
  • 并取消安装并重新安装。

如何解决此错误?

1 个答案:

答案 0 :(得分:0)

尝试一下:

from serial import Serial  # note the capital S change

ser = Serial('/dev/ttyUSB0')  # open serial port

print(ser.name)         # check which port was really used

ser.write(b'hello')     # write a string

ser.close()