Changing the directory where .pyc files are created
Solution 1
There's no way to change where the .pyc files go. Python 3.2 implements the __pycache__
scheme whereby all the .pyc files go into a directory named __pycache__
. Python 3.2 alpha 1 is available now if you really need to keep your directories clean.
Until 3.2 is released, configure as many tools as you can to ignore the .pyc files.
Solution 2
Yes, starting from Python 3.8 you can control this behavior. The original discussion starts from pep 304 in 2003.
While this original PEP was withdrawn, a variant of this feature was eventually implemented for Python 3.8 in https://bugs.python.org/issue33499
In the result, you can control using PYTHONPYCACHEPREFIX=path
, -X pycache_prefix=path
and sys.pycache_prefix
.
Solution 3
This might be useful for some:
Miscellaneous options, especially -B
option:
If given, Python won’t try to write .pyc files on the import of source modules. See also
PYTHONDONTWRITEBYTECODE
.
Comments
-
Scharron almost 2 years
Is there a way to change the directory where
.pyc
file are created by the Python interpreter? I saw two PEPs about that subject (0304 and 3147), but none seems to be implemented in the default interpreter (I'm working with Python 3).Did I miss something ?
-
yantrab over 13 yearsCan you tell us more about why you want to move them?
-
Scharron over 13 yearsIt was just out of curiosity, to keep directories clean.
-
mattgately over 6 yearsI was wondering the same thing. I have a case where the directories with the source files are not writable, and want to relocate the pycache so that all users can benefit from the cache without having write access to the actual python source file directories.
-
-
Aniket Thakur over 8 yearsYour custom .py files get compiled and stored in .pyc file under
__pycache__
folder in same directory. You will find common compiled files underC:\Python34\Lib\__pycache__
. -
bers over 3 yearsThis answer is outdated, check stackoverflow.com/a/60024195/880783
-
xcodz-dot almost 3 yearsfor linux users who do not care what happens with the pycache, this will work very well when added to .bashrc:
export PYTHONPYCACHEPREFIX=/tmp
. The cache files will be sent/tmp
and will be cleared after 10 days or on system reboot.