Having an issue with a built pex file not finding ...
# general
a
Having an issue with a built pex file not finding a locally available import. My BUILD file looks like:
Copy code
python_sources(
    name="healthdash_sources",
    sources=["healthdash/**/*.py"],
)

pex_binary(
    name="healthdash_app",
    entry_point="healthdash.NOC_Pod_Overview",
    dependencies=[
        ":healthdash_sources",
    ],
)


poetry_requirements(
    name="poetry",
)
And my pyproject.toml looks like:
Copy code
[tool.poetry]
name = "healthdash"
version = "0.1.0"
description = ""
authors = ["REDACTED <REDACTED@website.net>"]
readme = "README.md"

[tool.poetry.dependencies]
python = ">=3.10,<4.0"
streamlit = ">=1.28.1"
httpx = { extras = ["http2"], version = ">=0.25.1" }
logicmonitor-sdk = ">=3.0.172"
confz = ">=2.0.1"
pymssql = ">=2.2.11"
altair = "^5.4.0"
skykick = { path = "../../lib/skykick/", develop = true }

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
In my entrypoint, NOC_Pod_Overview, I have an
import skykick
statement. My understanding is that pants should automatically infer that dependency and look where the poetry file says it should be, but maybe I'm misunderstanding how Pants reads from pyproject.toml?
Sorry didn't include the actual problem, the issue is that we get a
ModuleNotFoundError: No module named 'skykick'
If its unclear, skykick is just files, its not packaged, and currently is not part of the packaging/build step
e
If its within the monorepo, why not just use it directly? If
../../lib
is listed as a source root, you should just be able to import it and use it without even listing it in your pyproject.toml
This may have some consequences if you are trying to also publish it as a package, but if your only goal is to use it within your monorepo, it should work fine.
a
Mostly because I strongly dislike relative imports, and would much rather rely on poetry to handle it. Plus its easier to write new import statements with "import X", which works fine with poetry, vs writing relative imports which are not necessarily repeatable (its not as true in a monorepo sure, but it still adds unnecessary complexity).
e
It wouldn't be a relative import. I have a similar set up. I have a
src/python
directory that is listed as a source root in
pants.toml
(and set as part of my
PYTHONPATH
for the sake of VS code). within
src/python
I have
internal_lib_1
,
project_1
,
project_2
where the internal lib is meant to be shared, and project 1 and 2 are project specific. All of my imports are
from internal_lib_1 import something
or
import internal_lib_1
h
Further to Luke’s correct observation, I don’t think Pants can do anything useful with that
skykick
stanza in
[tool.poetry.dependencies]
. Pants can read 3rdparty dependencies (i.e., those specified by a name and version constraint) from there, but internal deps from other parts of the repo are inferred internally. You just need the source root to be configured. You don’t need relative imports.