echoing-nightfall-72095
01/14/2021, 1:39 PMpants.toml i’ve defined roots
[source]
root_patterns = [
"/apps/*",
"/libs"
]
I can run my apps with:
./pants run apps/a
However, all imports cannot be found using linting (IDE is not happy either).
I have include in dependencies
python_library(
dependencies=[
"3rdparty/python:flask",,
"libs/z",
"libs/y",
"libs/x"
],
)
I import as:
from z import something
and
from sublib import somthingelse
where the sublib is defined in a BUILD file using python_library()
Everything runs great but I would like full support.
When I look at the documentation I can see an example of multiple top level project https://www.pantsbuild.org/docs/source-roots#multiple-top-level-projects.
Is there a way to make project subtop level and still have linters finding imports and stuff?
BR Alexenough-analyst-54434
01/14/2021, 2:59 PM./pants run X works and ./pants lint X fails? Can you provide full ./pants lint output?hundreds-father-404
01/14/2021, 3:00 PMfrom x.sublib1 import Bar
from y.sublib2 import Qux
Or, are you doing
from sublib1 import Bar
Where sublib1 is a subdirectory of one of {x, y, z}, and which parent folder it uses is determined by where the folder is defined? That is, you might have x/sublib and y/sublib, and which to use depends on the location you're importing from? (I don't expect that to work, but trying to understand your setup)echoing-nightfall-72095
01/14/2021, 3:13 PMlintenough-analyst-54434
01/14/2021, 3:16 PMhundreds-father-404
01/14/2021, 3:18 PMrun would work but not lint. run is usually more stringent
My main confusion is how these imports of subdir are structured (☝️) and what is meant by
Is there a way to may project subtop level
echoing-nightfall-72095
01/14/2021, 3:18 PMimport x.sublibechoing-nightfall-72095
01/14/2021, 3:19 PMhundreds-father-404
01/14/2021, 3:20 PMbut I get PyLint error in lintBy chance, do you have
__init__.py files in your repo? Where are those? Pylint and MyPy do not work with implicit namespace packages (PEP 420) very well, whereas other workflows like run support itechoing-nightfall-72095
01/14/2021, 3:39 PMa.py in project folder a
from handler.handle import func
handler is a subfolder in a that has a handle.py with a function named func I get:
E0611: No name 'handlers' in module 'handlers' (no-name-in-module)
E0401: Unable to import 'handlers.handle' (import-error)hundreds-father-404
01/14/2021, 3:45 PMso your import statements appear to be well-formed thenIf you have `libs/a/subdir/file.py`—and source root is `libs`—then your import should always be
import a.subdir.file or from a.subdir.file import func. It shouldn't be subdir.file - having that top-level a is really crucial.
Note that in a monorepo, you can import from anywhere in the repo. You need that top-level a part for every tool (not only Pants) to know where subdir.file is coming from.hundreds-father-404
01/14/2021, 3:47 PMa folder in your imports, you'd need to change your source roots to be libs/a, libs/b. And then your imports should always be import subdir.file without ever having a be used
But you have to guarantee that you don't have something like libs/a/subdir and libs/b/subdir at the same time - those would both strip down to subdir and Python would be confused which to use. This is why the namespaces like a and b are helpfulenough-analyst-54434
01/14/2021, 3:51 PMhundreds-father-404
01/14/2021, 3:54 PMrun isn't touching the problematic imports, whereas they are likely running ./pants lint :: and running over everything. Run would fail were it to touch those same imports
Oh, well and PEP 420. Run should work with that now, but iirc Pylint still doesn't like it.enough-analyst-54434
01/14/2021, 3:56 PMechoing-nightfall-72095
01/14/2021, 7:13 PMhundreds-father-404
01/14/2021, 7:15 PMsrc/python, for example. You'd do it on libs/ in your repoechoing-nightfall-72095
01/14/2021, 7:19 PMhundreds-father-404
01/14/2021, 7:20 PMechoing-nightfall-72095
01/14/2021, 7:48 PMechoing-nightfall-72095
01/14/2021, 7:55 PMapps/translation_service/app.py I try to from handlers.handlers import transalte_text in apps/translation_service/handler I have three files: handlers.py , __init__.py and BUILD with a python_library() declaration. When running ./pants lint :: I keep getting this PyLint error:
apps/translation_service/app.py:3:0: E0611: No name 'handlers' in module 'handlers' (no-name-in-module)
apps/translation_service/app.py:3:0: E0401: Unable to import 'handlers.handlers' (import-error)
If i move the handlers.py into apps/translation_service and from handlers import translate_text then I get:
apps/translation_service/app.py:3:0: E0611: No name 'handle_translate_tags' in module 'handlers' (no-name-in-module)
apps/translation_service/app.py:3:0: E0611: No name 'handle_translate_text' in module 'handlers' (no-name-in-module)hundreds-father-404
01/14/2021, 7:57 PM./pants roots say?echoing-nightfall-72095
01/14/2021, 7:57 PMhundreds-father-404
01/14/2021, 7:59 PMapps/translation_service/__init__.py. It can be empty. It sounds like right now you only have apps/translation_service/handlers/__init__.py iiucechoing-nightfall-72095
01/14/2021, 8:00 PMhundreds-father-404
01/14/2021, 8:01 PMhandlers/handlers.py to handlers/handlers_foo.py, and the import handlers.handlers_fooechoing-nightfall-72095
01/14/2021, 8:04 PMapps/translation_service/app.py:3:0: E0611: No name 'handlers_foo' in module 'handlers' (no-name-in-module)
apps/translation_service/app.py:3:0: E0401: Unable to import 'handlers.handlers_foo' (import-error)hundreds-father-404
01/14/2021, 8:06 PM./pants dependencies apps/translation_service/app.py say? I'm wondering if perhaps Pants is for some reason not picking up the dependency (although that's weird that run would work.)echoing-nightfall-72095
01/14/2021, 8:06 PMechoing-nightfall-72095
01/14/2021, 8:08 PMechoing-nightfall-72095
01/14/2021, 8:12 PMapps/semantic_tagging_service/app.py , from apis/v1/api import BLUEPRINT where apps/semantic_tagging_service/apis/v1/api.py works fine 😄hundreds-father-404
01/14/2021, 8:14 PM--no-process-execution-cleanup-local-dirs, then look if all the files you expect are there. This will help us to confirm whether it's an issue with Pants's not properly including the files vs. with Pylint not liking your project setupechoing-nightfall-72095
01/14/2021, 8:18 PMhundreds-father-404
01/14/2021, 8:19 PM./pants run will run under <build-root>/pants.d so that you can do things like write files to your build root. But most processes like test and lint instead run in a temporary directory, often in /tmp/. That option will tell you where the tmpdir is and avoid cleanup of it so that you can inspect the "chroot"echoing-nightfall-72095
01/14/2021, 8:24 PMhundreds-father-404
01/14/2021, 8:25 PMls /tmp/path/to/dir and seeing what files are in it.
Run ./pants --no-process-execution-cleanup-local-dirs lint ::, then look for the log message saying what folder was usedechoing-nightfall-72095
01/14/2021, 8:30 PMechoing-nightfall-72095
01/14/2021, 8:31 PM____findbinary.shhundreds-father-404
01/14/2021, 8:34 PMechoing-nightfall-72095
01/14/2021, 8:37 PMechoing-nightfall-72095
01/14/2021, 8:37 PM[python-setup]
interpreter_constraints = ["CPython==3.8.*"]echoing-nightfall-72095
01/14/2021, 8:40 PMechoing-nightfall-72095
01/14/2021, 8:51 PMpylint apps/translation-service/app.py and get
apps/translation_service/app.py:3:0: E0401: Unable to import 'handlers.handlers_foo' (import-error)hundreds-father-404
01/14/2021, 8:54 PMPYTHONPATH=apps/translation-service pylint apps/translation-service/app.py so that Python/Pylint know to ignore the prefixechoing-nightfall-72095
01/14/2021, 9:00 PMechoing-nightfall-72095
01/14/2021, 9:00 PMhundreds-father-404
01/14/2021, 9:02 PM./pants roots already includes apps/translation-service, Pants should be setting PYTHONPATH to that value automatically.
You've already confirmed the right files are showing up in the chroot, so it's not an issue with missing dependencies.
One silly thing to check, do you accidentally have an __init__..py in the apps/ folder?echoing-nightfall-72095
01/14/2021, 9:03 PMechoing-nightfall-72095
01/15/2021, 8:18 AMhundreds-father-404
01/15/2021, 8:37 AM