7

I have the following goal: I have a python script, which should be running in my custom Anaconda environment. And this process needs to be automatizated.

The first thing I've tried was to create an .exe file of my script using pyinstaller in the Anaconda command prompt, opened in my environment. And put the .exe into Windows Task Scheduler. But I did not succeeded cause my script seems to be too complex, contain too many imports so pyinstaller didn't create the .exe.

The next thing I thought of was an attempt to run my script using Windows CMD with appropriate attributes, and also put it into Windows Task Scheduler.

Now my question is if there is a way to set up Task Scheduler so it could run CMD with attributes, which would activate my environment and with this environment run my script right away? I need this to be done automatically once a day at a given time.

Update 3: am I blind or what? enter image description here I mean, here it is:enter image description here

1 Answer 1

13

You could

  1. Create a .bat file (e.g. run_python_script.bat) with contents shown below.
  2. Create task in "Task Scheduler" to run the .bat file.

1.a. The .bat file contents with conda environments

  1. Check your <condapath>. Your conda.exe is located at <condapath>/Scripts.
  2. Put into your .bat file
call "<condapath>\Scripts\activate.bat" <env_name> & cd "<folder_for_your_py_script>" & python <scriptname.py> [<arguments>]
  • <env_name> is the name of the conda environment.
  • <folder_for_your_py_script> is the folder that contains <scriptname.py>
  • <scriptname.py> is the script you want to start.
  • [<arguments>] represent the optional arguments (if you need to give arguments to your script)

1.b. The .bat file contents with venv

"<path_to_python_exe>" "<path_to_python_script>" [<arguments>]

where

  • <path_to_python_exe> is the path to your python executable. If you are using a virtual environment (venv), then use the python.exe found in the /venv/Scripts folder
  • <path_to_python_script> is the path to your python script.
  • [<arguments>] represent the optional arguments (if you need to give arguments to your script)

2. Creating task in Task Scheduler

  1. Go to "Task Scheduler" -> "Create Basic Task"
  2. Give the name & timing info
  3. Add to the "Program/Script" the path to your run_python_script.bat.

Appendix: Creating venv with Anaconda

It seems that conda create command does not create similar virtual environments as python -m venv command. To create normal python virtual environment with the venv

  1. Check your <condapath>. Your conda.exe is located at <condapath>/Scripts.
  2. Create virtual environment to folder you want (let's call it venv_folder), by running following command in <venv_folder>
<condapath>\python.exe -m venv venv
  1. Now, your <path_to_python_exe> will be <venv_folder>\venv\Scripts.python.exe.
  2. If you need to install packages to this virtual environment, you use
<venv_folder>\venv\Scripts.python.exe -m pip install <package_name>
11
  • 1
    I downloaded Anaconda to test this myself and seems that the environments created with conda create are not "regular python virtual environments". I edited the answer to include instructions how to use venv with Anaconda.
    – Niko Fohr
    Commented Jul 20, 2020 at 10:37
  • 1
    I see. I updated the answer for conda environments.
    – Niko Fohr
    Commented Jul 20, 2020 at 14:47
  • 1
    I tested the answer again and checked it works on Task Scheduler. On my PC, everything was ok. I can only think of two possible options (1) The folder or file has a typo (2) Some permission problem keeps the Task Scheduler from accessing the script. In that case, I would try to run a test script in another folder. So, when double clicking the .bat, does it run the script? How about, if you move the bat to any other directory and double click? Does it work then, too?
    – Niko Fohr
    Commented Jul 21, 2020 at 14:22
  • 1
    Good to hear you got it working. I have to say that I don't know why the Task Scheduler could not access your Dropbox folder. Maybe it could be a topic of another question (on SuperUser, perhaps).
    – Niko Fohr
    Commented Jul 21, 2020 at 17:22
  • 1
    You can't imagine how greatful I am for your support! Even one big THANK YOU cannot be enough. I've lastly updated the question, maybe you can see difference
    – Outlaw
    Commented Jul 21, 2020 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.