Python script works locally but throws ModuleNotFoundError in production, what's going on?

Asked by Rachel N. · · Viewed 122 times

8

Runs perfectly fine on my machine but the exact same script throws ModuleNotFoundError the second it's deployed to our server. We're using a virtualenv both places so I'm confused what's different.

pythondeploymentdebugging

3 Answers

  • 7

    ✓ Accepted answer

    Almost always a mismatch between which python is actually running the script versus the one your venv was built with. If cron or systemd is kicking it off, there's a good chance it's using the system python, not your venv's interpreter. Worth running 'which python' inside the actual process that's failing, not just in your terminal, because those can be different environments entirely.

    Afrixh TeamTeam

  • 3

    Classic cron gotcha honestly, cron doesn't source your shell profile so it was quietly running system python for us instead of the venv one. Had to hardcode the full venv python path directly in the cron entry.

    Tariq M.

  • 1

    Also double check your requirements.txt actually matches what's installed locally. 'Works on my machine' usually means someone pip installed something manually and forgot to add it to the file.

    Sophie C.

Your answer