frans
frans

Reputation: 9798

How do I avoid pollution of a .venv folder with files owned by root when running a script with root privileges?

In virtual environment (created with poetry) I'm running a Python script regularly as current user via

poetry run ./script.py

or

.venv/bin/python3 script.py

From within this script I need to run another script with root privileges, which boils down to running that script via sudo:

sudo .venv/bin/python3 other_script.py

or

ssh root@localhost /<path>/<to>/.venv/bin/python3 /<path>/<to>/other_script.py

(I probably could also setuid the script but I'd prefer the ssh-way since I need it anyway. Also installing the virtual environment for root is not an option for me)

Running a script this way will create __pycache__ folders and .pyc files owned by root and running python3 -B or setting PYTHONDONTWRITEBYTECODE will not create any bytecode files at all (which I'd also like to avoid).

On an abstract level - is there a nice way to run a Python3 script from an existing virtual environment owned by current user with root privileges without having to fiddle with setuid or missing pyc files? Can I somehow tell Python (or the current environment) to create pyc files/folder with certain ownership or at a location owned by root?

Upvotes: 1

Views: 104

Answers (0)

Related Questions