7

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()
Joseph Hansen
  • 12,665
  • 8
  • 50
  • 68
AlbertSm
  • 105
  • 2
  • 2
  • 12
  • I havent programmed with Arduino or a raspberrypi but there is a general issue in installing python modules which I have sometimes run across. Sometimes some modules get installed for python2.x instead of 3.x You could verify which version they were installed for by opening up an interactive shell and trying to import the module there – Chaitanya Nettem Dec 13 '13 at 05:18
  • It is nearly impossible to give you meaningful advice since we don't know where your python 3 is located. You're facing this problem probably because `pip` installed the package for python 2 instead of 3. My advice is to simply use python 2, there are no major differences between the two. – Games Brainiac Dec 13 '13 at 05:40
  • yes you are right. pip installed the package for python2 I just checked that. is there a way to install it to python3? i mean this is my first python program and i am not really good at it. what is the differences between the two? this program took me awhile to write... – AlbertSm Dec 13 '13 at 05:57

2 Answers2

17

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.

Community
  • 1
  • 1
flyer
  • 9,280
  • 11
  • 46
  • 62
  • thank for the help. it worked. I uninstall the python2 anyway before but to install pip on python3 i used sudo apt-get install python3-pip after that sudo pip-3.2 install pyserial. the problem solved but i now face with another problem. it is saying port is already open. – AlbertSm Dec 13 '13 at 07:30
  • @AlbertSm, use the shell command `netstat -npl` to see which process uses the port and it's up to you to decide whether to kill it. – flyer Dec 13 '13 at 07:32
  • 1
    in the code i was trying to open the port but i didnt know that python3 already does it for you. that was the error. it is working now – AlbertSm Dec 13 '13 at 19:22
  • 1
    Just to add on, if pip3 command is not found, use this command `ls /usr/bin/pip*` to get its alternative/replacement. For mine, it's pip-3.2 – Joyce Sep 09 '14 at 19:11
1

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.

Harshan Gowda
  • 181
  • 2
  • 10