Running python scripts in Anaconda environment through Windows cmd

2024/9/30 19:38:27

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

Answer

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>
https://en.xdnf.cn/q/71044.html

Related Q&A

How to work out ComplexWarning: Casting complex values to real discards the imaginary part?

I would like to use a matrix with complex entries to construct a new matrix, but it gives me the warning "ComplexWarning: Casting complex values to real discards the imaginary part".As a resu…

Is it possible to use POD(plain old documentation) with Python?

I was wondering if it is possible to use POD(plain old documentation) with Python? And how should I do it?

ctypes error AttributeError symbol not found, OS X 10.7.5

I have a simple test function on C++:#include <stdio.h> #include <string.h> #include <stdlib.h> #include <locale.h> #include <wchar.h>char fun() {printf( "%i", 1…

Filtering negative timedeltas

Consider a series holding timedelta64[ns] that measures at the time difference between two events A and B:> time_deltas499900 -1 days +23:45:13 499916 -1 days +23:50:57 499917 00:03:2…

What is the Matlab equivalent of the yield keyword in Python?

I need to generate multiple results but one at a time, as opposed to everything at once in an array.How do I do that in Matlab with a generator like syntax as in Python?

Convert from CMYK to RGB

Im having trouble converting a single page pdf (CMYK) to a jpg (RGB). When I use the code below, the colors in the jpg image are garish. Ive tried reading through the Wand docs, but havent found anythi…

TopologicalError: The operation GEOSIntersection_r could not be performed

Hi Guys, I am trying to map the district shapefile into assembly constituencies. I have shape files for [Both].Basically I have to map all the variables given at district level in census data to assemb…

Plot a function during debugging in Python

I used to work in Matlab and it is really convenient (when working with big arrays/matrices and nested functions) to visualize intermediate results during debugging using plot function. In Python I can…

getting last n items from queue

everything I see is about lists but this is about events = queue.queue() which is a queue with objects that I want to extract, but how would I go about getting the last N elements from that queue?

Paramiko ValueError p must be exactly 1024, 2048, or 3072 bits long

I am trying to connect SFTP using Python script. Im unable to connect due to "p error".import paramiko client = paramiko.SSHClient() client.load_system_host_keys() client.connect(####.com, us…