Schedule a script developed in Anaconda via Windows Task Scheduler
Asked Answered
B

3

7

I'm trying to use Windows Task Scheduler to run a script in python and write a csv file. I've always used Anaconda, so I don't understand how Python's command line works. If I run this on Spyder,

import pandas as pd
import datetime
now_is = pd.DataFrame(['Now is '+ str(datetime.datetime.now())])
now_is.to_csv('C:/Users/camila/now_is.csv')

it works perfectly. But Task Scheduler executes this .py using the command terminal, where this code won't work.
I guess I need to install pandas again, but I can't even get pip to work on this...

Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> import pip
>>> pip.__version__
'9.0.1'

>>> pip install pandas
  File "<stdin>", line 1
    pip install pandas
              ^
SyntaxError: invalid syntax
>>>
  • How can I schedule a script developed in Anaconda on Windows Task Scheduler?
  • How can I import the modules that I have in Anaconda in the command line?
Bobsled answered 28/3, 2018 at 19:44 Comment(5)
The title contradicts the question. Which one to answer -- how to schedule a script or how to run a shell command from Python console?Dreda
To install something with pip on Windows, you should run it in the Windows console (sometimes called the command-line prompt) — not from the Python console.Thisbee
Should it be "How can I schedule a script developed in Anaconda on Windows Task Scheduler?" ? I'm super new in python so I confuse the terinology @DredaBobsled
Where you developed the script isn't relevant.Thisbee
Does this answer your question? Run a python script in virtual environment from windows task schedulerFunnyman
G
24

Follow these instructions:

  • Create a bat file.
  • Then add this code:
@ECHO OFF 
TITLE Execute python script on anaconda environment
ECHO Please Wait...
:: Section 1: Activate the environment.
ECHO ============================
ECHO Conda Activate
ECHO ============================
@CALL "C:\Users\user\AppData\Local\Continuum\anaconda3\Scripts\activate.bat" TestEnvironment
:: Section 2: Execute python script.
ECHO ============================
ECHO Python test.py
ECHO ============================
python C:\Users\user\PycharmProjects\Test\test.py

ECHO ============================
ECHO End
ECHO ============================

PAUSE

Ref Run a python script in virtual environment from windows task scheduler

Grecian answered 4/11, 2019 at 14:47 Comment(1)
Thanks!! This worked. Note for other users, change "TestEnvironment" to your desired env, ie, what you type for f"conda activate {TestEnvironment}"... ha. (obviously my comment is a little facetious using f-strings and makes no sense, but I hope the logic helps a little).Formation
Q
0

To use pip, you need to run it from the Windows Command Prompt, CMD.EXE. It should show up if you type cmd at the Start menu.

When you go to schedule a Python script, use the "create a basic task" wizard (the full version is needlessly complicated), set the action to "start a program," the program to run as python.exe, and put the script's path and arguments in the arguments box.

Quadruplex answered 29/3, 2018 at 16:45 Comment(0)
S
0

to add to @Nag, it took me quite a while, do find out that the default anaconda environment is simply "base".

Shellbark answered 10/11, 2021 at 8:39 Comment(2)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Grass
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewSmiley

© 2022 - 2024 — McMap. All rights reserved.