steep-carpenter-74400
05/18/2021, 9:48 PMNo module named main
error -- which I can see is due to having two projects with a src/main.py
. Indicative project structure here:
āāā BUILD
āāā apple-pie-api
āĀ Ā āāā BUILD
āĀ Ā āāā Pipfile
āĀ Ā āāā Pipfile.lock
āĀ Ā āāā requirements.txt
āĀ Ā āāā src
āĀ Ā āāā main.py
āĀ Ā ...
āāā common-requirements.txt
āāā pants
āāā pants.toml
āāā randmoji-api
ā āāā BUILD
ā āāā Pipfile
ā āāā Pipfile.lock
ā āāā requirements.txt
āĀ Ā āāā src
āĀ Ā āāā main.py
āĀ Ā ...
ā ...
I hit the issue when I try to execute the output pex for one of the projects; e.g., via ./pants package apple-pie-api:pex_binary && ./dist/apple-pie-api/pex_binary.pex
.
Renaming one of the main.py
(e.g., to main2.py
) does the trick. But I'm wondering if there's a way to avoid needing to do that?
Looking at the source roots https://www.pantsbuild.org/docs/source-roots this isn't in those example set ups, so perhaps I'm off piste here? But this is a set up I'd like to use as part of a transition to pants.steep-carpenter-74400
05/18/2021, 9:52 PMenough-analyst-54434
05/18/2021, 10:12 PM$ ./pants package apple-pie-api:pex_binary
15:10:05.90 [WARN] The pex_binary target apple-pie-api:pex_binary has the field `entry_point='src/main.py'`, which maps to the Python module `main`, but Pants cannot safely infer a dependency because >1 target exports this module, so it is ambiguous which to use: ['apple-pie-api/src/main.py:../apple-pie-api', 'randmoji-api/src/main.py:../randmoji-api'].
Please explicitly include the dependency you want in the `dependencies` field of apple-pie-api:pex_binary, or ignore the ones you do not want by prefixing with `!` or `!!` so that <=1 targets are left.
Alternatively, you can remove the ambiguity by deleting/changing some of the targets so that only 1 target exports this module. Refer to <https://www.pantsbuild.org/v2.4/docs/troubleshooting#import-errors-and-missing-dependencies>.
15:10:05.91 [INFO] Wrote dist/apple-pie-api/pex_binary.pex
Which is followed via:
$ git diff
diff --git a/apple-pie-api/BUILD b/apple-pie-api/BUILD
index 3f3f778..44e6ed6 100644
--- a/apple-pie-api/BUILD
+++ b/apple-pie-api/BUILD
@@ -7,6 +7,9 @@ python_library(
pex_binary(
name="pex_binary",
entry_point="src/main.py",
+ dependencies=[
+ "apple-pie-api",
+ ]
)
# You need `python_requirements` to teach pants that this project has third-party requirements
Which nets you:
$ ./pants package apple-pie-api:pex_binary
15:10:00.12 [INFO] Wrote dist/apple-pie-api/pex_binary.pex
$ zipinfo dist/apple-pie-api/pex_binary.pex | tail -10
-rw-r--r-- 2.0 unx 558 b- defN 80-Jan-01 00:00 .bootstrap/pex/vendor/_vendored/setuptools/pkg_resources/py31compat.py
-rw-r--r-- 2.0 unx 984 b- defN 80-Jan-01 00:00 .bootstrap/pex/venv_bin_path.py
-rw-r--r-- 2.0 unx 155 b- defN 80-Jan-01 00:00 .bootstrap/pex/version.py
-rw-r--r-- 2.0 unx 590 b- defN 80-Jan-01 00:00 PEX-INFO
-rw-r--r-- 2.0 unx 2868 b- defN 80-Jan-01 00:00 __main__.py
-rw-r--r-- 2.0 unx 932 b- defN 80-Jan-01 00:00 main.py
drwxr-xr-x 2.0 unx 0 b- stor 80-Jan-01 00:00 numerics/
-rw-r--r-- 2.0 unx 55 b- defN 80-Jan-01 00:00 numerics/__init__.py
-rw-r--r-- 2.0 unx 658 b- defN 80-Jan-01 00:00 numerics/pi.py
And the desired outcome:
$ ./dist/apple-pie-api/pex_binary.pex
Traceback (most recent call last):
File "/home/jsirois/dev/mattjw/jsirois-pants-multiproject-mwe/dist/apple-pie-api/pex_binary.pex/.bootstrap/pex/pex.py", line 489, in execute
File "/home/jsirois/dev/mattjw/jsirois-pants-multiproject-mwe/dist/apple-pie-api/pex_binary.pex/.bootstrap/pex/pex.py", line 406, in _wrap_coverage
File "/home/jsirois/dev/mattjw/jsirois-pants-multiproject-mwe/dist/apple-pie-api/pex_binary.pex/.bootstrap/pex/pex.py", line 437, in _wrap_profiling
File "/home/jsirois/dev/mattjw/jsirois-pants-multiproject-mwe/dist/apple-pie-api/pex_binary.pex/.bootstrap/pex/pex.py", line 545, in _execute
File "/home/jsirois/dev/mattjw/jsirois-pants-multiproject-mwe/dist/apple-pie-api/pex_binary.pex/.bootstrap/pex/pex.py", line 672, in execute_entry
File "/home/jsirois/dev/mattjw/jsirois-pants-multiproject-mwe/dist/apple-pie-api/pex_binary.pex/.bootstrap/pex/pex.py", line 684, in execute_module
File "/usr/lib/python3.8/runpy.py", line 210, in run_module
return _run_code(code, {}, init_globals, run_name, mod_spec)
File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/home/jsirois/dev/mattjw/jsirois-pants-multiproject-mwe/dist/apple-pie-api/pex_binary.pex/main.py", line 6, in <module>
ModuleNotFoundError: No module named 'uvicorn'
Well - not quite the desired outcome, but now you're on the road to working more things out.enough-analyst-54434
05/18/2021, 10:13 PMenough-analyst-54434
05/18/2021, 10:14 PMenough-analyst-54434
05/18/2021, 10:16 PMmain
modules.steep-carpenter-74400
05/18/2021, 10:21 PMsteep-carpenter-74400
05/18/2021, 10:33 PMModuleNotFoundError: No module named 'uvicorn'
too. I didn't hit it on my local, so maybe the requirements.txt got re-generated on yours? Which is indeed what my (misleading) instructions do suggest to do š
.steep-carpenter-74400
05/18/2021, 10:37 PMmain.py
is fixed I'm re-thinking my assumptions about mixing requirements files (and common requireemnts). I'll come back here if i hit any other issues.
Thanks again!