Search code examples
pythonpython-3.xwindowsscheduled-taskswindows-task-scheduler

How to fix Last Run Result (0x2331) error when running a python script with Windows Task Scheduler


I am trying to run a python script with Windows Task Scheduler but the Last Run Result is 0x2331 for some reason. This error seems to indicate a windows path issue.

Dependencies

I am using Windows 10 Pro (64 bit) and the absolute path to my python executable is C:\Users\user\AppData\Local\Microsoft\WindowsApps\python.exe.

My python script

The absolute path to my python script is C:\Users\user\.spyder-py3\AutoScript\test.py. It simply writes the current time to a text file. If text file does not exist, it creates the text file:

# -*- coding: utf-8 -*-
import datetime

with open('C:/Users/user/.spyder-py3/AutoScript/readme.txt', 'a+') as f:
    f.write(f'{datetime.datetime.now()}\n')

If I run the script normally in Spyder 3 IDE (Anaconda 3) with Python 3.9.7, readme.txt gets created with no errors and the file content is as expected. Therefore, there are no issues with the script.

Creating task with Task Scheduler

I have included screenshots of the settings in Task Scheduler. Click on the images to view them in high resolution.

enter image description here

enter image description here

enter image description here

Result

When I run the task on Task Scheduler and then refresh Task Scheduler, the "Last Run Result" of my task is (0x2331).

What I tried

I tried messing around with the different settings in Task Scheduler with no success. The first 3 columns represent the settings details and the last column contains the result obtained.

Program/script Add arguments : Start in : Last Run Result
C:\Users\user\AppData\Local\Microsoft\WindowsApps\python.exe C:\Users\user.spyder-py3\AutoScript\test.py (0x2331)
"C:\Users\user\AppData\Local\Microsoft\WindowsApps\python.exe" "C:\Users\user.spyder-py3\AutoScript\test.py" (0x2331)
C:\Users\user\AppData\Local\Microsoft\WindowsApps\python.exe test.py C:\Users\user.spyder-py3\AutoScript\ (0x2331)
C:\Users\user\AppData\Local\Microsoft\WindowsApps\python.exe "test.py" C:\Users\user.spyder-py3\AutoScript\ (0x2331)
"C:\Users\user\AppData\Local\Microsoft\WindowsApps\python.exe" "test.py" "C:\Users\user.spyder-py3\AutoScript" The directory name is invalid (x8007010B)
"C:\Users\user\AppData\Local\Microsoft\WindowsApps\python.exe" "test.py" C:\Users\user.spyder-py3\AutoScript\ (0x2331)

Batch file

Using this post as reference, I also tried placing using a executePy.bat file. It was placed in the same folder as test.py.

executePy.bat:

@echo off
"C:\Users\user\AppData\Local\Microsoft\WindowsApps\python.exe" "test.py"

The settings in Task Scheduler were as follows:

Program/script Add arguments : Start in : Last Run Result
C:\Users\user.spyder-py3\AutoScript\executePy.bat (0x2331)

None of the above changes solved my issue. Any help is appreciated. Thanks.


Solution

  • Problem

    When I type where python into the command prompt, I get

    C:\Users\user\anaconda3\python.exe
    C:\Users\user\AppData\Local\Microsoft\WindowsApps\python.exe
    

    However, when I type python into the command prompt, Microsoft Store opens up and I am asked to download Python 3.10 interpreter.

    The command prompt was not recognizing my current python interpreter.

    Solution

    To fix the problem, I had to add python to my environment variables in windows.

    Configuring environment variables

    For some reason the first python interpreter from C:\Users\user\AppData\Local\Microsoft\WindowsApps\python.exe did not resolve the issue so instead I decided to use the python interpreter from Anaconda.

    Following this tutorial, I added C:\Users\user\anaconda3\Scripts\ and C:\Users\user\anaconda3\ to the Path variable in the system variables section.

    Now when I type python in the command prompt I get:

    Python 3.9.7 (default, Sep 16 2021, 16:59:28) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
    

    and a warning about an unactivated conda environment. I ignored the warning for now.

    Creating batch file

    Using this post as reference, I created a executePy.bat file in the same folder as my script test.py. The executePy.bat contains :

    @ECHO OFF 
    python C:\Users\user\.spyder-py3\AutoScript\test.py
    

    C:\Users\user\.spyder-py3\AutoScript\test.py is the absolute path to the python script I want to execute.

    Note

    For simplicity, I did not activate any anaconda environment. Therefore, if I try to import externallibraries in my script, the task will not run successfully. The solution would be to activate an environment on anaconda with the required libraries first.

    More info on activating anaconda environment can be found at:

    configuring task on task scheduler

    enter image description here

    Program/script Add arguments : Start in :
    C:\Users\user.spyder-py3\AutoScript\executePy.bat

    Result

    Last Run Result : The operation completed successfully (0x0).

    These posts might be useful if you still need help after correctly configuring the environment variables: