Page 1 of 1

trouble running python script with sudo [obsolete]

Posted: Sat Oct 12, 2013 5:09 am
by justStarting
so I have a python script running fine on my RPi when I trigger it with "python takeSpeech.py", however as soon as I add a sudo to it like:
sudo python takeSpeech.py
I get the following error:
Traceback (most recent call last):
File "takeSpeech.py", line 11, in <module>
import wolframalpha
ImportError: No module named wolframalpha

Here's the code up until that point:

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from collections import Counter
import time
import serial
from subprocess import Popen, PIPE, STDOUT
import os
import signal
import urllib
import urllib2, json
import wolframalpha
here's what I know about the woflramalpha location/ls info:
pi@raspberrypi ~ $ sudo find / -name wolframalpha
/home/pi/.local/lib/python2.7/site-packages/wolframalpha-1.0.2-py2.7.egg/wolframalpha

pi@raspberrypi ~ $ ls -ld /home/pi/.local/lib/python2.7/site-packages/wolframalpha-1.0.2-py2.7.egg/wolframalpha
drwxr-xr-x 2 pi pi 4096 Jun 17 03:05 /home/pi/.local/lib/python2.7/site-packages/wolframalpha-1.0.2-py2.7.egg/wolframalpha

running the script DOES work with "sudo -E takeSpeech.py", however I'm adding this to an file in init.d which is automatically calling the python script using "sudo". Does anyone happen to know what I may need to change to get this working?

Re: trouble running python script with sudo

Posted: Sat Oct 12, 2013 7:01 am
by Joe Schmoe
I don't know anything about Python, but this sounds like a Python version (I.e., PATH variable setting) problem.

I had this same problem with the youtube-dl.py script - running on a machine with 2 versions of Python installed. The error messages look vaguely familiar - to those I get when I try to run the youtube script with a "too old" version of Python.

You just have to sort it out. Figure out which version is being invoked in the various scenarios. Or just full-path the python invocation in the command line...

Also note: There should never be any need to use "sudo" in an init.d script, since those scripts normally run as root anyway. But, re-reading your text, I think you already know this.

Re: trouble running python script with sudo

Posted: Sat Oct 12, 2013 7:16 am
by justStarting
after adding:

Code: Select all

import sys
print sys.version
then running the same script the version returns as "2.7.3 (default, Jan 13 2013, 11:20:46)
[GCC 4.6.3]" for with and without using sudo

Re: trouble running python script with sudo

Posted: Sat Oct 12, 2013 7:56 am
by mors3
How did you install the wolframalpha package?

Re: trouble running python script with sudo

Posted: Sat Oct 12, 2013 11:12 am
by justStarting
it was either "pip install wolframalpha" or "easy_install wolframalpha", it's been a while so I don't actually recall, I think it was pip though

Re: trouble running python script with sudo

Posted: Sat Oct 12, 2013 4:22 pm
by IanH2
It looks like the 'pi' user has a different PYTHONPATH environment variable setting to the root user.

You can print the Python path with:

Code: Select all

import sys
print sys.path
This is the list of directories which Python searches for modules in. To find where it gets a given module from, use its __file__ attribute, i.e.

Code: Select all

print wolframalpha.__file__
You can add things to the Python path in the program itself, like this:

Code: Select all

sys.path.append("/path/to/somewhere")
import wolframalpha

Re: trouble running python script with sudo

Posted: Sat Oct 12, 2013 10:33 pm
by justStarting
that appears to have done the trick, much appreciated =)

Re: trouble running python script with sudo

Posted: Mon Mar 09, 2020 1:43 am
by codysman
IanH2 wrote:
Sat Oct 12, 2013 4:22 pm
It looks like the 'pi' user has a different PYTHONPATH environment variable setting to the root user.

You're right - if you installed the packages for `pi` user they are not available for `sudo` user and vice-versa.
To solve the problem just do: `sudo su` and reinstall the problematic package.

Re: trouble running python script with sudo

Posted: Mon Mar 09, 2020 5:15 pm
by DirkS
codysman wrote:
Mon Mar 09, 2020 1:43 am
and vice-versa.
Wrong. Packages installed with sudo are installed globally and available for all users

Re: trouble running python script with sudo

Posted: Mon Mar 09, 2020 6:33 pm
by mahjongg
Locked because thread is 6 years old, and thus obsolete