简体   繁体   English

'serial'没有属性串行错误,没有文件名或导入方法

[英]'serial' has no attribute Serial Error, not filename or import method

I have looked for similar solutions here however the only ones I can find do not fix the problem, and the solutions like this one python module 'serial' has no attribute 'Serial' [duplicate] does not get solved. 我在这里寻找了类似的解决方案,但是,我能找到的唯一解决方案无法解决问题,而像这样的python模块“ serial”没有属性“ Serial”的解决方案也无法解决。

This code 这段代码

self.ser = serial.Serial(port=self.dev_path, baudrate=600, bytesize=8, parity='N', stopbits=1, timeout=None)

is giving the error 正在给出错误

AttributeError: module 'serial' has no attribute 'Serial'

I am importing serial as 我正在导入序列为

import serial

However other solutions suggest using 但是其他解决方案建议使用

from serial import Serial

it gives the error 它给出了错误

NameError: name 'serial' is not defined

Edit Full Code: 编辑完整代码:

def __init__(self, debugging=False):
    # self.ser = serial.Serial(port='/dev/ttyUSB0',baudrate=600, bytesize=8, parity='N', stopbits=1, timeout=None)
    self.ser = serial.Serial(port=self.dev_path, baudrate=600, bytesize=8, parity='N', stopbits=1, timeout=None)
    print(str(self.ser.name))
    self.status()
    self.debug = debugging
    if (self.debug):
        print(self.ser.name)
        print("Pulse: " + str(self.pulse) + "\n")


def __del__(self):
    self.ser.close()

Maybe you have installed the serial package serializing/deserializing JSON/YAML/XML into python class instances and vice versa and not the pySerial package for accessing the serial port? 也许您已将JSON / YAML / XML序列化/反序列化 的串行软件包安装到python类实例中,反之亦然,而不是用于访问串行端口的pySerial软件包

Try to uninstall the serial package and install the pyserial package instead: 尝试卸载serial软件包并安装pyserial软件包:

pip uninstall serial
pip install pyserial

Also make sure your file is not called serial.py . 还要确保您的文件未称为serial.py In that case import serial would just import your own file. 在这种情况下, import serial只会导入您自己的文件。

I just ran into this issue, and for me it had to do with importing serial over pyserial before trying anything in the first place. 我只是遇到了这个问题,对我而言,这与在首先尝试任何操作之前通过pyserial导入serial有关。 To remedy, I had to perform the following: 为了补救,我必须执行以下操作:

pip uninstall serial
pip uninstall pyserial
pip install pyserial

After that it worked like a charm. 之后,它就像一种魅力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM