Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Python ignore .pyc files

Tags:

Is there a way to make Python ignore any .pyc files that are present and always interpret all the code (including imported modules) directly? Google hasn't turned up any answers, so I suspect not, but it seemed worth asking just in case.

(Why do I want to do this? I have a large pipeline of Python scripts which are run repeatedly over a cluster of a couple hundred computers. The Python scripts themselves live on a shared NFS filesystem. Somehow, rarely, after having been run hundreds of times over several hours, they will suddenly start crashing with an error about not being able to import a module. Forcing the regeneration of the .pyc file fixes the problem. I want, of course, to fix the underlying causes, but in the meantime we also need the system to continue running, so it seems like ignoring the .pyc files if possible would be a reasonable workaround).

P.S. I'm using Python 2.5, so I can't use -B.

like image 450
Ryan Gabbard Avatar asked Aug 17 '10 15:08

Ryan Gabbard


People also ask

Is it possible to decompile a compiled .PYC file into a .py file?

Yes, it is possible.

Does Python need PYC to run?

pyc file is not compiled in the strictest sense of the term, as it is not native machine code. It is, as @tomdachi wrote, python-specific byte-code (very similar to a Java . class), that's why it still needs the python interpreter to execture. in fact, the pyc only was generated when you import the py file.


2 Answers

You could use the standard Python library's imp module to reimplement __builtins__.__import__, which is the hook function called by import and from statement. In particular, the imp.load_module function can be used to load a .py even when the corresponding .pyc is present. Be sure to study carefully all the docs in the page I've pointed to, plus those for import, as it's kind of a delicate job. The docs themselves suggest using import hooks instead (per PEP 302) but for this particular task I suspect that would be even harder.

BTW, likely causes for your observed problems include race conditions between different computers trying to write .pyc files at the same time -- NFS locking is notoriously flaky and has always been;-). As long as every Python compiler you're using is at the same version (if not, you're in big trouble anyway;-), I'd rather precompile all of those .py files into .pyc and make their directories read-only; the latter seems the simplest approach anyway (rather than hacking __import__), even if for some reason you can't precompile.

like image 52
Alex Martelli Avatar answered Dec 05 '22 00:12

Alex Martelli


It's not exactly what you asked for, but would removing the existing .pyc files and then not creating any more work for you? In that case, you could use the -B option:

>python --help usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ... Options and arguments (and corresponding environment variables): -B     : don't write .py[co] files on import; also PYTHONDONTWRITEBYTECODE=x 
like image 36
Blair Conrad Avatar answered Dec 04 '22 22:12

Blair Conrad