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?

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

| improve this answer | |
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.

| improve this answer | |
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
| improve this answer | |
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.

| improve this answer | |
0

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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