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...
#!/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()
The Python "ModuleNotFoundError: No module named 'serial'" occurs when we forget to install the pyserial module before importing it or install it in an incorrect environment. To solve the error, install the module by running the pip install pyserial command.
Download Page: https://pypi.python.org/pypi/pyserial. Latest: Documentation: http://pyserial.readthedocs.io/en/latest/ Project Homepage: https://github.com/pyserial/pyserial.
This module encapsulates the access for the serial port. It provides backends for Python running on Windows, OSX, Linux, BSD (possibly any POSIX compliant system) and IronPython. The module named “serial” automatically selects the appropriate backend.
Here's a question about How to install pip with Python 3?. After that, you could use pip
to install pyserial
compatible with python-3.x, like the following:
$ sudo pip3 install pyserial
Here is a doc about how to install pyserial
using its source code compatible with python-3.x
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 pip2
or pip3
to get and install those packages.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With