`sys.path` question: I am adding a new backend to ...
# development
f
sys.path
question: I am adding a new backend to Pants called
src/python/pants/backend/observability/opentelemetry
. I am seeing this backend shadow the actual third-party
opentelemetry
package though because the parent directory
src/python/pants/backend/observability
is in the
sys.path
when running tests and so an absolute import (
from opentelemetry import trace
) fails because of the shadowing. This parent directory is not a source root, is this expected behavior?
It looks like the source roots are resolved as
prepared_sources.source_roots=('.', 'src/python')
c
(This is all surprising me, and glancing at
src/python/pants/backend/
I'm double surprised we didn't hit it before.)
h
I don't think that is expected or desired, but I guess it's because that source root is just a bad choice for this test
f
My question is really understanding how
.
can even be a valid source root?
h
Remove it and see what breaks?
f
I didn't add any source roots though; there's nothing to remove which isn't already in
pants.toml
.
The source roots I pasted in above are from dumping the value in the relevant rule code (and the
sys.path
in use in the test file is from dumping the value there).
I investigated more. The root
conftest.py
in the repository has its source root resolved as
.
and not
/
.
Resolving the source root of files in the repository root as
.
seems wrong. It would make more sense for them to be resolved as
/
. (Whether this will fix the shadowing problem is its own issue.)
And the source root is a red herring. The parent directory is being added by pytest automatically and then force imported. https://docs.pytest.org/en/stable/explanation/pythonpath.html
And there is no way to disable the force import with the pytest
—import-mode
flag.
Basically, pytest force imports “opentelemetry” as the parent directory in this case and then any future import from
opentelemetry
will come from the cached import.
And it did that because I did not have an
__init__.py
in the parent directory. Adding that changed the auto-import behavior. Ugh.
h
Python is the woooooooorst!