Update:
In Python 3.8 -X pycache_prefix=PATH
command-line option enables writing .pyc
files to a parallel tree rooted at the given directory instead of to the code tree. See $PYTHONPYCACHEPREFIX
envvarcredits: @RobertT’ answer
The location of the cache is reported in sys.pycache_prefix
(None
indicates the default location in __pycache__
[since Python 3.2] subdirectories).
To turn off caching the compiled Python bytecode, -B
may be set, then Python won’t try to write .pyc
files on the import of source modules. See $PYTHONDONTWRITEBYTECODE
envvarcredits: @Maleev’s answer
Old [Python 2] answer:
There is PEP 304: Controlling Generation of Bytecode Files. Its status is Withdrawn
and corresponding patch rejected. Therefore there might be no direct way to do it.
If you don’t need source code then you may just delete *.py
files. *.pyc
files can be used as is or packed in an egg.