I have this directory structure ```/openapi ./BU...
# general
p
I have this directory structure
Copy code
/openapi
 ./BUILD
 ./observatory.yml
/teletom/tom/server
  ./BUILD
   ... python
I have these source roots in my toml
Copy code
[source]
root_patterns = [
 "openapi",
 "teletom"
]
In openapi I set the yml as a resource then set it as a dependency to the teletom/tom/server python source. When I try to load the resource with
Copy code
importlib.resources.path("openapi", "observatory.yml")
I am seeing..
Copy code
ModuleNotFoundError: No module named 'openapi'
Does the src root need to be like openapi/src/openapi?
f
Are you able to share the BUILD file contents as well as the full log of the failing
pants
invocation?
p
Copy code
pants run teletom/tom/entry:binaries#server.py
13:22:37.90 [WARN] The `pex_binary` target teletom/tom/entry:binaries#server.py transitively depends on the below `files` targets, but Pants will not include them in the PEX. Filesystem APIs like `open()` are not able to load files within the binary itself; instead, they read from the current working directory.

Instead, use `resources` targets or wrap this `pex_binary` in an `archive`. See <https://www.pantsbuild.org/v2.16/docs/resources>.

Files targets dependencies: ['openapi:config', 'teletom/frontend:build']
13:23:15.34 [INFO] Completed: Building 25 requirements for teletom.tom.entry/server_py.pex from the python-default.lock resolve: adafruit-blinka, adafruit-circuitpython-neopixel, cffi, connexion[swagger-ui], dataclasses-json, flas... (278 characters truncated)
Traceback (most recent call last):
  File "/home/geethree/.pyenv/versions/3.9.10/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/home/geethree/.pyenv/versions/3.9.10/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/geethree/.pex/unzipped_pexes/d5ce9d1a05de575035c8b552120b12265c3f21d4/__main__.py", line 106, in <module>
    bootstrap_pex(__entry_point__, execute=__execute__, venv_dir=__venv_dir__)
  File "/home/geethree/.pex/unzipped_pexes/d5ce9d1a05de575035c8b552120b12265c3f21d4/.bootstrap/pex/pex_bootstrapper.py", line 618, in bootstrap_pex
    pex.PEX(entry_point).execute()
  File "/home/geethree/.pex/unzipped_pexes/d5ce9d1a05de575035c8b552120b12265c3f21d4/.bootstrap/pex/pex.py", line 560, in execute
    sys.exit(self._wrap_coverage(self._wrap_profiling, self._execute))
  File "/home/geethree/.pex/unzipped_pexes/d5ce9d1a05de575035c8b552120b12265c3f21d4/.bootstrap/pex/pex.py", line 467, in _wrap_coverage
    return runner(*args)
  File "/home/geethree/.pex/unzipped_pexes/d5ce9d1a05de575035c8b552120b12265c3f21d4/.bootstrap/pex/pex.py", line 498, in _wrap_profiling
    return runner(*args)
  File "/home/geethree/.pex/unzipped_pexes/d5ce9d1a05de575035c8b552120b12265c3f21d4/.bootstrap/pex/pex.py", line 603, in _execute
    return self.execute_entry(
  File "/home/geethree/.pex/unzipped_pexes/d5ce9d1a05de575035c8b552120b12265c3f21d4/.bootstrap/pex/pex.py", line 800, in execute_entry
    return self.execute_module(entry_point.module)
  File "/home/geethree/.pex/unzipped_pexes/d5ce9d1a05de575035c8b552120b12265c3f21d4/.bootstrap/pex/pex.py", line 808, in execute_module
    runpy.run_module(module_name, run_name="__main__", alter_sys=True)
  File "/home/geethree/.pyenv/versions/3.9.10/lib/python3.9/runpy.py", line 210, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "/home/geethree/.pyenv/versions/3.9.10/lib/python3.9/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/home/geethree/.pyenv/versions/3.9.10/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/geethree/.pex/unzipped_pexes/d5ce9d1a05de575035c8b552120b12265c3f21d4/tom/entry/server.py", line 12, in <module>
    from tom.server.main import make_controller
  File "/home/geethree/.pex/unzipped_pexes/d5ce9d1a05de575035c8b552120b12265c3f21d4/tom/server/main.py", line 28, in <module>
    OBSERVATORY_CONFIG = importlib.resources.path("openapi", "observatory.yaml")
  File "/home/geethree/.pyenv/versions/3.9.10/lib/python3.9/importlib/resources.py", line 161, in path
    reader = _get_resource_reader(_get_package(package))
  File "/home/geethree/.pyenv/versions/3.9.10/lib/python3.9/importlib/resources.py", line 49, in _get_package
    module = _resolve(package)
  File "/home/geethree/.pyenv/versions/3.9.10/lib/python3.9/importlib/resources.py", line 40, in _resolve
    return import_module(name)
  File "/home/geethree/.pyenv/versions/3.9.10/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ModuleNotFoundError: No module named 'openapi'
f
Copy code
13:22:37.90 [WARN] The `pex_binary` target teletom/tom/entry:binaries#server.py transitively depends on the below `files` targets, but Pants will not include them in the PEX. Filesystem APIs like `open()` are not able to load files within the binary itself; instead, they read from the current working directory.

Instead, use `resources` targets or wrap this `pex_binary` in an `archive`. See <https://www.pantsbuild.org/v2.16/docs/resources>.
^^ that's the problem
p
openapi/BUILD
Copy code
resource(
    name="resource",
    source="observatory.yaml",
)
...server/BUILD
Copy code
python_sources(
    dependencies=[
        "//teletom/frontend:build",
        "//openapi:resource",
        "//.build/requirements:reqs#setuptools",
   ]
)
Sorry never pressed send.
f
Does the module need an
__init__.py
file ?
p
OOOO let me try
f
and you'll probably need to add a
python_sources
target to let Pants make use of that file
p
negative =(
f
that warning is still suspicious. do you happen to have
openapi:config
and
teletom/frontend:build
file
targets as it mentioned?
p
yes teletom/frontend:build is a file
f
and if you change them to
resource
does the warning go away and/or does the error go away?
p
Yeah the warning goes away if I remove the file targets
f
maybe unzip the pex and see what the contents are to verify how the
openapi
modules have been packed into the pex?
That
ModuleNotFoundError: No module named 'openapi'
error implies to me that something is wrong with how the
openapi
module is being placed in the pex
hmm you have file in
openapi/observatory.yaml
and the source root
openapi
. You would need the file in
openapi/openapi/observatory.yaml
in order to import it in the manner you are trying to do
p
yeah that is what I was thinking 😉 said o/src/o though 😃
f
The source root is the top of the module import hierarchy.
well then you would need the file in
openapi/src/openapi
with a source root of
openapi/src