1

I'm trying out an example on Arduino: http://playground.arduino.cc/Interfacing/Python

The example (running on Ubuntu) works great in the shell:

import serial

ser = serial.Serial('/dev/ttyACM0', 9600)

while True:
    print(ser.readline())

However attempting to execute as a script:

Desktop/python_arduino/./serial.py...

Which executes this:

#!/usr/bin/env python
import serial

ser = serial.Serial('/dev/ttyACM0', 9600)

while True:
    print(ser.readline())

And I get this:

Traceback (most recent call last):
  File "Desktop/python_arduino/./serial.py", line 2, in <module>
    import serial
  File "/home/leo/Desktop/python_arduino/serial.py", line 4, in <module>
    ser = serial.Serial('/dev/ttyACM0', 9600)
AttributeError: 'module' object has no attribute 'Serial'

What is causing this inconsistency? It should be easy to import serial regardless of shell or script right?

2
  • What was causing the behaviour you observed? Commented Oct 13, 2013 at 10:07
  • See my answer below. The script filename was the same name as the import. So the filename was serial.py. The module is called serial. I changed the script's filename, and it worked. Commented Oct 14, 2013 at 20:18

2 Answers 2

2

I FOUND IT!

The issue was actually subtle yet simple.

The script filename was the same name as the import.

So the filename was serial.py. The module is called serial, so it created a conflict.

I changed the script's filename, and it worked.

Sign up to request clarification or add additional context in comments.

1 Comment

I had to delete the .pyc file as well.
1

The difference between the shell and your script may be different path setups. Compare the paths and see if anything differs for the script vs. the shell

import ser
print ser.__file__

import sys
print sys.executable

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.