Closed thejcannon closed 1 year ago
@thejcannon, your example shell session is useful and is also true all the way back to Python 2.7 - almost:
$ cat foo.py
import sys; print("\n".join(sys.path))
$ python2.7 foo.py
/home/jsirois/dev/pantsbuild/jsirois-pex
/usr/lib/python2.7
/usr/lib/python2.7/plat-x86_64-linux-gnu
/usr/lib/python2.7/lib-tk
/usr/lib/python2.7/lib-old
/usr/lib/python2.7/lib-dynload
/usr/local/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages
$ python2.7 -m foo
/usr/lib/python2.7
/usr/lib/python2.7/plat-x86_64-linux-gnu
/usr/lib/python2.7/lib-tk
/usr/lib/python2.7/lib-old
/usr/lib/python2.7/lib-dynload
/usr/local/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages
$ python2.7 -c "$(cat foo.py)"
/usr/lib/python2.7
/usr/lib/python2.7/plat-x86_64-linux-gnu
/usr/lib/python2.7/lib-tk
/usr/lib/python2.7/lib-old
/usr/lib/python2.7/lib-dynload
/usr/local/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages
In other words, all three of those modes insert CWD at the head of sys.path with sometimes CWD spelled as an absolute path and sometimes as ""; so when PEX acts as an interpreter (via either forcing with PEX_INTERPRETER=1 or else when it has no entry point defined), it should do the same. Currently zipapp mode does this correctly just here: https://github.com/pantsbuild/pex/blob/67fc7e05315fc347bcf269712814f160c6035efe/pex/pex.py#L627-L631
But venv mode does it unconditionally as you highlight here: https://github.com/pantsbuild/pex/blob/67fc7e05315fc347bcf269712814f160c6035efe/pex/venv/pex.py#L443-L444
https://github.com/pantsbuild/pex/pull/1832#discussion_r1025363133 captures the location of the bug.
sys.path
behavior is documented https://docs.python.org/3/library/sys.html#sys.path which shows the prepending tosys.path
is dependent on howpython
is invoked.And to further prove the point: