python没有名为serial的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20559457/
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
python no module named serial
提问by AlbertSm
I am having a problem with my python program. I wrote the program to get the data(temperature) from arduino to my raspberry pi sqlite database. but it gives me an error at line4(import serial) saying "ImportError: No module named serial". I use python3 and have already updated the pyserial. i am new in python so i am making some mistakes...
我的python程序有问题。我编写了程序以将数据(温度)从 arduino 获取到我的 raspberry pi sqlite 数据库。但它在第 4 行(导入序列)给我一个错误,说“导入错误:没有名为序列的模块”。我使用python3并且已经更新了pyserial。我是 python 新手,所以我犯了一些错误......
#!/ussr/bin/python
# -*- coding: utf-8 -*-
import serial
import datetime
import sqlite3 as lite
import sys
import time
ser = serial.Serial('/dev/ttyACM1', 9600, timeout=1)
ser.open()
count = 0
con = lite.connect('realtime_data.db')
try:
while 1:
indata = ser.readline()
current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
count = count + 1
print (count)
with con:
cur = con.cursor()
cur.execute("INSERT INTO Temperatures VALUES( ?, ?, ? )", (count, current_time, indata))
if count > 100:
cur.execute("DELETE FROM Temperatures")
count = 0
# time.sleep(3) #upload to database every 5 seconds
except KeyboardInterrupt:
ser.close()
采纳答案by flyer
Here's a question about How to install pip with Python 3?. After that, you could use pipto install pyserialcompatible with python-3.x, like the following:
这是一个关于如何使用 Python 3 安装 pip的问题?. 之后,您可以使用与python-3.x兼容的pip安装pyserial,如下所示:
$ sudo pip3 install pyserial
Here is a docabout how to install pyserialusing its source code compatible with python-3.x
这是关于如何使用与 python-3.x 兼容的源代码进行安装的文档pyserial
P.S.: If there are both python-2.x and python-3.x on your platform, like Arch Linux, when you want to install some packages, you should be careful to choose which python version the package should be compatible with and then use pip2or pip3to get and install those packages.
PS:如果你的平台上同时存在python-2.x和python-3.x,比如Arch Linux,当你想安装一些包时,你应该小心选择包兼容的python版本然后使用pip2或pip3来获取和安装这些软件包。
回答by Harshan Gowda
If The Filename you have saved is same as Module name then it will give you error. For example if your file name is "serial.py" and you have import serial, then it will first check in serial.py for the methods you have declared.
如果您保存的文件名与模块名称相同,那么它会给您错误。例如,如果您的文件名是“serial.py”并且您导入了serial,那么它会首先在serial.py 中检查您声明的方法。

