Is it possible to have Python save the .pyc
files to a separate folder location that is in sys.path
?
/code
foo.py
foo.pyc
bar.py
bar.pyc
To:
/code
foo.py
bar.py
/code_compiled
foo.pyc
bar.pyc
I would like this because I feel it'd be more organized. Thanks for any help you can give me.
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.