3

Hi I am trying to run a python script as sudo from inside my virtualenvironment.

When I have activated my virtualenvironment I would normally use python somescript.py and my script starts up with the correct version of python and everything

When I use sudo python somescript.py I load up the wrong python install, which is not the one from my environment.

How do I resove this?

asked May 10, 2020 at 16:52
4
  • You need to install the correct version of python in your virtual env. Are you using Conda for your enviroments or something else? Commented May 10, 2020 at 16:54
  • I am using venv I created the environment with this command python3 -m venv ‘Name‘ Commented May 10, 2020 at 16:57
  • 1
    Why does your Python script need sudo privileges? Commented May 10, 2020 at 17:01
  • Very often, activating the virtual environment is not necessary. Have you tried something like sudo path/to/venv/bin/python somescript.py? Commented Jan 31, 2021 at 11:53

2 Answers 2

7

The Activate script sets some environment variables (defines some functions, ...), which facilitate invoking Python (and tools).
One way (more like a workaround) of achieving your goal, would be the variables to be carried across the [man7]: sudo(8) session. For that, you need to:

All in all:

sudo -E env PATH=${PATH} python somescript.py

Output (works for simple commands):

(py_venv_pc064_03.05.02_test0) [cfati@cfati-ubtu16x64-0:~/Work/Dev/StackOverflow/q061715573]> ~/sopr.sh
### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###
[064bit prompt]> _PY_CODE="import sys, os; print(\"EXE: {:s}\nPATH: {:s}\n\".format(sys.executable, os.environ[\"PATH\"]))"
[064bit prompt]>
[064bit prompt]> python3 -c "${_PY_CODE}"
EXE: /home/cfati/Work/Dev/VEnvs/py_venv_pc064_03.05.02_test0/bin/python3
PATH: /home/cfati/Work/Dev/VEnvs/py_venv_pc064_03.05.02_test0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
[064bit prompt]>
[064bit prompt]> sudo python3 -c "${_PY_CODE}"
EXE: /usr/bin/python3
PATH: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
[064bit prompt]>
[064bit prompt]> sudo -E env PATH=${PATH} python3 -c "${_PY_CODE}"
EXE: /home/cfati/Work/Dev/VEnvs/py_venv_pc064_03.05.02_test0/bin/python3
PATH: /home/cfati/Work/Dev/VEnvs/py_venv_pc064_03.05.02_test0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

The one way that never fails in this kind of situations, is using (Python's) executable full path (although [SO]: How to install a package for a specific Python version on Windows 10? (@CristiFati's answer) is for a different OS, it still applies). But since that's just a SymLink, you'd probably want to preserve the environment anyway:

sudo -E env PATH=${PATH} /somePath/someFolder/myEnvironment/bin/python somescript.py

The other way around: create a Shell (wrapper) script that activates the environment (and other prerequisites), then calls the Python script*, and call that one with SuDo (or manually: suso su - then in the console run the required commands).

answered May 10, 2020 at 17:06
Sign up to request clarification or add additional context in comments.

2 Comments

this still loads up the python from /user/bin/python, not the one thats in the virtual environment
You;re probably not in the venv when you launch sudo.
1

I think this is answered in here: https://askubuntu.com/questions/234758/how-to-use-a-python-virtualenv-with-sudo

The issue is almost certainly that when you run sudo, the virtualenv environment variables, aliases, functions, etc aren't being carried over.

The solution would be to explicitly run the virtual environment's Python executable with sudo. For example if your virtualenv is ./AwesomeProject, then you could run sudo ./AwesomeProject/bin/python to use the script with the virtualenv with root privileges.

answered May 10, 2020 at 17:01

5 Comments

when I try doing this, it does not load the correct python it seems. Basically I am doing this whole thing to work with tensorflow, specifically the newest nightly build of that. When I open python with sudo ./name_of_venv/bin/python and check the installed version, it is not the tensorflow build that I wanna use
what is which python telling when you are running correct one?
which python is showing me the path to the virtual environment /somePath/someFolder/myEnvironment/bin/python while sudo which python is giving me /user/bin/python
So you should be use sudo /somePath/someFolder/myEnvironment/bin/python to use correct that as sudo. Or maybe you have installed Tensorflow into other location you intended?
when I try to just load up that python with sudo /somePath/someFolder/myEnvironment/bin/python I get unable to execute permission denied

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.