我如何解决此python代码中的错误

时间:2020-07-25 01:04:14

标签: python arduino anaconda serial-port spyder

获取我正在运行的代码的返回错误。由于这是复制的代码,因此我没有编写它,也不确定如何解决它。 OP WINDOWS 10 anaconda3 python 3.7.6

代码:

#Purpose: Save data from an arduino into file using python
#Name: Kira Kemock
#Date: 7/24/2020
#filename = myfile.txt
import serial                ****LINE 5*****
port_addr='COM4' # com port of the arduino
baud='9600'
file_name='mydata.txt' # this file will be saved in the same location as the py file

with serial.Serial(port_addr,baud) as port, open(file_name,"w+", 256) as outf:
    for i in range(200): #read 200 lines into the file
        fullline = port.readline()
        line = str(fullline)
        outf.write(line)
        outf.write('\n')
        outf.flush()

返回:

runfile('C:/PythonCode/SaveSerial.py', wdir='C:/PythonCode', post_mortem=True)
Traceback (most recent call last):

  File "C:\PythonCode\SaveSerial.py", line 5, in <module>
    import serial

ModuleNotFoundError: No module named 'serial'

第5行已标记 第5行 *,以帮助识别问题

1 个答案:

答案 0 :(得分:0)

您需要安装串口。你可以用

pip3 install serial

打开powershell并输入