echoing-farmer-15630
07/02/2021, 4:26 PMa
(dir structure a/ns/a...
where this ns
contains __init_.py
)_ as well as b and c (which look like b/ns/b..
and c/ns/c...
). root directories a,b,c have SOURCE_ROOT set and a BUILD for python_requirements
, and all three ns/X
directories have a BUILD for python_library
. I will say that b/ns
and c/ns
don't have a BUILD because there are no source files there (including an __init__py
because I didn't want it to conflict with a
).
Okay. AAaaaanyway, I have a few python_test
targets that use b
or c
, and they seem to run OK (the ./pants dependencies
shows b/ns/b/__init__.py
and relevant files, and the tests run).
But I have one irritating test that isn't playing nice: ./pants dependencies
shows b/ns/b/_init_.py
and all files that I think I'm importing, but running the test fails with the equivalent of
module/tests/test_file.py:2: in <module>
from ns.b.submodule.submodule import Thing
E: ModuleNotFoundError: No module named `ns.b`
So I don't quite understand what's gone wrong. I've stared at comparable BUILD files to try and understand what's different. Is there a way to force rebuilding of the test pex just to make sure there's not a caching issue? Or something else I should look at?echoing-farmer-15630
07/02/2021, 4:33 PMpants repl
into the working target which shows ns.b
as importable (and ns
as importable). But in the failing test target, ns.b
isn't importable, but import ns
shows some elements like __version__
of the base package a
, which isn't listed anywhere in the dependencies!enough-analyst-54434
07/02/2021, 4:35 PM__init__.py
files / do they have namespace package declarations in them and nothing else?enough-analyst-54434
07/02/2021, 4:35 PM__init__.py
files to get proper implicit namespace packages.enough-analyst-54434
07/02/2021, 4:36 PMenough-analyst-54434
07/02/2021, 4:37 PMechoing-farmer-15630
07/02/2021, 4:40 PM__init__.py
but no others could or it would be overwritten, but what may well be the case is that no packages in the namespace can have an __init__.py
in the main namespace. Let me try that, because that indeed is where the collision seems to be happening (looking at the failed test, it's bringing in something in the main package, where the passing test isn't).echoing-farmer-15630
07/02/2021, 4:43 PM__init__.py
and the test passed. Some refactoring required.
Thanks; not a pants issue at all; I just misunderstood __init__.py
and namespaces.enough-analyst-54434
07/02/2021, 4:43 PM__init__.py
containing one of these:
+ https://packaging.python.org/guides/packaging-namespace-packages/#pkgutil-style-namespace-packages
+ https://packaging.python.org/guides/packaging-namespace-packages/#pkg-resources-style-namespace-packages
Or else use implicit namespace packages (aka delete your __init__.py
files.enough-analyst-54434
07/02/2021, 4:43 PM