Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
562 views
in Technique[技术] by (71.8m points)

module - Serial import python

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It should be:

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

Note the capital 'S' in serial.Serial


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...