Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an Arduino hooked up with 2 DS18B20 temp sensors. I'm very (VERY) new to python. I am looking for a way to read the serial input and parse it into a sqlite database, but that is getting ahead of myself. Why do I get an error while trying to define my serial port to a variable?

First things first sys.version

2.7.1 (r271:86832, Jul 31 2011, 19:30:53) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)]

My current, just read input from the serial connection program.

from serial import serial
import time


# open serial port
ser = serial.Serial('/dev/tty.usbmodem621',9600,timeout=2) 
ser.open()


while True:
    print('dev 0' + ser.read())     
    pass

ser.close()

I can not currently get it to compile. Most of the results I've found for this error tell to add from serial import serial, but in this case it hasn't worked.

The error.

$ python ser.py
Traceback (most recent call last):
  File "ser.py", line 1, in <module>
    from serial import serial
  File "/Users/frankwiebenga/serial.py", line 8, in <module>
AttributeError: 'module' object has no attribute 'Serial'

Also if I just use import serial I get the same error

$ python ser.py
Traceback (most recent call last):
  File "ser.py", line 1, in <module>
    import serial
  File "/Users/frankwiebenga/serial.py", line 8, in <module>
AttributeError: 'module' object has no attribute 'Serial'

Also, per comment. Created new file named something.py and still get the same error regardless of using import serial or from serial import serial.

$ python something.py 
Traceback (most recent call last):
  File "something.py", line 1, in <module>
    from serial import serial
ImportError: No module named serial

When running my bash script I get an output that is valid, so I know it isn't the Arduino code.

Output:

Requesting temperatures...DONE
Device 0: 25.62
Device 1: 25.75
Requesting temperatures...DONE
Device 0: 25.62
Device 1: 25.81

Bash:

while true  # loop forever
do
   inputline="" # clear input

   # Loop until we get a valid reading from AVR
   until inputline=$(echo $inputline | grep -e "^temp: ")
   do
      inputline=$(head -n 1 < /dev/tty.usbmodem621)
   done
   echo "$inputline"
done 
share|improve this question
Your script is named serial.py, which is confusing things when you do import serial. Rename your script to something else. – TJD Aug 24 '12 at 23:12
@TJD Still doesn't fix it. – atrueresistance Aug 24 '12 at 23:18
@atrueresistance, it sounds like you just created a new file named something.py rather than renaming the serial.py to something.py. You will have to rename or remove the script with the name serial.py. – Justin Peel Aug 24 '12 at 23:33
@atrueresistance, you have to both do the renaming, and use the proper import command import serial. Your updated error has from serial import serial, which uses the wrong case for Serial. As dkamins mentions, use either import serial or from serial import Serial – TJD Aug 25 '12 at 0:07

2 Answers

You need to use import serial. serial is the name of the module and it does not contain an attribute with name serial.

http://pyserial.sourceforge.net/shortintro.html#opening-serial-ports

share|improve this answer

You can EITHER do:

from serial import Serial

s = Serial(...)

OR:

import serial

s = serial.Serial(...)

Choose one.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.