Running without Python source files in Python 3.4
No enough reputation to add comment to BrenBarn's answer. So here is some complement.
According to the compileall
doc:
-b
Write the byte-code files to their legacy locations and names, which may overwrite byte-code files created by another version of Python. The default is to write files to their PEP 3147 locations and names, which allows byte- code files from multiple versions of Python to coexist.
So you could run python -m compileall -b .
to compile all the code files in this directory recursively.
According to the PEP:
It's possible that the foo.py file somehow got removed, while leaving the cached pyc file still on the file system. If the
__pycache__/foo.<magic>.pyc
file exists, but the foo.py file used to create it does not, Python will raise an ImportError when asked to import foo. In other words, Python will not import a pyc file from the cache directory unless the source file exists.
But:
In order to continue to support source-less distributions though, if the source file is missing, Python will import a lone pyc file if it lives where the source file would have been.
So it would seem that __pycache__
and sourceless distributions are mutually exclusive. If you want to remove the source, you need to move the .pyc files out into the directory where the source would have been.