【问题标题】:How can I fix the error "cannot import name 'serial' from 'serial' (unknown location)"?如何解决错误“无法从'serial'(未知位置)导入名称'serial'”?
【发布时间】:2020-07-24 02:53:35
【问题描述】:

在我的代码中,我尝试使用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 上搜索此错误消息,但找不到解决我问题的答案。

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

  • pip install serial,
  • pip install pyserial,
  • 还可以卸载并重新安装。

我该如何解决这个错误?

【问题讨论】:

  • 我想知道您卸载并重新安装了什么。根据我的经验,Serial 和 Pyserial 都无法安装在一起。任何一个都必须去。
  • 它可能只是使用正确的大小写:尝试from serial import Serial,当你使用它时,使用:ser = Serial('/dev/ttyUSB0') # open serial port
  • 这能回答你的问题吗? Python serial without pyserial
  • @RufusVS 感谢您的命令但我无法理解您所说的。这个意思对吗?或者有什么需要解决的? ++啊哈!我现在试试这个
  • @JohnMelodyMelissa 感谢您的回答。但是我已经一起安装和卸载了它......我看到了链接的问题,但它与我的问题有点不同......我真的很感激。

标签: python pyserial serial-communication


【解决方案1】:

试试这个方法:

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

【讨论】:

  • 我的天啊鲁弗斯大师!!我从你那里得到了解决方案:)。我该如何表达这种喜悦我真的很感谢您的帮助。我可以再问一个吗? Serial 和 serial.Sreial 有什么区别?
  • 如果您使用from serial import Serial 导入,它会将 Serial 类放入您的名称空间,以便您可以按照我的说明使用它。如果您使用 import serial 导入模块,则要使用您需要的类 ser = serial.Serial(...
  • 啊哈!!我真的很感激先生。我必须一步一步地学习@@!!
猜你喜欢
  • 2023-02-21
  • 1970-01-01
  • 2022-06-17
  • 2022-06-30
  • 2020-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-07
相关资源
最近更新 更多