Concept
1. Before scheduling: make the script schedule-ready
A scheduled job runs with no one watching. So the script must:
- use full paths or start from a known folder (it won't start in your project folder automatically),
- run without questions (no
input(), no pop-ups), - log what happened to a file,
- exit with an error code when something fails (Python does this automatically on an unhandled exception).
Test the exact command in a fresh terminal first. If it doesn't work there, it won't work on a schedule.
2. Find your Python path
Use the Python inside your virtual environment, so the right libraries are there:
| OS | Path looks like |
|---|---|
| Windows | C:\Reports\monthly-report\.venv\Scripts\python.exe |
| Mac/Linux | /home/divyesh/monthly-report/.venv/bin/python |
3. Windows: a batch file (recommended)
Create run_report.bat in the project folder:
@echo off
cd /d C:\Reports\monthly-report
if not exist logs mkdir logs
".venv\Scripts\python.exe" monthly_report.py --input data\current --output reports\Sales_Report.xlsx >> logs\run.log 2>&1
cd /dmoves to the project folder (and drive), so relative paths work.>> logs\run.log 2>&1appends normal output and errors to a log file.
Double-click it once to test, then check logs\run.log.
4. Windows Task Scheduler (clicks)
- Start menu → Task Scheduler → Create Task… (not "Basic", so you see all options).
- General: Name
Daily Sales Report. Choose Run whether user is logged on or not if the PC may be locked. Tick Run with highest privileges only if needed. - Triggers → New: Daily, 9:00 AM. For Monday–Saturday choose Weekly and tick those days.
- Actions → New: Program/script:
C:\Reports\monthly-report\run_report.bat. Start in:C:\Reports\monthly-report. - Conditions: untick "Start the task only if the computer is on AC power" for laptops. Tick Wake the computer to run this task if you like.
- Settings: tick Run task as soon as possible after a scheduled start is missed (catches up if the PC was off at 9).
- OK → enter your Windows password.
Test: right-click the task → Run. Check Last Run Result: 0x0 means success.
5. Windows: the same from the command line
schtasks /Create /TN "Daily Sales Report" /TR "C:\Reports\monthly-report\run_report.bat" /SC DAILY /ST 09:00
schtasks /Run /TN "Daily Sales Report"
schtasks /Query /TN "Daily Sales Report" /V /FO LIST
6. Mac / Linux: cron
Open your schedule with crontab -e and add one line:
0 9 * * 1-6 cd /home/divyesh/monthly-report && .venv/bin/python monthly_report.py --input data/current --output reports/Sales_Report.xlsx >> logs/run.log 2>&1
The five time fields:
| Field | Value | Meaning |
|---|---|---|
| minute | 0 | at minute 0 |
| hour | 9 | 9 AM |
| day of month | * | every day |
| month | * | every month |
| day of week | 1-6 | Monday to Saturday (0 = Sunday) |
More examples: 30 18 * * * (6:30 PM daily), 0 9 1 * * (9 AM on the 1st of each month), */15 * * * * (every 15 minutes). Check yours at crontab.guru.
crontab -l lists your jobs. cron uses the computer's time zone — check it with date.
On a Mac, cron may need Full Disk Access (System Settings → Privacy & Security) to read folders like Documents or Desktop. macOS also has launchd, the native scheduler, but cron is simpler to start with.
7. What about VBA and xlwings?
- pandas/openpyxl scripts run fine in the background.
- Anything that opens Excel (xlwings, VBA via a script) needs a logged-in desktop session. "Run whether user is logged on or not" usually fails for these — use "Run only when user is logged on" and keep the user logged in, or move the job to pandas + openpyxl.
- A VBA macro can be scheduled by opening a workbook whose
Workbook_Openruns the macro and then closes Excel — workable, but fragile. Prefer Python for unattended jobs.
8. Troubleshooting checklist
| Symptom | Likely cause |
|---|---|
| Works by hand, not on schedule | relative paths / missing "Start in" / wrong Python |
ModuleNotFoundError |
system Python used instead of the venv Python |
| Nothing in the log | output not redirected (>> log 2>&1 missing) |
| Didn't run at all | PC asleep/off; task disabled; password changed |
| PermissionError writing xlsx | the report file was open in Excel |
Common mistakes
Skipping the manual test of the exact command. Using python instead of the full venv path. No log file, so failures are invisible.