Hoping some pants/pex expert might be able to help...
# general
e
Hoping some pants/pex expert might be able to help me here. I have a package that will not install correctly inside a pex. No error messages on pex creation/startup (PEX_VERBOSE=9 looks normal to me). I've been pulling my hair out over this on and off for a few days and I'm out of ideas. I stumbled upon this trying to add pants to an existing python repo, but here is the simplest reproduction:
Copy code
$ python3 -m venv pextest
$ source ./pextest/bin/activate
(pextest) $ pip install pex
Successfully installed pex-2.1.161
(pextest) $ pex azure-core-tracing-opentelemetry
>>> from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ModuleNotFoundError: No module named 'azure.core.tracing.ext.opentelemetry_span'
The same with just pip/venv:
Copy code
$ python3 -m venv piptest
$ source ./piptest/bin/activate
(piptest) $ pip install azure-core-tracing-opentelemetry
Successfully installed azure-core-1.29.7 azure-core-tracing-opentelemetry-1.0.0b11 certifi-2023.11.17 charset-normalizer-3.3.2 deprecated-1.2.14 idna-3.6 importlib-metadata-6.11.0 opentelemetry-api-1.22.0 requests-2.31.0 six-1.16.0 typing-extensions-4.9.0 urllib3-2.2.0 wrapt-1.16.0 zipp-3.17.0
(piptest) $ python -c 'from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan'
(piptest) $ python
Python 3.9.7 (default, Nov  8 2021, 06:01:45) 
[GCC 10.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan
>>>
I think it might be something to do with a namespace conflict between the
azure-core
and
azure-core-tracing-opentelemetry
packages, but I don't see anything special about them. Any ideas or pointers?
g
Can you try
--venv --venv-site-packages-copies
? Malformed namespace packages generally work better with those flags.
🙌 1
Normally
pex
will symlink together and make up a PYTHON_PATH to construct the full venv, but that might mean multiple PYTHON_PATH entries might contain
azure
. If that isn't a proper namespace package the interpreter will not search the later entries, so it becomes a bit random depending on path order etc. Those two args ensure that a full venv is constructed, and that the venv contains the full sources. That should merge the two source trees, which is what'd happen in a regular venv
e
That works!
And
Copy code
pex_binary(
    ...
    execution_mode="venv",
    venv_site_packages_copies=True,
Is working as well
g
Awesome, was about to mention those. I unfortunately think there is no equivalent configuration for
python_test
or
python_source
, which would also use
pex
. But they work slightly differently and are oftentimes more robust.
e
Luckily we don't use tracing in our unit tests
Do you know what about the package might be misconfigured for it to mismatch? Any common prefix or is there some configuration the azure packages should be using to be more explicit? I can try to get a fix in upstream. Or is this really a pex only issue?
g
The issue is really that there's an
azure.core.tracing
in
azure-core
, and both
core
and
tracing
there contain an
__init__.py
. In general, you cannot nest a Python distribution inside another -- we're not even talking about namespace packages at this point. There should probably be an
azure.ext.tracing
with
azure.ext
being a namespace, that'd probably work.
I mean, just thinking about file ownership. Which package does
azure.core.tracing
even refer to? And worse, who owns the files in azure.core.tracing? Etc. I don't think with this setup where the assumption is to merge the filetrees that even a pkgutil-style source-tree would work, and I'm not sure if pex works with non-native namespaces either. https://packaging.python.org/en/latest/guides/packaging-namespace-packages/ has a lot more info about the semantics of namespace packages.
e
Will have to read up a bit more. Unlikely I can convince microsoft to change this now. Oh well.
venv_site_packages_copies
forever
Thanks again for the help!
f
I also encountered this issue when attempting to import
from azure.eventhub.extensions.checkpointstoreblobaio import BlobCheckpointStore
within a PEX. Using the
execution_mode="venv"
and
venv_site_packages_copies=True
arguments to
pex_binary
produces a working PEX. However, I am attempting to “zip-deploy” the PEX as an Azure Function and cannot get it to work (i.e., I must use
execution_mode="zipapp"
which means
venv_site_packages_copies
is ignored). Any suggestions for how I might resolve this issue with the
zipapp
execution mode?