简体   繁体   中英

Task Scheduler doesn't work when my Python 3.6 script has the requests module imported into it.

I have the following .py file scheduled in task scheudler:

import time

print ("hello world")
time.sleep(10)
print ("hello world 2")

It seems to run fine in task scheduler. However, as soon as I import other modules like requests or pyodbc or tweepy like so:

import time
import requests

print ("hello world")
time.sleep(10)
print ("hello world 2")

the script doesn't run. It seems that it crashes when it tries to import requests. Why is this? Is there a way around this error?

I'm using Windows 10 64bit

Late response but hopefully it helps someone.

I had the same issue but for another module (pyautogui). I had the Program/Script for my task as just python originally and the "Add arguments (optional)" field was the path to my file. I found out that the version of Python that Task Scheduler was running was different than the one that ran when I typed python file.py in command prompt. So

  1. I'm assuming you can run your script normally with Python with command prompt and it works. Find the location of the python.exe that runs when you run Python like that (just import sys and then print sys.path )
  2. Change the Program/Script to be the path to that exe (eg C:\\Python36\\python.exe ), keeping your Argument field the same as before (the path to your Python file you want to run.)

If no luck, then make sure you pip -r install requirements.txt outside of your venv if you're using one, since Task Scheduler's Python won't have access to those.

If that doesn't work you can also try switching from "Run whether user is logged on or not" to "Run only when user is logged on" and checkmark "Run with highest privileges".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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