This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Unexpected effect of sys.pycache_prefix = ""
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.11
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: andrei.avk, christian.heimes
Priority: low Keywords:

Created on 2021-12-04 03:58 by andrei.avk, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (7)
msg407632 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-12-04 03:58
Setting sys.pycache_prefix = "", re-creates CWD structure under current directory, e.g. if run from /Users/foo/test, will create /Users/foo/test/Users/foo/test/myfile.pyc . Is that intentional? It seems a little weird. At least it might be good to document it.
msg407633 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-12-04 03:59
(I forgot to mention this happens on MacOS).
msg407729 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2021-12-05 18:59
It works as designed and documented. An empty string is considered a relative directory.

https://docs.python.org/3/library/sys.html#sys.pycache_prefix


> If this is set (not None), Python will write bytecode-cache .pyc files to (and read them from) a parallel directory tree rooted at this directory...
>
> A relative path is interpreted relative to the current working directory.
msg407763 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-12-06 00:52
If CWD=/a/b and pycache_prefix=c, the resulting dir is /a/b/c as I would expect.

If CWD=/a/b and pycache_prefix='', I would expect resulting dir to be /a/b 
 instead of /a/b/a/b .

For example as the shell `cd` command accepts relative path as arg, I would expect that `cd c` changes to /a/b/c ; but it would be very strange to expect `cd ` with empty arg to change to /a/b/a/b .
msg407764 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-12-06 00:54
Can this also create the risk of 'path too long' issues?
msg407775 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2021-12-06 09:04
The cache prefix mirrors the directory structure. Python takes the absolute path to the .py file, converts the path to a relative path by stripping off the leading slash, and finally joins the path with the prefix.

Take this example:

$ PYTHONPYCACHEPREFIX=/tmp/pycache python3.10 -c pass

This will create the cache file "/tmp/pycache/usr/lib64/python3.10/os.cpython-310.pyc" for the Python file "/usr/lib64/python3.10/os.py.
msg408358 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-12-12 01:43
Thanks, I've misunderstood how it works. It makes sense now. Closing as not a bug.
History
Date User Action Args
2022-04-11 14:59:53adminsetgithub: 90135
2021-12-12 01:43:28andrei.avksetstatus: open -> closed
resolution: not a bug
messages: + msg408358

stage: resolved
2021-12-06 09:04:21christian.heimessetmessages: + msg407775
2021-12-06 00:54:53andrei.avksetmessages: + msg407764
2021-12-06 00:52:45andrei.avksetmessages: + msg407763
2021-12-05 18:59:14christian.heimessetnosy: + christian.heimes
messages: + msg407729
2021-12-04 03:59:05andrei.avksetmessages: + msg407633
2021-12-04 03:58:31andrei.avkcreate