Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use sudo with python inside virtualenvironment

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?

like image 740
Skrasa Avatar asked Mar 09 '26 04:03

Skrasa


1 Answers

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:

  • Pass the -E flag to SuDo

  • PATH needs to be carried manually ([StackExchange.Unix]: How to make `sudo` preserve $PATH?)

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).

like image 183
CristiFati Avatar answered Mar 11 '26 18:03

CristiFati



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!