2

Dependencies

OS : Windows 10 Pro (64 bit)
IDE : Spyder 3 (Anaconda 3) with Python 3.9.7

Absolute path to python exe : C:\Users\user\AppData\Local\Microsoft\WindowsApps\python.exe

Absolute path to python script : C:\Users\user\.spyder-py3\AutoScript\test.py

My python script

# -*- 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')

The script simply writes the current time to a text file. If text file does not exist, it creates the text file.

If I run the script normally in my IDE, readme.txt gets created with no errors and the file content is as expected.

Creating task with 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) which seems to be a windows path issue

What I tried

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)

using batch file

Reference executePy.bat is in the same folder as test.py

executePy.bat

@echo off
"C:\Users\user\AppData\Local\Microsoft\WindowsApps\python.exe" "test.py"
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.

Bunny
  • 1,180
  • 8
  • 22

1 Answers1

1

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

When I type python into the command prompt, Microsoft Store opens up where I have the option to download Python 3.10 interpreter.

The command prompt was not recognizing the current python interpreter I had. To fix the problem, I had to add python to my environment variables in windows.

Solution

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

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 other libraries such as panda 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 : Link 1 Link 2

Then use the script given here

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)

Other related posts if you still need help after correctly configuring the environment variables

Run a batch file with Windows task scheduler

Running a python script through Windows Scheduler not working

Scheduling a .py file on Task Scheduler in Windows 10

Schedule a script developed in Anaconda via Windows Task Scheduler

Run a python script in virtual environment from windows task scheduler

Bunny
  • 1,180
  • 8
  • 22