justStarting
Posts: 12
Joined: Fri Jul 12, 2013 12:55 am

trouble running python script with sudo [obsolete]

Sat Oct 12, 2013 5:09 am

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?
Last edited by justStarting on Sat Oct 12, 2013 7:11 am, edited 1 time in total.

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: trouble running python script with sudo

Sat Oct 12, 2013 7:01 am

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.
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

justStarting
Posts: 12
Joined: Fri Jul 12, 2013 12:55 am

Re: trouble running python script with sudo

Sat Oct 12, 2013 7:16 am

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

mors3
Posts: 27
Joined: Fri Oct 11, 2013 6:04 pm
Location: Amesbury, UK

Re: trouble running python script with sudo

Sat Oct 12, 2013 7:56 am

How did you install the wolframalpha package?

justStarting
Posts: 12
Joined: Fri Jul 12, 2013 12:55 am

Re: trouble running python script with sudo

Sat Oct 12, 2013 11:12 am

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

IanH2
Posts: 79
Joined: Tue Dec 18, 2012 10:17 am

Re: trouble running python script with sudo

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 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
-----
https://github.com/IanHarvey

justStarting
Posts: 12
Joined: Fri Jul 12, 2013 12:55 am

Re: trouble running python script with sudo

Sat Oct 12, 2013 10:33 pm

that appears to have done the trick, much appreciated =)

codysman
Posts: 1
Joined: Mon Mar 09, 2020 1:40 am

Re: trouble running python script with sudo

Mon Mar 09, 2020 1:43 am

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.

DirkS
Posts: 10363
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: trouble running python script with sudo

Mon Mar 09, 2020 5:15 pm

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

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13093
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: trouble running python script with sudo

Mon Mar 09, 2020 6:33 pm

Locked because thread is 6 years old, and thus obsolete

Return to “Python”