pex-tool / pex

A tool for generating .pex (Python EXecutable) files, lock files and venvs.
https://docs.pex-tool.org/
Apache License 2.0
2.52k stars 258 forks source link

"run from source" instructions dump into repl #2322

Closed cburroughs closed 7 months ago

cburroughs commented 8 months ago

From the README

To run Pex from source, rather than through what is on your PATH, invoke via Python:

.. code-block::

    $ python -m pex
$ cd /tmp
$ git clone git@github.com:pantsbuild/pex.git
Cloning into 'pex'...
remote: Enumerating objects: 14640, done.
remote: Counting objects: 100% (14640/14640), done.
remote: Compressing objects: 100% (3963/3963), done.
remote: Total 14640 (delta 9831), reused 14285 (delta 9765), pack-reused 0
Receiving objects: 100% (14640/14640), 9.60 MiB | 6.98 MiB/s, done.
Resolving deltas: 100% (9831/9831), done.
ecsb@le76 /tmp $ cd pex
$ python3.10 -m pex
Python 3.10.13 (main, Jan  5 2024, 22:23:28) [GCC 13.2.1 20230826] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> 

-m pex.cli appears to work. Not sure what is currently intended.

jsirois commented 8 months ago

@cburroughs, pex will also drop you in a repl. That's how Pex works.

jsirois commented 7 months ago

Perhaps this session using packaged Pex explains the behavior best:

$ python -mvenv example.venv
$ source example.venv/bin/activate
(example.venv) $ pip -q install -U pip
(example.venv) $ pip -q install -U ansicolors==1.1.7 cowsay==5.0 pex
(example.venv) $ python -c 'import colors; print(colors.__file__)'
/home/jsirois/example.venv/lib/python3.10/site-packages/colors/__init__.py
(example.venv) $ python -c 'import cowsay; print(cowsay.__file__)'
/home/jsirois/example.venv/lib/python3.10/site-packages/cowsay/__init__.py
(example.venv) $ cowsay --version
5.0
(example.venv) $ pex ansicolors cowsay
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import colors; print(colors.__file__)
/home/jsirois/.pex/installed_wheels/00d2dde5a675579325902536738dd27e4fac1fd68f773fe36c21044eb559e187/ansicolors-1.1.8-py2.py3-none-any.whl/colors/__init__.py
>>> import cowsay; print(cowsay.__file__)
/home/jsirois/.pex/installed_wheels/274b1e6fc1b966d53976333eb90ac94cb07a450a700b455af9fbdf882244b30a/cowsay-6.1-py3-none-any.whl/cowsay/__init__.py
>>>
now exiting InteractiveConsole...
(example.venv) $ pex ansicolors
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import colors; print(colors.__file__)
/home/jsirois/.pex/installed_wheels/00d2dde5a675579325902536738dd27e4fac1fd68f773fe36c21044eb559e187/ansicolors-1.1.8-py2.py3-none-any.whl/colors/__init__.py
>>> import cowsay; print(cowsay.__file__)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ModuleNotFoundError: No module named 'cowsay'
>>>
now exiting InteractiveConsole...
(example.venv) $ pex
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import colors; print(colors.__file__)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ModuleNotFoundError: No module named 'colors'
>>> import cowsay; print(cowsay.__file__)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ModuleNotFoundError: No module named 'cowsay'
>>>
now exiting InteractiveConsole...
(example.venv) $

In other words, by default (with no -o / --output, etc.) Pex resolves the requirements you request and drops you into a hermetic environment REPL with just access to those resolved requirements. You cannot access non-stdlib modules visible to the underlying interpreter the (ephemeral in this case) PEX is running under. So, if you supply 0 arguments (and thus 0 requirements), Pex drops you into a hermetic REPL exposing only the underlying Python's stdlib.

Does that make sense @cburroughs?

The behavior also caught me off guard back in 2010 and Wickman had to explain it to me. Now the behavior seems obvious, but I'm far removed from that Pex newb at this point.

jsirois commented 7 months ago

@cburroughs I'll close this as an answered question unless you want to keep it open to change the run from source instructions.