How to import python module inside webiopi?

428 views
Skip to first unread message

rfid gao

unread,
May 11, 2016, 11:55:19 PM5/11/16
to WebIOPi
Dear sir,
   I have success lunch webiopi .
  But when I try to use my customer python code, it cannot import the module.
My code:
#!/usr/bin/env python
import webiopi
import RPi.GPIO as GPIO
import video_dir   <== fail here:
import car_dir
import motor
from socket import *
from time import ctime          # Import necessary modules
# -------------------------------------------------- #
#cro definition part                              #
# -------------------------------------------------- #
@webiopi.macro
def go_forward():
    motor.forward()

I had put my customer python code at:
/home/pi/WebIOPi-0.7.1/examples/server/

The var/log/webiopi complain:
2016-05-12 03:23:23 - WebIOPi - INFO - Loading myscript from /home/pi/WebIOPi-0.7.1/examples/server/web_server.py
2016-05-12 03:23:23 - WebIOPi - ERROR - No module named 'video_dir'
But if I run it in the shell, it works:
.
pi@raspberrypi:~/WebIOPi-0.7.1/examples/server $ sudo python web_server.py
pi@raspberrypi:~/WebIOPi-0.7.1/examples/server $ python web_server.py
pi@raspberrypi:~/WebIOPi-0.7.1/examples/server $

So it mean the python environment can import this module, but webiopi cannot.

I am new to python and webiopi. I dig a little. find:
.pi@raspberrypi:~/WebIOPi-0.7.1/examples/server $ /usr/bin/env python
Python 2.7.9 (default, Mar  8 2015, 00:52:26)
but webiopi is using:
2016-05-12 03:39:54 - WebIOPi - INFO - Starting WebIOPi/0.7.1/Python3.4

Could somebody help me out or point the reference document for me?

Your help are very appreciate!
Thanks
John
  

rfid gao

unread,
May 12, 2016, 12:12:48 AM5/12/16
to WebIOPi
Dear sir.
  I found one solution is to include the python source code in myproject.
  This is work, but if I have a lot of module need import, it need add one by one. This cannot support for a big project.
  Any other solution?
  Thanks
John

hal8000b

unread,
May 12, 2016, 12:54:01 PM5/12/16
to WebIOPi
I asked a similar question last week:
https://groups.google.com/forum/#!topic/webiopi/4ln8Vy5tKgI

Has various solutions but you can also place your custom python modules
in /home/pi/myproject/python

Make sure the module ends in .py  e.g video_dir.py

and you import it without the py extension
e.g
In script.py call it with
import video_dir

and it should find your module.
Reply all
Reply to author
Forward
0 new messages