limited-balloon-79828
09/27/2023, 4:31 AMpants test
but it can be found using pants run
ImportError: cannot import name 'module_something' from 'src.common' (unknown location)
There was one time that the test was successful but I changed the code with the same import structure and the above error shows up.
Does anyone know how could this happen?
Small reproducible repository: https://github.com/tomgun132/pants_test_proto
Thank you beforehand!limited-balloon-79828
09/27/2023, 4:33 AMsrc/common/module_something.py
exists in the unzipped files.
However, when I run pants test --keep-sandboxes=on_failure
and see the sandbox, src/common/module_something.py
doesn't exist there.broad-processor-92400
09/27/2023, 5:12 AMlimited-balloon-79828
09/27/2023, 5:13 AMbroad-processor-92400
09/27/2023, 5:14 AMlimited-balloon-79828
09/27/2023, 5:18 AMsrc/common
contains protobuf generated codes...broad-processor-92400
09/27/2023, 5:19 AMlimited-balloon-79828
09/27/2023, 5:22 AMpython -m grpc_tools.protoc -I. --python_out=src/common --pyi_out=src/common --grpc_python_out=src/common module_something.proto
But the strange thing is that, this only happens in pants test
. When I run pants run
to the code that has the same import, it works without any problem even though it has the same warning.broad-processor-92400
09/27/2023, 5:24 AMlimited-balloon-79828
09/27/2023, 5:24 AMlimited-balloon-79828
09/27/2023, 5:27 AMpy
or pyi
.
The BUILD file for src/common
is something like this
python_sources(
name="common-generated",
sources=[
"common/*.py*",
],
)
and the BUILD file in my main code
python_sources(
name="app",
sources=["app/*.py"],
dependencies=[
"src:common-generated",
"third_party:reqs",
],
)
broad-processor-92400
09/27/2023, 5:31 AMlimited-balloon-79828
09/27/2023, 6:03 AMlimited-balloon-79828
09/27/2023, 6:04 AMpants test ::
, it will yield:
ImportError while loading conftest '/tmp/pants-sandbox-5cFlJZ/app/tests/conftest.py'.
app/tests/conftest.py:3: in <module>
from common.common_pb2 import SumRequest
src/common/common_pb2.py:14: in <module>
import entities_pb2 as entities__pb2
E ModuleNotFoundError: No module named 'entities_pb2'
broad-processor-92400
09/27/2023, 6:15 AMentities_pb2
, but the adjacent file is "meant" to be accessible as import common.entities_pb2
(or from . import entities_pb2
) according to how the source roots are configured.
import entities_pb2 as entities__pb2
e.g. if I manually change
-import entities_pb2 as entities__pb2
+from . import entities_pb2 as entities__pb2
then pants test ::
gets further.
Some options are:
1. change the pants source roots so that it understands that each protobuf file is meant to be top-level packages, maybe something like src/common
2. change the protobuf code generation command to generate code appropriate for submodule (I think this means the result you're looking for is an __init__.py
file, and it using relative imports), no idea how to do this, you'll have to investigate
3. investigate pants' builtin support for protobuf and see if that helps (it might help with other things) https://www.pantsbuild.org/docs/protobuf-pythonlimited-balloon-79828
09/27/2023, 6:31 AMhome/tomgun/.pyenv/versions/3.10.11/lib/python3.10/importlib/__init__.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
app/tests/test_app.py:3: in <module>
from <http://app.app|app.app> import foo
E ModuleNotFoundError: No module named 'app'
limited-balloon-79828
09/27/2023, 6:42 AMsrc/entities
, there are multiple generated codes, let's say entities_foo_pb2
, entities_bar_pb2
, and entities_xyz_pb2
• service/common_pb2.py
import all of them with something like from entities.entities_foo_pb2 import entities_foo_pb2 as entities__foo_pb2
• For some reasons during test, the sandbox doesn't include entities_bar_pb2
, but entities_foo_pb2
and entities_xyz_pb2
exist inside the test sandboxbroad-processor-92400
09/27/2023, 6:45 AMlimited-balloon-79828
09/27/2023, 6:47 AMsrc/service/common_pb2.py:14: in <module>
from entities import entities_bar_pb2 as entities_bar__pb2
E ImportError: cannot import name 'entities_bar_pb2' from 'entities' (unknown location)
limited-balloon-79828
09/27/2023, 6:47 AMlimited-balloon-79828
09/27/2023, 7:09 AMpython_sources
in the same BUILD file that have sources
to the same file.
Thank you for your time!