Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

running a python script in a pyenv environment with sudo or withing a systemd service #2945

Open
sterwen opened this issue Apr 14, 2024 · 2 comments

Comments

@sterwen
Copy link

sterwen commented Apr 14, 2024

This is not a bug more a limitation.
Haven't found a way to run a script installed in a virtual environment as sudo or from systemd.

Both sudo or systemd start from a minimal environment so the modules are not found.
sudo <script> is launched from the virtual environment

From systemd, the solution seems to activate the virtualenv for the execution, but if some scripts in the application need to run as root we are in the same problem.

Bottom line, the is always the solution to hard code everything and fully ditch pyenv for the execution environment but to distribute widely or application this is a limitation.

Description

Tested on Debian 12
Python 3.11
pyenv 2.4.0-2-ge1f07da7

@native-api
Copy link
Member

native-api commented Apr 14, 2024

Set up the running shell's environment the same way as you set up your regular shell's environment to use Pyenv prior to running the needed command.
Alternatively, if you need just a single virtualenv environment to work, just sourcng its activate prior to running the command would probably suffice.

@aphedges
Copy link
Contributor

It's not the most portable solution, but you can directly hard code the path to the python executable installed with pyenv. Here is an example for this case that I used years ago (source):

[Service]
ExecStart=/home/<user>/.pyenv/versions/3.12.3/bin/python <script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants