Pyserial library problem (PYTHON)

Asked

Viewed 429 times

0

I am trying to control Arduino by a Python script through the "pyserial" and "serial" library. But every time I run the code it misses the first few lines:

import serial

ser = serial.Serial('COM7', 9600)

error returned:

Traceback (most recent call last):
File "[...]teste.py", line 3, in <module>
ser = serial.Serial('COM7', 9600)
AttributeError: 'module' object has no attribute 'Serial'

I have already given a google search, but all applied solutions return error. Can anyone help me? Arduin is connected, I already loaded the code into it, but the problem is actually in the library.

Already deletes library folders and installed again.

I use Pycharm and Sublime

Result of print(dir(serial)):

['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'abs', 'absolute_import', 'all', 'any', 'apply', 'ascii', 'basestring', 'bin', 'bool', 'buffer', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'cmp', 'coerce', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'division', 'divmod', 'enumerate', 'errors', 'eval', 'execfile', 'exit', 'file', 'filter', 'float', 'format', 'frozenset', 'generators', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'hooks', 'id', 'input', 'int', 'intern', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'long', 'map', 'max', 'memoryview', 'meta', 'min', 'model', 'native_str', 'nested_scopes', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'print_function', 'properties', 'property', 'quit', 'range', 'raw_input', 'reduce', 'reload', 'repr', 'request', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'standard_library', 'staticmethod', 'str', 'sum', 'super', 'sys', 'test', 'tuple', 'type', 'unichr', 'unicode', 'unicode_literals', 'utilities', 'vars', 'with_statement', 'xrange', 'zip']
  • What version of Python? How did you install the module pyserial? Shortly after the import what the result of print(dir(serial))?

1 answer

1

This type of error is typical of when you think you imported one module and imported another: probably your project has another file serial.py. This file must be being imported instead of the serial which is made available by pyserial.

The solution is to rename your file called serial.py to something else, and the import serial will go on to take the original library.

(In a separate note, you really should try using Python 3 there, if possible - Python 2 is quite old already and will only have any kind of support for another 2 years, not to include any of the new language features and have less and less support from third party libraries.)

  • This is one of the solutions found on google. I had already renamed my file to "test.py" and includes only the above code, but still gives the error. I already installed Python 3 also to test, but it didn’t work.

  • by the listing you give is as if someone had played the __builtins__ over the serial. Browse any serial.py file on your computer and remove it, then re-install the pyserial. In addition to dir in pyserial, what appears if you print serial.__file__ ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.