10

I"m trying to use pyserial. When I do the following script.

import serial
ser= serial.serial("COM5", 9600)
ser.write("Hello worldn")
x = ser.readline()
print(x)     

Error code:

c:\Python27>python com.py
Traceback (most recent call last):
  File "com.py", line 2, in <module>
    ser= serial.serial("COM5", 9600)
AttributeError: 'module' object has no attribute 'serial'

I read a suggestion and changed it to:

from serial import serial
ser= serial.serial("COM5", 9600)
ser.write("Hello worldn
x = ser.readline()
print(x)     

I now get the error

c:\Python27>python com.py
Traceback (most recent call last):
  File "com.py", line 1, in <module>
    from serial import serial
ImportError: cannot import name serial

I read that this can be from having ini in your module, but dont' know anyting about this.

I printed my sys.path and pyserial is in there.

['C:\\Users\\Jeff\\Desktop', 'C:\\Python27\\lib\\site-packages\\distribute-0.6.4
9-py2.7.egg', 'C:\\Python27\\lib\\site-packages\\pyserial-2.7-py2.7.egg', 'C:\\W
indows\\SYSTEM32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\
\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Pyt
hon27\\lib\\site-packages', 'C:\\Python27\\lib\\site-packages\\setuptools-0.6c11
-py2.7.egg-info']

Getting kind of annoyed :(... Thanks for help.

dsolimano
  • 8,870
  • 3
  • 48
  • 63
jeffpkamp
  • 2,732
  • 2
  • 27
  • 51
  • what is your exact stack trace ? – karthikr Nov 01 '13 at 14:19
  • The error you are getting means your interpreter cannot locate the serial module. Have you installed it? Are you sure you have proper permissions to use it? – Michael Foukarakis Nov 01 '13 at 14:23
  • I installed the module using distribute easy-install. the folder is under python27/lib-site-packages/pyserial-2.7-py2.7.egg/serial. also added the stack traces. – jeffpkamp Nov 01 '13 at 14:44
  • 1
    Did you happen to call one of *your* programs `serial.py`? At the start of your code, add `import serial` and `print(serial.__file__)` as the first two lines. – DSM Nov 01 '13 at 14:52
  • 1
    I did name it serial.py initially, but changed it. Here is the output of the print. c:\Python27\lib\site-packages\pyserial-2.7-py2.7.egg\serial\__init__.pyc – jeffpkamp Nov 01 '13 at 15:02
  • I'll second that. DONT NAME YOUR FILE SERIAL.PY. sorry for all caps but I've been burned by this twice in two years, and I missed this comment in this file both times. Thanks :) – benathon Feb 06 '15 at 12:08
  • I hava had similar problems with my python 3.6.5 and I needed to uninstall "serial" with pip and then install "pyserial" with pip. – Filozof71 Apr 21 '18 at 21:34

1 Answers1

15

It should be:

import serial
ser = serial.Serial("COM5", 9600)

Note the capital 'S' in serial.Serial

jwygralak67
  • 924
  • 7
  • 13
  • 2
    That appears to be it! At least the error went away, I'll have to wait until I"m home to be sure it's actually talking to the port. Dang capitalization and indentation get me every time :). Thanks – jeffpkamp Nov 01 '13 at 16:22
  • 3
    import serial ; help(serial) – greggo May 10 '14 at 01:27