Python 如何为 crontab 设置 virtualenv?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4150671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 14:28:47  来源:igfitidea点击:

How to set virtualenv for a crontab?

pythoncronvirtualenvvirtualenvwrapper

提问by Continuation

I want to set up a crontab to run a Python script.

我想设置一个 crontab 来运行 Python 脚本。

Say the script is something like:

说脚本是这样的:

#!/usr/bin/python
print "hello world"

Is there a way I could specify a virtualenv for that Python script to run in? In shell I'd just do:

有没有办法可以为该 Python 脚本指定一个 virtualenv 来运行?在 shell 中,我会这样做:

~$ workon myenv

Is there something equivalent I could do in crontab to activate a virtualenv?

我可以在 crontab 中做一些等效的事情来激活 virtualenv 吗?

采纳答案by Andy White

If you're using "workon" you're actually using "virtualenv wrapper" which is another layer of abstraction that sits on top of virtualenv. virtualenv alone can be activated by cd'ing to your virtualenv root directory and running:

如果您使用“workon”,您实际上是在使用“virtualenv 包装器”,它是位于 virtualenv 之上的另一层抽象。virtualenv 可以通过 cd 到您的 virtualenv 根目录并运行来激活:

source bin/activate

workon is a command provided by virtualenv wrapper, not virtualenv, and it does some additional stuff that is not necessarily required for plain virtualenv. All you really need to do is source the bin/activate file in your virtualenv root directory to "activate" a virtualenv.

workon 是由 virtualenv 包装器提供的命令,而不是 virtualenv,它执行一些普通 virtualenv 不一定需要的额外内容。你真正需要做的就是在你的 virtualenv 根目录中找到 bin/activate 文件来“激活”一个 virtualenv。

You can setup your crontab to invoke a bash script which does this:

您可以设置您的 crontab 来调用执行此操作的 bash 脚本:

#! /bin/bash    
cd my/virtual/env/root/dir
source bin/activate

# virtualenv is now active, which means your PATH has been modified.
# Don't try to run python from /usr/bin/python, just run "python" and
# let the PATH figure out which version to run (based on what your
# virtualenv has configured).

python myScript.py

回答by Mike Pennington

Another solution that works well for me...

另一个对我来说效果很好的解决方案......

0    9    *    *    *    /path/to/virtenv/bin/python /path/to/cron_script.py

I prefer using python directly from the virtualenv...

我更喜欢直接从 virtualenv 使用 python ......

回答by Ross Rogers

With bash, you can create a generic virtual env wrapper that you can use to invoke anycommand, much like how timecan wrapper any command.

使用 bash,您可以创建一个通用的虚拟环境包装器,您可以使用它来调用任何命令,就像如何time包装任何命令一样。

virt_env_wrapper.bash:

virt_env_wrapper.bash

#!/bin/bash    
source path/to/virtual/env/bin/activate
"$@"

Bash's magical incantation "$@"re-escapes all tokens on the original command line so that if you were to invoke:

Bash 的魔法咒语会"$@"重新转义原始命令行上的所有标记,因此如果您要调用:

virt_env_wrapper.bash python foo.py bar 'baz blap'

foo.pywould see a sys.argvof ['bar', 'baz blap']

foo.py将看到sys.argv['bar', 'baz blap']