问题
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?
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/49542912/schedule-a-script-developed-in-anaconda-via-windows-task-scheduler