silly-queen-7197
09/08/2023, 7:20 PMimport __pex__
. I have a minimal setup via (while this question is about pex, I'm interested in being able to consume `pex`s generated by pants. This just seemed like a more simplified way to reframe things)
python3 -m venv venv
source venv/bin/activate
python -m pip install pex
python -m pip install ipython
python -m pex 'flask' -o flask.pex
python -m IPython
In [1]: import sys
In [2]: sys.path.append('flask.pex')
In [3]: import __pex__
but get
ModuleNotFoundError: No module named 'prompt_toolkit'
Error in sys.excepthook:
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
File "<frozen importlib._bootstrap>", line 1126, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
ModuleNotFoundError: No module named 'IPython'
I have pex==2.1.145
installedsilly-queen-7197
09/08/2023, 7:25 PMpython -m pex flask ipython -o flask.pex
silly-queen-7197
09/08/2023, 7:27 PMipython
seems to get stucksilly-queen-7197
09/08/2023, 7:35 PMModuleNotFoundError: No module named 'zmq'
when trying to run the above commands in jupytersilly-queen-7197
09/08/2023, 7:56 PMpython -m pex 'flask' -o flask.pex
and running the following (lets call it main.py
)
import sys
sys.path.append('flask.pex')
import __pex__
from flask import Flask
print(Flask)
works. Commands also work in python
.silly-queen-7197
09/08/2023, 8:01 PMipython
that causes it to crashenough-analyst-54434
09/08/2023, 8:35 PMsilly-queen-7197
09/08/2023, 9:27 PMpython3.10 -m pex flask --venv -o flask.pex
and spinning up python3.10 -m IPython
In [1]: import sys
In [2]: sys.path.append('flask.pex')
In [3]: import __pex__
works. But when I try to import flask
I get ModuleNotFoundError: No module named 'hmac'
silly-queen-7197
09/08/2023, 9:29 PMflask
after import __pex__
works in a python script.silly-queen-7197
09/08/2023, 9:32 PMpython3.10 -m IPython
In [1]: import hmac
In [2]: import secrets
In [3]: import sys
In [4]: sys.path.append('flask.pex')
In [5]: import __pex__
In [6]: import flask
In [7]:
silly-queen-7197
09/08/2023, 9:33 PMIn [1]: import sys
In [2]: sys.path.append('flask.pex')
In [3]: import __pex__
In [4]: import hmac
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Cell In[4], line 1
----> 1 import hmac
ModuleNotFoundError: No module named 'hmac'
silly-queen-7197
09/08/2023, 10:00 PMmain.py
script again but I get a similar error this time. The difference being this one uses a PEX created with --venv
I tried --venv-copies
and --venv-site-packages-copies
and while those worked when running main.py
they don't work in ipython
.enough-analyst-54434
09/09/2023, 4:11 AMsilly-queen-7197
09/09/2023, 8:37 PMpex
that captures dependencies (1st and 3rd party) from our monorepo that folks can consume in jupyter
notebooks.silly-queen-7197
09/09/2023, 8:57 PMvs code
and the embedded jupyter notebook
extension which will install necessary dependencies into a python environment so as we pick a python
version that satisfies our constraints I think we're set there.
I just want to be able to declaratively create `pex`s like
pex_binary(
dependencies=["//:reqs#Flask"],
execution_mode="venv"
)
that will be consumed via notebooks (or one giant pex
, I don't know if that's a better or worse approach)silly-queen-7197
09/09/2023, 9:00 PMimport sys
import os
sys.path.append('../dist/foobar/foobar.pex')
import __pex__
in the jupyter notebook (spawned from some place like ~/.pyenv/versions/3.10.8/bin/python
not a venv
or pex
) I get ModuleNotFoundError: No module named 'secrets'
. Ultimately this is the error I really care about.
Sorry about asking such broad questions! I really appreciate you taking the time to look at.silly-queen-7197
09/09/2023, 9:42 PMpex_binary(
dependencies=["//:reqs#Flask"],
)
works. I think what I'm confused about is I don't know if that's a bug or expected behaviorenough-analyst-54434
09/09/2023, 9:43 PMexecution_mode="venv"
causes failure, but without that bit, you can:
import sys
import os
sys.path.append('../dist/foobar/foobar.pex')
import __pex__
And not get the secrets
error?enough-analyst-54434
09/09/2023, 9:46 PMenough-analyst-54434
09/09/2023, 9:47 PMsilly-queen-7197
09/09/2023, 9:47 PMenough-analyst-54434
09/09/2023, 9:47 PMenough-analyst-54434
09/09/2023, 9:48 PM__pex__
magic hook will involve some fiddly corner case that a repro will be useful for.silly-queen-7197
09/09/2023, 10:21 PMenough-analyst-54434
09/09/2023, 10:22 PMsilly-queen-7197
09/09/2023, 10:26 PMsilly-queen-7197
09/10/2023, 12:05 AMmkdir with-venv
unzip with-venv/with-venv.pex -d with-venv/
To debug, I'm running the following in a notebook with the same setup as described in the README.md
import sys
sys.path.append('./with-venv')
import __pex__
I set a breakpoint at import __pex__
.
I can import secrets
at [pex_bootstrapper.py#L671](https://github.com/pantsbuild/pex/blob/main/pex/pex_bootstrapper.py#L671) however if I restart the notebook and run the debugger again, I can't import secrets at [pex_bootstrapper.py#L673](https://github.com/pantsbuild/pex/blob/main/pex/pex_bootstrapper.py#L673).
If I inspect venv_python
I get /home/yjabri/.pex/venvs/6c8eaec5b07b6fe5d8c257cd72d1297dfafdfc8e/779eb2cc0ca9e2fdd204774cbc41848e4e7c5055/bin/python
. As a sanity check I can run
/home/yjabri/.pex/venvs/6c8eaec5b07b6fe5d8c257cd72d1297dfafdfc8e/779eb2cc0ca9e2fdd204774cbc41848e4e7c5055/bin/python -c "import secrets"
without any exceptions.
The line changes sys.path
from
python
[
"/home/yjabri/Projects/debug-pex-import/with-venv/.bootstrap/pex/vendor/_vendored/attrs",
"/home/yjabri/Projects/debug-pex-import/with-venv/.bootstrap",
"/home/yjabri/Projects/debug-pex-import",
"/home/yjabri/.pyenv/versions/3.10.13/lib/python310.zip",
"/home/yjabri/.pyenv/versions/3.10.13/lib/python3.10",
"/home/yjabri/.pyenv/versions/3.10.13/lib/python3.10/lib-dynload",
"",
"/home/yjabri/.local/lib/python3.10/site-packages",
"/home/yjabri/.pyenv/versions/3.10.13/lib/python3.10/site-packages",
"./with-venv"
]
to
python
[
'/home/yjabri/.pex/venvs/6c8eaec5b07b6fe5d8c257cd72d1297dfafdfc8e/779eb2cc0ca9e2fdd204774cbc41848e4e7c5055/lib/python3.10/site-packages',
'./with-venv'
]
If I create a notebook with
import sys
sys.path = [
'/home/yjabri/.pex/venvs/6c8eaec5b07b6fe5d8c257cd72d1297dfafdfc8e/779eb2cc0ca9e2fdd204774cbc41848e4e7c5055/lib/python3.10/site-packages',
'./with-venv'
]
Then import secrets
throws a ModuleNotFoundError
. However if I modify the path to
import sys
sys.path = [
'/home/yjabri/.pyenv/versions/3.10.13/lib/python3.10',
'/home/yjabri/.pex/venvs/6c8eaec5b07b6fe5d8c257cd72d1297dfafdfc8e/779eb2cc0ca9e2fdd204774cbc41848e4e7c5055/lib/python3.10/site-packages',
'./with-venv'
]
then import secrets
workssilly-queen-7197
09/10/2023, 12:05 AMsilly-queen-7197
09/10/2023, 12:54 AM