
My environment
This is my environment -- hardware, OS and software. + Raspberry Pi 3B + DietPi OS + Python 3.14.0 + Python scripts running via cron
I get this ValueError when my Python program runs
I have a Python program that runs on a Raspberry Pi 3B via cron.
Recently, it started throwing errors similar to this:
Look at the last few lines, for reference:
.....
from _py_warnings import (
File "", line 1371, in _find_and_load
File "", line 1342, in _find_and_load_unlocked
File "", line 938, in _load_unlocked
File "", line 758, in exec_module
File "", line 891, in get_code
File "", line 514, in _compile_bytecode
ValueError: bad marshal data (invalid reference)
This happened for every Python program as well as those that ran via cron.
Solution: Search and delete .pyc files
Why does it happen?
The error message "ValueError: bad marshal data (invalid reference)" shows up when there is a problem with the compiled Python files (.pyc) in your environment.
This usually happens if the .pyc files are corrupted or if there is a mismatch between the Python version and the compiled files.
In my case, I suspect the corruption accelerated because of several REST calls my Python script makes everyday. I guess a better error handling with data obtained from remote calls may prevent this in future. I will test that theory and update this blog post.
Search and list .pyc files
First, search and list the .pyc files.
$ find . -name '*.pyc'
You will see a list of files ending in .pyc
Delete the .pyc files
Next, run the above command with -delete.
find . -name '*.pyc' -delete
That command will delete all the .pyc files in the current directory and subsequent nested directories.
After that is over, run the script, and it should run without any problem.
Conclusion
Hope this solution worked for you and you did not get the ValueError again. If it did or did not, please feel free to comment below. Thanks for reading.
Related Posts
If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.