Search code examples
pythonpython-2.7pyc

Is it possible and feasible to compile the entire Python standard library to byte code?


Is there an easy way to pre-"compile" the entire Python standard library to byte code? If so, would this also guarantee that Python never re-"compiles" a file if a corresponding .pyc file already exists (as long as the standard library is not updated or modified)?

Background: On an application server, Python sometimes spontaneously goes into a broken state that manifests itself like this:

C:\> python
Traceback (most recent call last):
  File "C:\Python27\lib\site.py", line 563, in <module>
    main()
  File "C:\Python27\lib\site.py", line 552, in main
    aliasmbcs()
  File "C:\Python27\lib\site.py", line 478, in aliasmbcs
    enc = locale.getdefaultlocale()[1]
AttributeError: 'module' object has no attribute 'getdefaultlocale'

and that can be resolved by re-installing Python. Someone else told me that he has seen corrupted (0-byte) .pyc files and that deleting those solved the problem. However, I cannot reproduce the issue by deliberately creating 0-byte .pyc files.

I don't like the fact that files belonging to the standard library are "compiled" to byte code on demand, as this happens within a process running both Python and native code that can crash unexpectedly. I would prefer to have all Python code pre-compiled, whether or not it helps to resolve the above situation.


Solution

  • Make the system .pyc files read-only, even for administrator. This should prevent Python from modyfing them unnecessarily.

    If You're on Windows go to properties → security → edit, and deny all privileges for all groups.