I have a very simple python script (to send an email) which works perfectly when I run it in the cmd window, or in python, or if I directly start a a .bat file pointing to it.
However, when I try to get the task scheduler to run it, nothing happens. The task scheduler says that it runs and completes successfully, the log file is blank, but no email is sent.
I'm aware there are a lot of other questions relating to this problem, and I've read through them and tried the solutions, but nothing seems to work. I am new to python (and to scheduling tasks!), so I may be implementing the solutions incorrectly.
Here is what I've tried...
Creating a batch file with the script in it in various ways:
python C:\Users\me\Documents\etc\script.py >C:\Users\me\Documents\etc\log.txt
python "C:\Users\me\Documents\etc\script.py >C:\Users\me\Documents\etc\log.txt"
C:\Users\me\AppData\Local\Programs\Python\Python35-32\python.exe C:\Users\me\Documents\etc\script.py
"C:\Users\me\AppData\Local\Programs\Python\Python35-32\python.exe" "C:\Users\me\Documents\etc\script.py"
C:\Users\me\Documents\etc\script.py
All of these work fine when double clicking on the file - but none run in task manager. (Although they say they have completed successfully). In task manager I just put the link to the .bat file in the "Program/script" box.
Doing the above but with the full path to the cmd.exe in the program/script, and the .bat file as an argument. I've also tried putting the location of the bat file in the "Start in (optional):"
Trying to run the .py file directly via the task scheduler, putting it in the "Program/script:" box
Trying to run the .py file directly via the task scheduler, putting the full path to the python.exe (see above) in the "Program/script", and the sript.py in the "Add arguments". I've also tried this with path to the location of the script in "Start in".
Trying to run the .py file via the cmd - so putting the full path to the cmd.exe in "Program/script" and the script.py file (full path) in the "Arguments".
Some of the ones where I try to run the script.py directly just say "running" in the Task Scheduler forever, but I didn't note down which these were.
I'm running Windows 7 (64bit), and have got Python 3.5.1 (32bit). I've got local admin rights.
Other scheduled tasks I have created (not involving a python script) work fine, but this has stumped me. Please help!
Just adding an answer in case this affects any other newbie :). I needed to check "Run only when user is logged on", and also uncheck "Run with highest privileges".
I'm guess this is because as eryksun says Outlook has a GUI.
Something else to try:
Make sure in your batch file you're adding a command to change directories to where your executable lives.
#echo off
echo.------------------------------------------------
echo.Windows Task invoked on %date%, %time% (local time)
echo.------------------------------------------------
SET My_exe_dir="C:\Program Files (x86)\MyProgram\FolderWhereExeLives"
SET Input_dir="C:\Program Files (x86)\MyInputFolder"
cd %My_exe_dir% <-- This was the key for me.
%My_exe_dir%\myprogram.exe %Input_dir%\MyInputFile.xml -1
For me, specifying the "start in" directory worked:
Define Start in directory in properties of scheduled task.
Start Windows Task Scheduler.
Navigate to the task and double click on it to open the Properties of the task.
Select tab Action and click on button Edit.
There is Start in (optional). Enter here the path of the executed batch file.
Click two times on button OK to save this important modification in properties.
Related
I will try to be brief!
For whatever reason, I couldn't make the entirety of my program in python, so I had to outsource one specific task to php (a language I do not know very well). As the python program runs, it is supposed to trigger the php program to run, and then do a few things afterwards which is not a problem.
It seems to me that, to be able to run something through python, you need it to be able to run through cmd first, and then you can make python use cmd to run the program. I had a few issues there, because the programs are on different drives, and the php program references other files and locations in the same directory and in sub-directories to where it is, this means I couldn't execute in one line of cmd, but first had to change directory, to then execute the php program from the folder it's in. Because my command wasn't just one line, I made a batch file containing all the steps.
My current working method is to open up cmd, change directory in cmd to where the php file is, and then run the php file. I had to add php to the "Environment Variable Path" to be able to do this. Here is the batch file that currently works when run by me:
cd /d C:
cd C:\Users\UserMain\Desktop\php\colorextract
php (2).php
When I double click this bat file, from my E drive, it successfully executes the php program. But when I tell python to execute the batch file, that is where things go wrong.
Here is my python code, apologies for the name of the bat file:
import os
os.system('cmd /k "bitch.bat"')
The resultant cmd window then goes thru the steps of the batch file: 1) it changes to the right directory, 2) it is unable to execute the php file because:
'php' is not recognised as an internal or external command, operable program or batch file.
Now, this is the standard error you get if you were to try running a php program without having added php to the "Environment Variable Path", I know this because I went through that same thing. But if I manually open a cmd window, not administrative or anything, I can 1) successfully perform the steps outlined in batch file, and program runs, and 2) I can even run the bat file, and that also runs the program.
The cmd window opened by python does not seem to be able to reference the "Environment Variable Path", or it is for another reason somehow handicapped against being able to do all the things that a normal cmd widow can. How can this be fixed?
Thanks in advance to anyone who reads this!
Edit: I found that python had not detected the changes I made to the environment variables the day before, hence why python's cmd was giving the exact error that not having php in the environment variable gives. After I restarted my computer, my code worked. Thank you to #Gerhard and #Aaron Junker for making me think much harder about this issue.
so I found a command that can be run after importing os.
print(os.environ)
I ran this, and it told me that Python could not see that php had been added to the environment variables, well, more likely that python did not have the most up to date information regarding what was in the path variable(s).
Restarting my computer made the changes kick in, and now my original code works. Whilst I do feel very stupid, I'm just happy that this is resolved.
This seems to me like both instances use different environment variables.
Open
System Properties -> Advanced -> environment variables and look that PHP is in the PATH variable in user variables and in System variables.
I have 2 python programs that act as 'services', each of them runs from its own venv. I'm looking to create a batch file to initiate both services, each on their own dedicated terminal.
I was able to create 1 batch file to initiate the venv and the service, but I cant merge them into one batch file.
CALL <PATH_TO_VENV_SCRIPTS>\activate
"<PATH_TO_PYTHON_FILE>main.py"
pause
When I run the file above, a terminal opens, activates the venv, and runs the main.py using the venv.
Now I'm trying to have 1 batch to start both services. I have the following:
START <PATH_TO_VENV1_ACTIVATE.BAT>
"<PATH_TO_SERVICE1_PYTHON_FILE>main.py"
START <PATH_TO_VENV2_ACTIVATE.BAT>
"<PATH_TO_SERVICE2_PYTHON_FILE>main.py"
The file above indeed opens 2 terminals, and activates both VENVs, but unfortunatelly the python files are initiated from a third terminal which opens before the aforementioned 2.
How can I get the python files to be initiated from their own VENV terminal?
Any help is greatly appreciated.
Thanks in advance!
You have to launch your python files as a command in your newly created venv terminal. If you start the python service from the parent script, they're executed in this terminal context.
In other words: You have to include the execution of the scripts into your PATH_TO_VENV1_ACTIVATE.BAT. However, if your python script does not terminate after execution, you won't ever get there and the "parent" terminal will hang on the first script. So make sure that the script runs in a way which does not block the parent calling context.
HTH
EDIT: If your scripts are starting a service anyway, consider creating a container and launch these from there. Therefore you have an encapsulated context paired with easy execution.
I'm running windows
I've got a python script that works as expected when I run it from my interpreter (Anaconda, i think that's an interpreter...), but when I run it from my file manager by double clicking the script ("script.py") I see a quick black screen flash, but nothing else. I'm using the input() function, so that's not the issue.
When I try the same thing with a simple test script, which is just the print() function and the input function, the command line or terminal screen, whatever the black screen is called, displays my printed string and closes only after I press enter.
I believe my issue has to do with file paths and working directories, but I'm a noob and I don't know how to solve this. My script declares this variable:
inv_folder=r"C:\Users\domin\OneDrive\Desktop\Test Folder" #folder to look for completed recipe files
which I believe is an absolute filepath. I then loop through the files in that folder and open and read them
for filename in os.listdir(inv_folder):
Main goal is to send my script to someone else's computer, and allow them to run it simply by double clicking on the file. Trying to do that on my computer and failing
Doubleclicking the .py file in Windows will generally run the file. In your case, if all your script is doing is looking for a directory and looping through files in it, it is quite possible that the script is working correctly, but that it is closing immediately upon completing the script.
E.g. if you have the following python file hello.py with the single line of code.
print("hello world")
If you doubleclick hello.py, you will only see a black window flash for a second, as the program runs and exits.
To check whether your code is working correctly or not, you are best to navigate to that folder in your command prompt and run the program within the command prompt. E.g. typing "hello.py" in the right folder will then show the correct output in the console.
Try running it in the console first, seeing if the code is giving any errors, and then working from there. But the behaviour you are describing is basically standard Windows behaviour and not necessarily an error.
If you need to pause the window after running, please see the following StackOverflow question and answers about "Python Equivalent to System('PAUSE')"
Have you considered to compile it?
You can compile it easily with pyinstaller and this command:
C:\<path_to_python>\Scripts\pyinstaller.exe --onefile -n <your script name> <your script path>
Of course replace the <> with the relevant paths for you.
I stumbled on an answer for basically the exact same question, but either I am implementing the solution wrong, the solution is no longer accurate, or some other issue is at play.
Run a python script in virtual environment from windows task scheduler
I used the code from above link, edited for my directories, and copied directly into the task scheduler program/script prompt.
C:\Users\I511843\myenv\activate.bat && python C:\Users\I511843\Desktop\Atom_scripts\test.py
I then set the program to run every time I unlocked the machine. The program simply writes a text file called test.txt containing the text "success!"
The text file populates in the correct directory when I run from the command line, however, the program doe snot execute from the task scheduler.
Did you try putting the commands in a batch file and executing the batch file from task scheduler? Something like below:
C:\Users\I511843\myenv\activate.bat && python C:\Users\I511843\Desktop\Atom_scripts\test.py goes into task.bat file.
In task scheduler, run task.bat from cmd.
I'm trying to schedule a python script through taskscheduler by making a batch-file.
Now please relax for a sec before saying this has already been answered. I have checked every single link and none of them worked for me.
Here's what I tried.
Normally putting python location in program/script and specifying the python file in arguments in task scheduler.
Trying through batch file
start C:\Python27\pythonw.exe C:\Users\...\time_convert.py
pause
Call C:\Python27\pythonw.exe C:\...\time_convert.py
pause
C:\Python27\pythonw.exe C:\Users\...\time_convert.py
pause
Also when I run the batch file manually it works fine. But not through task scheduler.
I can see a cmd window pops but a new text file doesn't gets created as per the .py script.
Here's the demo .py script:
print("done")
f=open("ay.txt",'w')
f.write("hi")
f.close()