Python’s .pyc files created readonly if .py file is readonly

But first, let me start by saying that this behavior is like trying to put toothpaste back in its tube after you already squeezed it out. It just doesn’t work!

So why does Python do this? Well, according to some guy named Amaury Forgeot d’Arc (who sounds like a character from a French novel), on Windows platforms if the .py file is set to read-only and you try to import it using `python -c “import foo”`, guess what happens? Yep, Python creates a .pyc file with the same read-only attribute.

This can be quite frustrating for those of us who use source control tools like Perforce that automatically set files to read-only when they’re checked out (which is pretty much everyone). When you try to run your code and it fails because Python won’t overwrite the .pyc file, you might start questioning whether you should switch to a different language altogether.

No worries, though! There are ways around this issue. For example, you can simply set the read-only attribute on the original .py file back to writable before running your code. Or, if you’re feeling adventurous, you could try modifying Python itself (which is like trying to fix a car engine with duct tape and bubble gum).

But let’s be real here this behavior is not going away anytime soon. In fact, according to some guy named Éric Araujo who sounds like he might have been in a band once, the current state of Python 2.6, 2.7, and 3.2 interpreters on Windows platforms means that many users cannot even think about upgrading from Python 2.5 because source control tools will set all .py files to read-only every time you run Python.

So if you’re feeling frustrated or angry (which is totally understandable), just remember at least you’re not alone in this struggle. And who knows, maybe someday we’ll look back on these dark days of Python and laugh about how ridiculous it all was. Until then, keep coding!

SICORPS