I ran across an uncommon use case after successful...
# general
n
I ran across an uncommon use case after successfully migrating to pantsbuild. This is probably more of a pex question than a pantsbuild question but I figured I'd start here as I can't seem to find a similar platform for pex related questions. Is it possible to use a pex file to execute a script in
-i
interactive mode?
There is a script in a docker container that was previously executed using
/path/to/venv/bin/python -i script.py arg1 arg2
. Now with pantsbuild, the
/path/to/venv
no longer needs to exist because everything else in the container is packaged using pex. I have considered leaving the current venv there as well as possibly using a modification of this recipe to make a new venv but I was wondering if there was some possible trickery with pex that I was overlooking?
a
I have run ./my.pex <myscript> without issue, if the pex has an entrypoint you will need to do
PEX_INTERPRETER=1 ./my.pex <myscript> arg1
e
The key thing to realize is a PEX can act as a Python interpreter. If it has no defined entrypoint, it does this by default. If it does have a defined entrypoint, you can force the behavior with `PEX_INTERPRETER=1`; so, for example, this almost works today:
Copy code
$ pex ansicolors -o ansicolors.pex
$ cat <<EOF > script.py
> import colors
> 
> print(colors.blue("Interactive?"))
> EOF
$ ./ansicolors.pex -i script.py 
Interactive?
Traceback (most recent call last):
  File "/home/jsirois/.pex/unzipped_pexes/28923b82caa398e274ae9593d6da0cfb4344ef1a/__main__.py", line 103, in <module>
    bootstrap_pex(__entry_point__, execute=__execute__, venv_dir=__venv_dir__)
  File "/home/jsirois/.pex/unzipped_pexes/28923b82caa398e274ae9593d6da0cfb4344ef1a/.bootstrap/pex/pex_bootstrapper.py", line 601, in bootstrap_pex
    pex.PEX(entry_point).execute()
  File "/home/jsirois/.pex/unzipped_pexes/28923b82caa398e274ae9593d6da0cfb4344ef1a/.bootstrap/pex/pex.py", line 540, in execute
    sys.exit(self._wrap_coverage(self._wrap_profiling, self._execute))
SystemExit
>>>
Unfortunately, it looks like the PEX_INTERPRETER=1 suppression doesn't quite work for interactive mode:
Copy code
$ pex cowsay --script cowsay -o cowsay.pex --venv prepend
$ ./cowsay.pex 'Moo!'
  ____
| Moo! |
  ====
    \
     \
       ^__^
       (oo)\_______
       (__)\       )\/\
           ||----w |
           ||     ||
$ PEX_INTERPRETER=1 ./cowsay.pex -c 'import cowsay; cowsay.tux("Quack?")'
  ______
| Quack? |
  ======
           \
            \
             \
              .--.
             |o_o |
             |:_/ |
            //   \ \
           (|     | )
          /'\_   _/`\
          \___)=(___/
$ cat <<EOF > script.py
import cowsay

cowsay.tux("It's good for you!")
EOF
$ PEX_INTERPRETER=1 ./cowsay.pex -i script.py 
Re-executing with Python interpreter options: cmdline='/home/jsirois/.pex/venvs/c16958b5b5ab036132bf7669f3d56e9ae1315ac0/5985ed09b49a653d6596b0e14d134c5456cf1a9f/bin/python -i /home/jsirois/.pex/venvs/c16958b5b5ab036132bf7669f3d56e9ae1315ac0/5985ed09b49a653d6596b0e14d134c5456cf1a9f/pex script.py'
  _________
| script.py |
  =========
         \
          \
            ^__^
            (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
Traceback (most recent call last):
  File "/home/jsirois/.pex/venvs/c16958b5b5ab036132bf7669f3d56e9ae1315ac0/5985ed09b49a653d6596b0e14d134c5456cf1a9f/pex", line 245, in <module>
    sys.exit(func())
SystemExit
>>>
@nutritious-minister-3808 so there are a few issues there. If you want Pex to fully support `-i`definitely file an issue.
n
Appreciate the swift response. That is similar to what I ran into when trying it locally. I will make a note to file an issue at some point. Thank you!