简体   繁体   English

systemd中的Python脚本:虚拟环境还是真实环境

[英]Python script in systemd: virtual environment or real environment

I have been trying to run a python script on start-up (on a Pi).我一直在尝试在启动时(在 Pi 上)运行 python 脚本。 I initially did this via an .sh script triggered by cron.我最初是通过一个由 cron 触发的 .sh 脚本来做到这一点的。

Posting with a problem on StackExchange Pi ( https://raspberrypi.stackexchange.com/questions/110868/parts-of-code-not-running-when-autostarting-script-in-crontab ) the suggestion is to use systemd.在 StackExchange Pi 上发布问题( https://raspberrypi.stackexchange.com/questions/110868/parts-of-code-not-running-when-autostarting-script-in-crontab )建议使用 systemd。

The person helping me there has suggested not using a virtual environment when executing the Python script (they note their limited familiarity with Python), and using the real environment instead.在那里帮助我的人建议在执行 Python 脚本时不要使用虚拟环境(他们注意到他们对 Python 的熟悉程度有限),而是使用真实环境。 But other resources strongly suggest the use of a virtual environment (eg https://docs.python.org/3/tutorial/venv.html ).但其他资源强烈建议使用虚拟环境(例如https://docs.python.org/3/tutorial/venv.html )。

In the hope of setting this up correctly could anyone weigh in on the correct approach?希望正确设置这一点,任何人都可以权衡正确的方法吗?

systemd is going to try to run your script on startup so your virtual environment will not have been activated yet. systemd 将尝试在启动时运行您的脚本,因此您的虚拟环境尚未激活。 You can (maybe) avoid that issue by telling systemd to use the python in the virtualenv's bin, with the appropriate environment variables.您可以(也许)通过告诉 systemd 在 vi​​rtualenv 的 bin 中使用 python 并使用适当的环境变量来避免该问题。 Or you can activate as a pre-run step for that script's launch in systemd.或者您可以激活作为该脚本在 systemd 中启动的预运行步骤。 Maybe.也许。

But on balance I'd make it easy on systemd and your OS and ignore the virtualenv absolutists.但总的来说,我会让 systemd 和你的操作系统变得容易,而忽略 virtualenv 绝对主义者。 Get the script to work on your dev machine using virtualenv all you want, but then prep systemd to use the global python, with suitable packages installed.使用您想要的 virtualenv 使脚本在您的开发机器上运行,然后准备 systemd 以使用全局 python,并安装合适的软件包。 You can always use virtualenvs on that pi, for scripts that don't have to work with systemd.对于不必使用 systemd 的脚本,您始终可以在该 pi 上使用 virtualenvs。 Systemd doesn't always have the clearest error messages. Systemd 并不总是有最清楚的错误消息。

This answer is certainly opinion-based.这个答案当然是基于意见的。

Use the virtual environment.使用虚拟环境。 I don't see any reason not to.我看不出有什么理由不这样做。 At some point you might want to have multiple Python applications run at the same time on that system, and these applications might require different versions of the same dependency and then you would be back to square one, so... Use the virtual environment.在某些时候,您可能希望在该系统上同时运行多个 Python 应用程序,而这些应用程序可能需要相同依赖项的不同版本,然后您将回到原点,因此...使用虚拟环境。

When configuring systemd , crontab , or whatever, make sure to use the python binary that is placed inside the virtual environment's bin directory, so that there is no need to activate the virtual environment:在配置systemdcrontab或其他任何东西时,请确保使用放置在虚拟环境bin目录中的python二进制文件,这样就不需要激活虚拟环境:

/path/to/venv/bin/python -m my_executable_module
/path/to/venv/bin/python /path/to/my_script.py
/path/to/venv/bin/my_executable_script

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM