Ryan Cain
Ryan Cain

Reputation: 43

'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.

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

Upvotes: 3

Views: 5531

Answers (2)

Sphynx
Sphynx

Reputation: 43

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. To remedy, I had to perform the following:

pip uninstall serial
pip uninstall pyserial
pip install pyserial

After that it worked like a charm.

Upvotes: 2

kalehmann
kalehmann

Reputation: 5011

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?

Try to uninstall the serial package and install the pyserial package instead:

pip uninstall serial
pip install pyserial

Also make sure your file is not called serial.py. In that case import serial would just import your own file.

Upvotes: 6

Related Questions