我正在尝试一个关于 Arduino 的示例:http: //playground.arduino.cc/Interfacing/Python
该示例(在 Ubuntu 上运行)在 shell 中运行良好:
import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
while True:
print(ser.readline())
但是尝试作为脚本执行:
桌面/python_arduino/./serial.py ...
执行此操作:
#!/usr/bin/env python
import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
while True:
print(ser.readline())
我明白了:
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'
是什么导致了这种不一致?import serial
无论shell还是脚本都应该很容易吧?