pex built by pex doesn't run outside its virtualenv

249 views Asked by At

TL;DR

I built pex according to the guide https://pex.readthedocs.io/en/v2.1.145/buildingpex.html and was expecting it to run fine outside of the virtualenv I used to build it, but alas:

$ ~/bin/pex
env: python3.9: No such file or directory

What I tried

I am reading pex's guide https://pex.readthedocs.io/en/v2.1.145/buildingpex.html and following their first "recipe" to build pex itself

You can build .pex files using the pex utility, which is made available when you pip install pex. Do this within a virtualenv, then you can use pex to bootstrap itself:

  • In an empty directory, I created a fresh venv
$ python3 -m venv venv
  • Activate the venv
$ . venv/bin/activate
  • Insatll pex
(venv) tmp $ pip install pex
Collecting pex
  Downloading pex-2.1.145-py2.py3-none-any.whl (2.9 MB)
     |████████████████████████████████| 2.9 MB 4.1 MB/s
Installing collected packages: pex
Successfully installed pex-2.1.145
  • Build pex
$ pex pex requests -c pex -o ~/bin/pex
  • Try pex interactively
$ pex
Python 3.9.6 (default, May  7 2023, 23:32:44)
[Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> ^D
now exiting InteractiveConsole...
  • The documentation says:

you can use pex in or outside of any virtualenv.

  • Therefore, I deactivate the venv and then try to run pex again:
$ deactivate
$ ~/bin/pex
env: python3.9: No such file or directory

What am I missing?

Additional information

Platform: MacOS Ventura 13.5.1

1

There are 1 answers

0
Marcello Romani On

Thanks to this SO question I learned about the --python-shebang option.

In my case, I first searched the path of the Python 3 interpreter on the system:

$ which python3
/usr/bin/python3

then re-built pex instide the VirtualEnv but specifying the shebang line with the Python 3 path on my system:

$ pex pex requests -c pex -o ~/bin/pex --python-shebang '#!/usr/bin/env python3'

Now I can exit the VirtualEnv and pex still works:

$ deactivate
$ ~/bin/pex
Python 3.9.6 (default, May  7 2023, 23:32:44)
[Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> ^D
now exiting InteractiveConsole...