3

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()
Ryan Cain
  • 43
  • 1
  • 7
  • 1
    Do you have serial installed? –  Dec 18 '18 at 13:56
  • Do you have any other variable named `serial`? Perhaps the instance of your class? – kalehmann Dec 18 '18 at 14:57
  • 1
    If nothing works, open the python command line interpreter, enter `import serial`, then `print(serial)` and `serial.` and start the autocompletion with tab to see what `serial` is in your context. – kalehmann Dec 18 '18 at 14:59
  • It would be helpful if you provided a [mcve] instead of separate, incomplete snippets. Anyhow, start Python interactively, `import serial` and then do `dir(serial)`. That should show you what the serial module contains. BTW: If you have a file `serial.py` in your current directory, you will import that instead of the system-wide one. – Ulrich Eckhardt Dec 18 '18 at 14:59
  • By the way, the `NameError` you provided indicates, that the import worked, but you still use `serial.Serial(...)` instead of `Serial(...)` later – kalehmann Dec 18 '18 at 15:00
  • There is no serial variable and when i do print(serial) it displays "" – Ryan Cain Dec 18 '18 at 15:05
  • @UlrichEckhardt when doing the dir(serial) it did display a long list of functions so it is importing correctly if i am not mistaken – Ryan Cain Dec 18 '18 at 15:11
  • Okay, what about the other comment? When you import the `Serial` class with `from serial import Serial`, do you change every occurrence of `serial.Serial` into `Serial`? – kalehmann Dec 18 '18 at 15:12
  • @kalehmann i have just tried that when i do it give the error 'ImportError: cannot import name 'Serial' from 'serial' (C:\Users\ryanc\Documents\Python\TestEnv\venv\lib\site-packages\serial\__init__.py)' – Ryan Cain Dec 18 '18 at 15:19
  • My last suggestion is that you still somehow have the wrong `serial` module. What is the output of `print(serial.__dict__)`? – kalehmann Dec 18 '18 at 15:32
  • This is the output of that command --- {'__name__': 'serial', '__doc__': None, '__package__': 'serial', '__loader__': <_frozen_importlib_external._NamespaceLoader object at 0x03740930>, '__spec__': ModuleSpec(name='serial', loader=<_frozen_importlib_external._NamespaceLoader object at 0x03740930>, submodule_search_locations=_NamespacePath(['C:\\Users\\ryanc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\serial'])), '__file__': None, '__path__': _NamespacePath(['C:\\Users\\ryanc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\serial'])} – Ryan Cain Dec 18 '18 at 15:41
  • I am sorry, that was not what I expected. Does `pip3 list | grep serial` gives you only `pyserial`? – kalehmann Dec 18 '18 at 15:54
  • I am not sure what 'grep' is however I did not run when done it grep serial it gave a list of the modules curses, future, iso8601, pip, pyserial, PyYAML, setuptools – Ryan Cain Dec 18 '18 at 16:04
  • This looks fine. Are you using something like `pyenv`? Try `where python3` and `where pip3`. Maybe you are not using the package provided by pip. – kalehmann Dec 18 '18 at 16:24
  • I am not using pyenv and where should I use the where commands – Ryan Cain Dec 18 '18 at 16:28
  • The where commands should be entered in cmd.exe. The output you have provided shows different paths for the serial module.(C:\\Users\\ryanc\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\serial and C:\\Users\\ryanc\\Documents\\Python\\TestEnv\\venv\\lib\\site-packages\\serial\) – kalehmann Dec 18 '18 at 16:45
  • the second one was using the console via PyCharm – Ryan Cain Dec 18 '18 at 17:09
  • Have you verified, that your code does not work with both paths? – kalehmann Dec 18 '18 at 17:13
  • Take a look at https://www.jetbrains.com/help/pycharm/installing-uninstalling-and-upgrading-packages.html and check that PyCharm uses the correct `serial` package – kalehmann Dec 18 '18 at 17:18
  • PyCharm uses the same packages that I previously stated curses, future, iso8601, pip, pyserial, PyYAML, setuptools and the code does give the same error in both locations – Ryan Cain Dec 18 '18 at 17:45
  • I am sorry, but I have no ideas left. Please let us know, when you figured it out. – kalehmann Dec 18 '18 at 18:27
  • Thank you so much for being this patient and i will update you when i figure it out – Ryan Cain Dec 18 '18 at 19:11

2 Answers2

5

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.

kalehmann
  • 4,821
  • 6
  • 26
  • 36
1

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.

Sphynx
  • 33
  • 5