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?
2 Answers 2
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).
2 Comments
/user/bin/python, not the one thats in the virtual environmentI 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.
5 Comments
sudo ./name_of_venv/bin/python and check the installed version, it is not the tensorflow build that I wanna usewhich python telling when you are running correct one?sudo /somePath/someFolder/myEnvironment/bin/python to use correct that as sudo. Or maybe you have installed Tensorflow into other location you intended?sudo /somePath/someFolder/myEnvironment/bin/python I get unable to execute permission deniedExplore related questions
See similar questions with these tags.
sudo path/to/venv/bin/python somescript.py?