https://pantsbuild.org/ logo
#general
Title
# general
a

abundant-hospital-56388

06/14/2022, 4:20 PM
Hey, With Python, I seem to be having a problem with importing trio when run from the
./pants run
command as well as in
./pants repl
. I'm running it on my local Ubuntu 22.04 machine where apparently the
sys.excepthook
is replaced by some
apport_excepthook
. Trio tries to deal with that, but needs to import
apport_python_hook
which seems to not exist in the pants environment. I'm wondering how the environment that the
./pants run
and
./pants repl
command are working in is set up. It seems to be somewhere between venv and running the system python natively as follows: • it is not able to import
apport_python_hook
which is available when running python3.10 on my system directly, but not in a venv. • when checking
sys.excepthook.__name__
that is set to
apport_excepthook
, same as when running python directly on the system, but not in venv.
1
Here are some easy steps to reproduce: • On Ubuntu 22.04 • run
python3.10
and then in there call
Copy code
import sys
print(sys.excepthook.__name__)
import apport_python_hook
• Create a venv from python3.10 and run the same things in there • Create the following pants.toml and pants file and run
./pants repl
and the same things in there
Copy code
[GLOBAL]
pants_version = "2.11.0"
backend_packages = [
    "pants.backend.python",
]

[python]
interpreter_constraints = ["CPython>=3.10,<4"]
The results should be as described above. This seems to be a bit inconsistent, as I assume
apport_python_hook
should be available for import if
sys.excepthook
is set to use
apport_excepthook
.
👀 1
h

happy-kitchen-89482

06/14/2022, 4:59 PM
How was Python 3.10 installed on this ubuntu box?
If I look in a vanilla Ubuntu docker container I get
excepthook
as the
sys.excepthook.__name__
and
apport_python_hook
is not available
So I wonder how
apport
was installed
w

witty-crayon-22786

06/14/2022, 6:38 PM
more generally: if you want to constrain which python interpreters pants will use (to include or exclude a system interpreter, or guarantee a particular interpreter), see https://blog.pantsbuild.org/choosing-a-python-interpreter-for-a-pants-project/
in this case, i’d probably suggest avoiding any system-installed interpreter which was going to act differently than a production interpreter might.
(…by removing it from the interpreter_search_path)
h

happy-kitchen-89482

06/14/2022, 9:01 PM
Looks like that hook gets installed with
apt install python3-apport
a

abundant-hospital-56388

06/14/2022, 9:29 PM
Thanks a lot for looking into it for me! I'll investigate it further tomorrow and let you know about where that leads me 👍
h

happy-kitchen-89482

06/14/2022, 10:00 PM
pex aggressively scrubs the sys.path of everything that isn't python standard library, to ensure hermeticity, so I assume that's what's happening here.
a

abundant-hospital-56388

06/15/2022, 9:31 AM
Ok, first to answer the question, which python version I'm using: It's the built-in Python3 version under
/usr/bin
. I also see now, why in the docker container this is not the case. Apparently python3-apport is installed as a dependency of ubuntu-desktop or ubuntu-desktop-minimal as both will be removed when I run
apt remove python3-apport
. The docker container however does not come with a desktop environment so it's probably not installed there. I actually really like to be able to use the system Python3 on Ubuntu and I don't want all devs to have to install an additional interpreter when that one is actually working fine for all other purposes. Also uninstalling
python3-apport
is not really an option. As a workaround one can add
Copy code
import sys
sys.excepthook = sys.__excepthook__
before any other imports in the binaries, but we have quite a few so that's a bit tedious. @happy-kitchen-89482 do you think it might then be an option to address this with pex as it seems to not be aggressive enough in this case and should probably also reset the
sys.excepthook
to
sys.__excepthook__
🤔
What also helps for me is adding
execution_mode = "venv"
to the
pex_binary
definition when running the pex_binary directly through
./pants run
. However, this seems not to help when first packaging the binary and then executing that as
./app.pex
.
h

happy-kitchen-89482

06/15/2022, 4:31 PM
I see, so you're OK with either using apport_python_hook, or properly not using it, but right now it thinks it's available when it isn't?
a

abundant-hospital-56388

06/17/2022, 8:24 AM
Sorry for the late reply, but yeah, exactly
h

happy-kitchen-89482

06/18/2022, 6:23 PM
@abundant-hospital-56388 Got it. Can you open an issue for this at https://github.com/pantsbuild/pants ?
a

abundant-hospital-56388

06/19/2022, 1:44 PM
🙏 1