Problem with PySerial library (PYTHON)

0

I'm trying to control the arduino by a Python script through the "pyserial" and "serial" library. But every time I run the code it gives an error in 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've done a google search, but all the solutions applied return error. Can someone help me? The arduino is connected, I already loaded the code on it, but the problem is actually in the library.

I already deleted the folders from the libraries and installed them 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']
    
asked by anonymous 04.03.2018 / 19:48

1 answer

1

This type of error is typical of when you think you imported one module and imported another one: your project probably has another serial.py file. This file must be imported in place of serial that is available from pyserial.

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

(On a side note, you really should try to use Python 3 there if possible - Python 2 is pretty old already and will only have any type of support for another 2 years, besides not including any of the new language features and have less and less support from third-party libraries.)

    
05.03.2018 / 11:46