如何修复错误 "cannot import name 'serial' from 'serial' (unknown location)"?

How can I fix the error "cannot import name 'serial' from 'serial' (unknown location)"?

在我的代码中,我尝试使用 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)

我在 Stack Overflow 上搜索了这条错误消息,但找不到解决我问题的答案。

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

我该如何解决这个错误?

这样试试:

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()