1

I want to schedule a Python script to start at 3AM and break at 5PM every weekday. However the problem arises when I need to start virtualenv as all the packages are install in a virtual environment.

I can get the script to run at a specific time but I can't activate the virtual environment (I'd normally type "env\scripts\activate"). Here is what my action looks like:

enter image description here

How can I incorporate activating virtualenv and what should my action look like to break the script?

5 Answers 5

1

My workon.bat script wouldn't set Env properties properly but this final command worked in Task Scheduler:

cmd /C c:/Users/name/Env/workon_name/Scripts/activate.bat & python run_script.py

1

You create a Windows batch file e.g. MyProgram.bat with the following content:

cd C:\PathToYourPythonFile
call C:\PathToYourCondaInstallation\Scripts\activate.bat YourEnv
python YourPythonFile.py

Make sure you replace YourEnv with a conda environment that you set up before.

You can start the batch file from your Task Scheduler, no additional arguments needed.

Add on:

Alternatively, to avoid the pop-up window I let the Task Scheduler call a three-line MyProgram.vbs file instead which calls the above batch file like this:

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\PathToYourBatchFile\MyProgram.bat" & Chr(34), 0
Set WshShell = Nothing

It is a bit awkward to always have three files (what would you expect with Windows...) but I use the same names for the files, so it's OK. I'd be happy to hear a better solution.

1
  • This (@sdittmar) worked for me
    – user88484
    Aug 23, 2020 at 10:16
0

What if you create a batch file, e.g. test.bat, that first activates your virtual environment, then runs your python script? Something like:

env\scripts\activate
D:\Python\Aldabra\env\Scripts\python.exe "D:\Python_scpt\test.py"

Then have that script run from Windows Task Scheduler.

In your script, I would also suggest making the env\scripts\activate part not depend on where the script is run. i.e. make it an absolute path.

0

you can create it in the command line using schtasks and specifying the path to the python executable inside your virtual environment directory

schtasks /Create /SC HOURLY /TN VirtualEnvPythonTask /TR "PATH_TO_VIRTUAL_ENV_PYTHON_EXECUTABLE PATH_TO_PYTHON_SCRIPT"

alternatively, you can use the task scheduler GUI with Action: start a program Set the Program/script: to the python executable in the virtual environment path, and add the python script in the Add arguments (optional): field

For example, I'm using conda, with an environment call MSSQL-ETL-ENV. The path to python would be

C:\Users\dbaker\AppData\Local\Continuum\anaconda3\envs\MSSQL-ETL-ENV\python
0
0

When you are directly calling the python.exe file in the virtual environment, you are indeed indirectly activating the virtualenvironment.

When you type .\env\Scripts\activate, you are just changing the path variable, such that the path to your Lib directory which contains the libraries installed in your virtualenvironment to occur first. As a result when python searches for those libraries it finds these libraries in the virtualenv first and uses them.

If you dont have any other constraints, then just using C:\path_to_virtualenvip\python.exe C:\path_to_python_file.py

should be enough to run the python file in your virtual-environment.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .