I am new to python and I have written my first program and run it a few times in the day. The file is called quran_review_scheduler.py in a folder under my user directory 'usr/dev/python/learning_python'. When I want to run this file, I open up Terminal and change directory to learning_python and run python3 quran_review_scheduler.py which runs fine. Now I want to make this script run every hour.
I have done some research and found launchd - 2 tutorials I have read are:
https://nathangrigg.com/2012/07/schedule-jobs-using-launchd
Then I created a plist that has the following contents.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>quran_scheduler</string>
<key>ProgramArguments</key>
<array>
<string>/Users/siraj/Dev/python/learning-python/quran_review_scheduler.py</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
Then I used the following statements:
launchctl load /Users/siraj/Library/LaunchAgents/quran_scheduler.plist
I did not get any output, hence I assumed that the job would start, but it did not start. Then I explicitly called the start, but nothing happened.
launchctl load /Users/siraj/Library/LaunchAgents/quran_scheduler.plist
Then, I started looking around to see whether there could be any other issues and came across this thread - How do I make a python file executable on macOS Sierra?
hence, I made a copy of quran_review_scheduler.py and named it quran_review_scheduler.command. Then I did chmod +x quran_review_scheduler.command to make it executable. Then, I changed the shebang line at the top to point to python3
#!/usr/bin/env python3
When I double-clicked on it, it was complaining that it is not able to find the dateparser - All I understand was that this python3 is not is used when I was running the program from Terminal. Then, by trial and error, I found out there is another python3 here and made that the first line.
#!/Library/Frameworks/Python.framework/Versions/3.8/bin/python3
Now when I double-clicked on the file, the dateparser error is gone, but it is complaining that it is not able to find the credentials.json file which is stored in the same directory as the python file.
So, here are my questions:
- How do I find all different instances of python on my mac and how do I have only one version of python3?
- How do run the above command file from the learning_python directory so that the credentials.json file would be found
- What is the problem with my launchd config? How do I know whether it is running or not?
Any pointers would be much appreciated as I have tried my best to read up various resources and make progress on my own, but got stuck.