2

CPython 3.x and PyPy follow PEP 3147 and store pyc-files in __pycache__ directories with a Python version suffix attached to them. How can I force CPython 2.7 to store its pyc-files in __pycache__ directories as well (opposed to its default behavior of storing pyc-files without suffix right next to the corresponding py-files in the same directory)?

I realize that I should probably not do this system-wide because a lot of other software expects CPython 2.x's default behavior. So what I am looking for is a way to do this per project or per virtual environment or, if there is no other solution, per custom compiled and custom installed (e.g. in ~/bin) CPython interpreter.


Some research: Relevant discussion on Debian mailing list; code review for PEP 3147 implementation in CPython 3.2.

4

1 回答 1

3

Though the backports section of PEP 3147 alludes to the possibility of it being back ported:

For versions of Python earlier than 3.2 (and possibly 2.7), it is possible to backport this PEP. However, in Python 3.2 (and possibly 2.7), this behavior will be turned on by default, and in fact, it will replace the old behavior.

This was not done as far as I know. The discussion here indicates why:

I would prefer that this not be in 2.7. The patch may be simple to port, but it represents quite a change in an eons old Python behvior.

That's the reply from the release manager for Python 2.7, he (correctly imho) didn't want this feature back ported.

In the end, your only option is implementing it yourself (which is a non-trivial task).

于 2017-05-03T12:03:18.570 回答