20250101 ‐ python path - cywongg/2025 GitHub Wiki

Safari

The error you’ve provided indicates that the Python script triggered by the cron job is unable to locate the config_base module. However, this module exists in the directory /wash/auto-cmrm-revamp/config_base.py. When you run the script manually from a Linux session, it works, but when run by crontab, it fails. This is a classic case of environment differences between your interactive shell and the cron environment.

Let’s address this specific issue and modify the solution accordingly.


Why Is This Happening?

  1. Environment Variables Are Different in Cron:

    • Cron jobs run in a minimal environment that does not inherit the same environment variables (like PYTHONPATH) as your interactive session. This is why the script cannot find config_base when triggered by the cron job—it does not know where to look for the module.
  2. Working Directory Is Not Set Properly:

    • Cron jobs execute from the user's home directory (~) by default, not the directory where the script resides. This means relative imports or paths in the script (e.g., from config_base import load_config) will fail if Python is not explicitly told where to look.
  3. Python Version or Path Differences:

    • If you are relying on a specific Python version or virtual environment and it’s not explicitly set in the cron job, cron might use the system's default Python version. This can lead to issues if the required version or packages are not available.

How to Fix the Issue?

1. Set the PYTHONPATH in the Cron Job

Add the directory /wash/auto-cmrm-revamp to the PYTHONPATH so Python can find the config_base.py module. You can set this directly in the cron job:

0 20 * * 1-5 PYTHONPATH=/home/waadev01/wash/auto-cmrm-revamp ~/wash/auto-cmrm-revamp/close_ticket.sh >> ~/wash/log/autoCMRM.log 2>&1

Alternatively, you can export PYTHONPATH in the script itself (if you have control over it):

#!/bin/bash
export PYTHONPATH=/home/waadev01/wash/auto-cmrm-revamp
python3 /home/waadev01/wash/auto-cmrm-revamp/close_ticket.py

2. Set the Working Directory in the Script

Ensure the script runs from the correct directory by explicitly changing the working directory in the cron job or script.

For example, modify the cron job to explicitly change to the correct directory before running the script:

0 20 * * 1-5 cd ~/wash/auto-cmrm-revamp && ./close_ticket.sh >> ~/wash/log/autoCMRM.log 2>&1

Alternatively, add a cd command to the shell script close_ticket.sh:

#!/bin/bash
cd /home/waadev01/wash/auto-cmrm-revamp
python3 close_ticket.py

This ensures that the script runs from the correct directory and can find config_base.py directly.

3. Use Absolute Imports in Python Script

If the Python script relies on relative imports like from config_base import load_config, ensure that it can handle absolute imports as well. Modify secondary.py to include the full module path:

import sys
sys.path.append('/home/waadev01/wash/auto-cmrm-revamp')

from config_base import load_config

This explicitly tells Python to search for modules in /home/waadev01/wash/auto-cmrm-revamp.

4. Specify the Full Python Path in Cron

Ensure the cron job uses the correct Python version and includes the full path to the Python binary. Find the full path of the Python version you want to use by running:

which python3

For example, if the result is /usr/bin/python3, modify the cron job like this:

0 20 * * 1-5 /usr/bin/python3 ~/wash/auto-cmrm-revamp/close_ticket.py >> ~/wash/log/autoCMRM.log 2>&1

5. Debugging Cron Jobs

If you’re still running into issues, redirect the cron job output (both stdout and stderr) to a log file for debugging:

0 20 * * 1-5 cd ~/wash/auto-cmrm-revamp && ./close_ticket.sh >> ~/wash/log/autoCMRM.log 2>&1

Check the contents of ~/wash/log/autoCMRM.log for further clues.


Final Example of close_ticket.sh

Here’s how your close_ticket.sh might look after incorporating the fixes:

#!/bin/bash
# Set the working directory
cd /home/waadev01/wash/auto-cmrm-revamp

# Set PYTHONPATH
export PYTHONPATH=/home/waadev01/wash/auto-cmrm-revamp

# Run the Python script
/usr/bin/python3 close_ticket.py

Key Takeaways

  • Set PYTHONPATH: Ensure the Python process knows where to find custom modules.
  • Set the Working Directory: Use cd in the script or cron job to ensure the script runs in the correct directory.
  • Use Absolute Imports: Modify the Python script to use absolute imports or dynamically add the correct path to sys.path.
  • Use the Correct Python Version: Specify the full path to the desired Python binary in the cron job.

By following these steps, the ModuleNotFoundError for config_base should be resolved when the script is triggered by cron.