ImportError:无法从“Serial”(未知位置)导入名称“Serial”

2024-04-16 11:10:43 发布

您现在位置:Python中文网/ 问答频道 /正文

每当我执行下面的代码时,都会出现以下错误:

ImportError: cannot import name 'Serial' from 'serial' (unknown location)

代码:

  from serial import Serial

  arduinodata = Serial('com4',9600)

  print("Enter n to ON LED and f to OFF LED")

  while 1:
    input_data = raw_input()
    print ("You Entered"+input_data)
    
    if (input_data == 'n'): 
        arduinodata.write(b'1')
        print("LED ON")

    if (input_data == 'f'):
        arduinodata.write(b'0')
        print("LED OFF")

我已经安装了所有必需的python模块。像pyserial,pyfirmata等,但它不断给我这个错误


Tags: to代码fromimportinputdataledif
3条回答

我遇到了同样的问题。我首先卸载了所有pippip3conda中的pyserial(我在所有3个上都安装了它),然后重新安装了它。这对我来说很好。希望有帮助

我也遇到了这个问题。这是由于“pyserial”和“serial”库是在pip的基础上按用户安装的,而pyserial也是在系统范围内安装的(可能是在不同版本中,使用Linux软件包管理器)

解决方案

删除每个用户的版本修复了我的问题。

很可能错过了一个

 __init__.py 

文件、模块或模块的文件子目录(串行)与文件可执行文件位于不同的层上。希望有帮助:)

相关问题 更多 >