careful-airline-93122
09/28/2023, 10:07 PMpants run
instead of poetry it just can't find the first-party imports and bombs out on no module found errors, It isn't stuff we should need to declare explicitly but tried that anyway. anybody else have similar issues?
It's working fine for imports from a 'shared' directory with logging config in it that's coming into the app, and because that's at the top level it's declared in source roots. so I'm wondering if it's something related to that.enough-analyst-54434
09/28/2023, 10:14 PMpants roots
and confirm the printed list includes the start of all your import paths; ie: a valid PYTHONPATH
for your source tree. Definitely slow down and get that right 1st. Almost nothing else should be attempted before understanding source roots and configuring them correctly.careful-airline-93122
09/28/2023, 10:15 PMcareful-airline-93122
09/28/2023, 10:32 PM/
, src/python
, etc.
we could import something like
import src.python.whatever
wherever we needed to throughout the codebase.
I think because we had a lower-level source root
src/python
what we needed to do is have our import statement pick up where the source root left off a la
import .whatever
in our case because we were trying to import from the directory above the containing file. and that directory was immediately below src/python
this seems to be working
as long as the imports aren't coming from within an __init__.py
file. pants doesn't like that much I'm guessing because of the way python import syntax handles it as just the module name.
we seemed to have worked around that by creating a resource target for that __init__.py
file within the same directory as the file, setting that file as a source.
I'm only mostly sure about my understanding of why it's working or not in various scenarios and I guess that's the journey, but maybe this helps somebodyenough-analyst-54434
09/28/2023, 10:57 PMPYTHONPATH=X python -c 'import my.app.module'
to work. Whatever the contents of X you have for PYTHONPATH
to have gotten that working - that's what the Pants source roots need to be for Python.enough-analyst-54434
09/28/2023, 10:58 PMcareful-airline-93122
09/29/2023, 5:22 PM/
in source_roots
(specifying the monorepo root dir) presumably I would hope that puts it in PYTHONPATH, and I would expect to be able to use imports throughout my app with no issue provided the dependencies I'm importing are under that point in the directory structure (provided I start my import statement from where that source_root path leaves off.) I believe to have observed that this understanding is not correct, though I'm going to go back and test it now. maybe I just got turned around. or maybe it didn't make it onto PYTHONPATH (I'm fairly sure I checked.)careful-airline-93122
09/29/2023, 5:23 PMpants run
seems to always run from the monorepo root so for applications like django that expect things to be in certain places relative to itself it's been a bit of a tug of warcareful-airline-93122
09/29/2023, 5:24 PMmanage.py
and in my pytest configuration (to handle pants test
scenarios that don't run via manage.py)enough-analyst-54434
09/29/2023, 5:33 PMexactly! and for example, if I have / in source_roots (specifying the monorepo root dir) presumably I would hope that puts it in PYTHONPATH, and I would expect to be able to use imports throughout my app with no issueBut, you said you have Python code under
src/python
. Surely src.python
is not a Python package. Surely the packages start under src/python
, no?enough-analyst-54434
09/29/2023, 5:36 PMsrc/python
listed as a source root if this repo is setup using words in the way they typically are used. Having just /
as a source root is almost positively incorrect unless you also have bare modules in the root. Do you? Like do you also have example.py
in the repo root that should be imported as import example
in addition to modules under src/python
like src/python/foo/bar.py
that should be imported as from foo import bar
?enough-analyst-54434
09/29/2023, 5:38 PMenough-analyst-54434
09/29/2023, 5:39 PMcareful-airline-93122
09/29/2023, 8:49 PMshared/python
which I'm realizing based on what you said would be just another source root and no different from any of the other modules. so having /
only as a source root would not make sense as it's not a python module, but a directory containing them. hopefully I'm grokking that right, and I appreciate you taking the time to explain.careful-airline-93122
09/29/2023, 8:51 PMenough-analyst-54434
09/29/2023, 8:54 PMcareful-airline-93122
09/29/2023, 8:56 PM