ambitious-actor-36781
07/13/2021, 6:04 AM//test/python/broker/mock_service.py
) that a test runs via POpen
and then checks the client and service can talk to each other.
So I've created a pex_binary
target in pants, and added that to the runtime_package_dependencies
field in my tests.
but when the binary runs, it just errors with ModuleNotFoundError: No module named 'broker.mock_service'
what am I missing?
Is it something to do with the tests and the actual library sitting under same-named parent folders? (in this case, broker
)enough-analyst-54434
07/13/2021, 6:21 AMambitious-actor-36781
07/13/2021, 6:25 AMsrc/python/broker/___init___.py
with ... stuff in itambitious-actor-36781
07/13/2021, 6:26 AMenough-analyst-54434
07/13/2021, 6:27 AMambitious-actor-36781
07/13/2021, 6:28 AMenough-analyst-54434
07/13/2021, 6:28 AMenough-analyst-54434
07/13/2021, 6:28 AMambitious-actor-36781
07/13/2021, 6:30 AMPlease
just munging everything together to get past this limitation.
But that was 5+ years ago, so I could be wrongenough-analyst-54434
07/13/2021, 6:32 AM__init__.py
.
2. They were a bit reckless an concatted all __init__.py
?ambitious-actor-36781
07/13/2021, 6:32 AMenough-analyst-54434
07/13/2021, 6:32 AMambitious-actor-36781
07/13/2021, 6:42 AMpython_library
and a pex_binary
all to make a single file executable.enough-analyst-54434
07/13/2021, 6:44 AMenough-analyst-54434
07/13/2021, 6:46 AM./pants tailor
goal by the way? That's a bridge to reduce manual boilerplate.enough-analyst-54434
07/13/2021, 6:46 AMenough-analyst-54434
07/13/2021, 6:47 AMambitious-actor-36781
07/13/2021, 6:50 AMbroker/mock_service.py
along with the broker
library.
It could be just slslskdfjsdflsdf.py
But given how pants maps things based on the root_patterns
it's just an inconvenient conflict that has arisen from separating src/python/<module>
and test/python/<module>
enough-analyst-54434
07/13/2021, 6:56 AMsys.path
/ PYTHONPATH
of ["src/python","test/python"]
and - Pants aside - Python requires proper namespace packages for you to split a package across both those sys.path
entries.ambitious-actor-36781
07/13/2021, 6:58 AM#!/usr/bin/env python
and a if ___name___ == "___main__":_
• Pants suggested /src/python/<package>
and /test/python/<package>
as filepaths
• Pex wants everything installed as a package
• and Python just made poor life choices.enough-analyst-54434
07/13/2021, 7:00 AMPex wants everything installed as a packageI don't follow. Can you explain more?
ambitious-actor-36781
07/13/2021, 7:02 AMif name == "main"
style script into a pex binary w/ dependencies.ambitious-actor-36781
07/13/2021, 7:04 AMenough-analyst-54434
07/13/2021, 7:05 AMpex -D <dir containing script> -e <script module name>
should do it.enough-analyst-54434
07/13/2021, 7:10 AM(pex) ~ $ mkdir src
(pex) ~ $ vi src/script.py
(pex) ~ $ cat src/script.py
#!/usr/bin/env python
if __name__ == "__main__":
print("Hello from script.")
(pex) ~ $ pex -D src/ -e script -o script.pex
(pex) ~ $ ./script.pex
Hello from script.
(pex) ~ $
enough-analyst-54434
07/13/2021, 7:12 AMambitious-actor-36781
07/13/2021, 8:31 AMraise error(msg.format(mod_name, type(ex).__name__, ex)) from ex
ImportError: Error while finding module specification for 'examples.mock_script' (ModuleNotFoundError: No module named 'examples')
Commit 3 - Created a single-file python_library
and add that as a dependency to the script and 🎉 it works.
It feels excessive making to declarations for what amounts to a single-file script. But it's not goingto be a frequent occurrence. Easy to accept it as a constraint of the tool.enough-analyst-54434
07/13/2021, 10:02 AMjsirois@siroisdesign:~/verbose-pants-script-example$ git diff
diff --git a/src/python/a_lib/BUILD b/src/python/a_lib/BUILD
index dfff480..d80ecb5 100644
--- a/src/python/a_lib/BUILD
+++ b/src/python/a_lib/BUILD
@@ -1,4 +1 @@
-
-python_library(
- name="a_lib",
-)
+python_library()
diff --git a/test/python/a_lib/BUILD b/test/python/a_lib/BUILD
index 740479d..408b173 100644
--- a/test/python/a_lib/BUILD
+++ b/test/python/a_lib/BUILD
@@ -1,7 +1,6 @@
+python_library()
+
pex_binary(
name="mock_script",
entry_point="mock_script.py",
- dependencies=[
- "//src/python/a_lib"
- ],
)
jsirois@siroisdesign:~/verbose-pants-script-example$ ./pants package test/python/a_lib/::
09:59:19.07 [INFO] Initialization options changed: reinitializing scheduler...
09:59:19.55 [INFO] Scheduler initialized.
09:59:21.21 [INFO] Wrote dist/test.python.a_lib/mock_script.pex
jsirois@siroisdesign:~/verbose-pants-script-example$ dist/test.python.a_lib/mock_script.pex
Foo
jsirois@siroisdesign:~/verbose-pants-script-example$
enough-analyst-54434
07/13/2021, 10:05 AM./pants package
goal. The resulting Pex works as expected. If you could clarify the exact command line you hoped would work at commit 1, that would help.enough-analyst-54434
07/13/2021, 10:05 AM