10

I've got a script that uses the Google Assistant Library and has to import some modules from there. I figured out this only works in a Python Virtual Environment, which is really strange. In the same folder I've got a script which uses the GPIO pins and has to use root. They interact with each other, so when I start the GPIO script, the Assistant script is also started. But for some reason the modules in there can't import when the script is started with root. Does anybody know something about this?

Jan
  • 123
  • 1
  • 2
  • 8
  • `sudo` runs under a different environment, and your user installed packages aren't available. Sound like your issue? Why don't you activate the virtualenv as root? – OneCricketeer Jun 11 '17 at 12:54
  • Install all your packages as root, I suppose. Or create a root `venv`. – erip Jun 11 '17 at 12:54
  • @cricket_007 That sounds right, but how can I activate the venv as root? – Jan Jun 11 '17 at 12:57
  • @Jan `sudo su` followed by your normal steps to activate a venv. – erip Jun 11 '17 at 13:00
  • You don't necessarily need to activate, just run the script using it. https://stackoverflow.com/a/11963127/2308683 – OneCricketeer Jun 11 '17 at 13:10
  • @erip activating with sudo doesn't work, just says command not found. – Jan Jun 11 '17 at 13:37
  • @cricket_007 I was activating it with "source env/bin/activate" and since I'm not really a python pro I don't know how to do this now :/ – Jan Jun 11 '17 at 13:44

4 Answers4

12

not 100% sure but have you tried:

sudo -E python myScriptName.py

As mentioned here

Giorgos Xou
  • 1,461
  • 1
  • 13
  • 32
9

Normally you can active a virtual env and use the interpreter inside the env to run your script. But it is not necessary.

Suppose you have a virtual env under the path /path-to-env/env the script you want to run example.py is under the path /path-to-script/example.py

you can already run this example.py like

sudo /path-to-env/env/bin/python /path-to-script/example.py
milo
  • 1,747
  • 9
  • 10
  • This only says sudo: /env/bin/activate: command not found (/env/bin/activate is the path to my venv) Do I need to source this? – Jan Jun 11 '17 at 14:07
  • Have you already created a virtual env? You don't need to activate it, try to run your script like I wrote. – milo Jun 11 '17 at 14:12
  • you mean /path-to-env/env/bin/python /path-to-script/example.py works but it doesn't work with sudo? basically same command just with/out sudo behave differently? – milo Jun 11 '17 at 14:19
  • With sudo it says command not found. Without sudo it says permission denied. – Jan Jun 11 '17 at 14:20
  • can you start python interactive mode by using sudo /path-to-env/env/bin/python? – milo Jun 11 '17 at 15:03
  • Starting it with sudo /env/bin/python doesn't find the RPi GPIO module. Do I have to install modules again for this? – Jan Jun 11 '17 at 15:35
2

Try to install the module using sudo.

I had the same problem with the module 'reportlab' from python. I realized that I had installed pip (the installer manager for reportlab) without sudo command.

The problem is that the package (pip and reportlab) has been installed as user and not as root, so when you try to use sudo, it does not recognize the system path to reportlab because you never installed in the first place, only for the user!

I recommend install pip and module with sudo always:

For python 2:

$ sudo add-apt-repository universe
$ sudo apt update
$ sudo curl https://bootstrap.pypa.io/get-pip.py --output get-pip.py
$ sudo python2 get-pip.py
$ sudo pip install google-assistant-library

For python 3 (from Docs Google assistant library):

$ sudo apt-get update
$ sudo apt-get install python3-dev python3-venv
$ sudo python3 -m venv env
$ sudo env/bin/python -m pip install --upgrade pip setuptools
$ sudo source env/bin/activate
$ sudo python -m pip install --upgrade google-assistant-library

Hope this helps! Regards!

2

I ended up just installing the python package as sudo and it worked fine. For my case it was sudo pip3 install findpi and then executed as sudo findpi and worked.

james-see
  • 12,210
  • 6
  • 40
  • 47