串行导入python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19728535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Serial import python
提问by jeffpkamp
I"m trying to use pyserial. When I do the following script.
我正在尝试使用 pyserial。当我执行以下脚本时。
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 iniin your module, but dont' know anyting about this.
我读到这可能是因为您的模块中有ini,但对此一无所知。
I printed my sys.path and pyserial is in there.
我打印了我的 sys.path 并且 pyserial 在那里。
['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.
有点恼火:(...感谢您的帮助。
采纳答案by jwygralak67
It should be:
它应该是:
import serial
ser = serial.Serial("COM5", 9600)
Note the capital 'S' in serial.Serial
注意serial.Serial中的大写“S”