从 Windows 任务调度程序在虚拟环境中运行 python 脚本 [英] Run a python script in virtual environment from windows task scheduler

查看:56
本文介绍了从 Windows 任务调度程序在虚拟环境中运行 python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 Windows 任务调度程序设置重复的 Python 任务.

I'm trying to set up a recurring Python task through windows task scheduler.

当我输入python.exe"的路径并将脚本的路径作为参数提供给 Windows 任务调度程序时,我已经成功(见下面的截图)

I have had success when I input the path to 'python.exe' and provide the script's path as a parameter to windows task scheduler (see screenshot below)

但是,我希望能够选择一个特定的虚拟环境来运行脚本.我对 venv 了解不多,我通常通过打开 cmd 并在所需的虚拟环境目录中运行 Scripts\activate.bat 来使用它.

However, I want to be able to choose a particular virtual environment in which to run the script. I don't have much knowledge of venv, and I typically use it by opening cmd and running Scripts\activate.bat in the desired virtual environment directory.

如何使用 Windows 任务调度程序完成每 24 小时在 venvxxx 中运行任务 x"?

How can I accomplish 'run task x in venvxxx every 24 hours' using windows task scheduler?

推荐答案

使用以下命令创建批处理文件:

Create batch file with these commands:

c:\__full_path_to_virtualenv__\Scripts\activate.bat && python __full_path_to_python_script__.py

&& 表示如果 command1 成功完成,则运行 command2.

&& means run command2 if command1 completed successfully.

然后将该批处理文件设置为要运行的脚本.您不需要在任务调度程序中设置任何其他参数(或者您可以在批处理文件中设置它们)并且可以设置 Start in 如果脚本必须从特定目录读取/写入并使用相对路径.

Then set that batch file as script to run. You don't need to set any additional arguments in task scheduler (or you can set them in batch file anyway) and can set Start in if script has to read/write from specific directory and uses relative paths.

这篇关于从 Windows 任务调度程序在虚拟环境中运行 python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆