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?
TylerH
  • 20,799
  • 66
  • 75
  • 101
Camila Burne
  • 156
  • 1
  • 1
  • 8
  • The title contradicts the question. Which one to answer -- how to schedule a script or how to run a shell command from Python console? – ivan_pozdeev Mar 28 '18 at 19:49
  • 2
    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. – martineau Mar 28 '18 at 19:51
  • 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 @ivan_pozdeev – Camila Burne Mar 28 '18 at 19:52
  • Where you developed the script isn't relevant. – martineau Mar 28 '18 at 19:52
  • Does this answer your question? [Run a python script in virtual environment from windows task scheduler](https://stackoverflow.com/questions/34622514/run-a-python-script-in-virtual-environment-from-windows-task-scheduler) – TylerH Aug 09 '23 at 19:55

3 Answers3

23

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

Nag
  • 231
  • 2
  • 4
  • 3
    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). – Jennings May 05 '20 at 07:19
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.

Alexander Martin
  • 443
  • 3
  • 12
0

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

bitterjam
  • 117
  • 11
  • 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](/help/how-to-answer). – Community Nov 10 '21 at 08:41
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/30303951) – ikamen Nov 10 '21 at 12:15