thankful-truck-6622
05/08/2024, 3:03 PMimportlib.import_module
to dynamically import different files. Since I'm not explicitly importing in python, pants
doesn't do the dependency magic to find files and include them in the build sandbox. That's fine because I can update the BUILD
to define the resource. But since the file I am importing is also using codegen protos it doesn't seem to find the pb2
files and include them in the sandbox either! So my first question was can I import the pb2
files? It doesn't appear that I can, but I'm new to this so maybe I am wrong.
File "/private/var/folders/jv/9rg1vfp12plc7px03gr7qryr0000gn/T/pants-sandbox-ooemt6/src/python/app/data/example/examples.py", line 1, in <module>
from example.v1.example_pb2 import Example
ModuleNotFoundError: No module named 'example'
The work around I have for now is I can explicitly import my example data file and pants finds everything! But that kind of defeats the purpose of generically using importlib.import_module
.
import importlib
from google.protobuf.json_format import MessageToJson
# Uncomment this to explicitly import the data file
# and the app will work!
# import app.data.example.examples
if __name__ == "__main__":
libs = ["examples"]
for name in libs:
lib = importlib.import_module(f"app.data.example.{name}")
I dunno, if anyone has any ideas for me I'd love to hear them!high-energy-55500
05/08/2024, 3:07 PMthankful-truck-6622
05/08/2024, 3:11 PMthankful-truck-6622
05/08/2024, 3:12 PMfrom example.v1.example_pb2 import Example
examples = [
Example(name="example1"),
Example(name="example2"),
]
high-energy-55500
05/08/2024, 3:15 PMBUILD
file and a __init__.py
file here?
https://github.com/fishst1k/pants-proto/tree/main/src/python/app/datathankful-truck-6622
05/08/2024, 3:17 PMhigh-energy-55500
05/08/2024, 3:25 PMexample_pb2
or example.proto
in that traceback?)thankful-truck-6622
05/08/2024, 3:31 PMpb2
is the compiled proto in python formthankful-truck-6622
05/08/2024, 3:34 PMpants run
and it generates the sandbox, if things are explicitly imported all the dependencies are found (yay!) - but when they are generically imported or provided via the BUILD
in this case it is unable to find the codegen stuff (the pb2
files)high-energy-55500
05/08/2024, 3:35 PMthankful-truck-6622
05/08/2024, 3:38 PMmodern-london-16641
05/08/2024, 4:55 PMlibs = ["app.data.example.examples"]
for lib_name in libs:
lib = importlib.import_module(lib_name)
thankful-truck-6622
05/08/2024, 5:12 PMthankful-truck-6622
05/08/2024, 5:13 PMmodern-london-16641
05/08/2024, 5:38 PMdiff --git a/pants.toml b/pants.toml
index e9ba168..fd39a0f 100644
--- a/pants.toml
+++ b/pants.toml
@@ -27,3 +27,6 @@ mypy_plugin = true
[python]
interpreter_constraints = ["CPython>=3.8,<3.11"]
enable_resolves = true
+
+[python-infer]
+string_imports = true
diff --git a/src/python/app/main.py b/src/python/app/main.py
index 764c4da..27d7661 100644
--- a/src/python/app/main.py
+++ b/src/python/app/main.py
@@ -8,9 +8,9 @@ from google.protobuf.json_format import MessageToJson
if __name__ == "__main__":
- libs = ["examples"]
- for name in libs:
- lib = importlib.import_module(f"app.data.example.{name}")
+ libs = {"app.data.example.examples": "examples"}
+ for path, name in libs.items():
+ lib = importlib.import_module(path)
data = getattr(lib, name)
for item in data:
~/workspace/pants-proto $ pants run src/python/app/main.py
12:37:43.61 [INFO] Initialization options changed: reinitializing scheduler...
12:37:46.47 [INFO] Scheduler initialized.
{
"name": "example1"
}
{
"name": "example2"
}
thankful-truck-6622
05/08/2024, 5:39 PM+[python-infer]
+string_imports = true
modern-london-16641
05/08/2024, 5:39 PMthankful-truck-6622
05/08/2024, 5:43 PM