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

echoing-farmer-15630

07/02/2021, 4:26 PM
Hmm; I wrote a long description so can add more, but I'm having a curious problem with some tests and namespaced packages (pants 2.50, python 3.9). Bah, some description necessary anyway 🙂 I have three "namespaced modules", call them
a
(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
Copy code
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?
It actually gets a bit stranger; I can
pants 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!
e

enough-analyst-54434

07/02/2021, 4:35 PM
So, why do you have
__init__.py
files / do they have namespace package declarations in them and nothing else?
Under Python 3.9 you can just omit the
__init__.py
files to get proper implicit namespace packages.
Forgive me if you know all of this. Its hard to read sometimes what you may or may not be aware of on the other end of the line.
1
e

echoing-farmer-15630

07/02/2021, 4:40 PM
Of course, and thanks. Atually this may indeed be the issue; I had thought that one package in the namespace could have an
__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).
Yep, that seems to be my error; removed that
__init__.py
and the test passed. Some refactoring required. Thanks; not a pants issue at all; I just misunderstood
__init__.py
and namespaces.
🙌 1
e

enough-analyst-54434

07/02/2021, 4:43 PM
Yeah, namespace packages don't work like you'd think in Python. You either need all of them to have the exact same
__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.
👍 1
Aha - ok - great.
5 Views