1

I have a strange raspberry pi issue. When I run a python code manually it works perfect but when I run it during startup it gives this error no module named socket server. This is the code given below and I have python 3

# Web streaming example
# Source code from the official PiCamera package
# http://picamera.readthedocs.io/en/latest/recipes2.html#web-streaming
import io
import picamera
import logging
import socketserver
from threading import Condition
from http import server

I had to put the file in boot order sequence so I did this

sudo nano /home/pi/.bashrc

And then I added this code at the bottom to run on startup

sudo python /home/pi/mypyscript.py

Where is the mistake? Does socketserver loads at the end. I am using Raspberry Pi zero to control a raspberry Pi camera.

asked Jun 17, 2018 at 6:58
1

2 Answers 2

4

python is never Python 3 in Debian, even if you have installed Python 3. Python 3 will install as python3, and the python binary points to Python 2 (see PEP 394) instead.

In Python 2, the socketserver module used to be called SocketServer (note the caps). Changing all references from socketserver to SocketServer would address that.

You can solve this by either:

  • changing the code to support Python 2, or
  • running python3 /home/pi/mypyscript.py instead of python /home/pi/mypyscript.py

It may also be helpful to note Milliways' advice, though that certainly isn't the cause of the error, just a potential pitfall when you do get this working.

answered Jun 17, 2018 at 9:06
2
  • Thanks a lot. It started working. I have a small doubt though how can i stop running the code? Do I need to open the file and stop it manually? Commented Jun 17, 2018 at 9:19
  • You would probably need to kill it manually, @Amit, by finding the correct process and running the kill command from the terminal. If you've run it interactively (i.e. it blocks your console) you can run Ctrl+C; otherwise, if you've run it at startup, you'll have to kill it. Commented Jun 17, 2018 at 10:37
2

Basically .bashrc DOES NOT run until you open a shell, and it runs EVERY TIME you open a shell. It DOES NOT run on startup.

bashrc is NOT intended to run scripts

answered Jun 17, 2018 at 7:29
1
  • You were correct as crontab did the job. I did crontab -e and then adding @reboot /usr/bin/python3 /home/pi/myscript.py& at the bottom it started working on reboot. Commented Jun 19, 2018 at 10:27

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.