refined-gigabyte-6122
11/09/2022, 8:28 PMexport-codegen
goal, I see the generated Python files where I expect them under dist/codegen
. But when `run`ing a script that depends on these generated files, I get an import error. I’ve included __init__.py
files in every directory.
Is there a way to manually inspect the directories that Pants links at runtime, especially the directory where it stores generated code?happy-kitchen-89482
11/09/2022, 8:33 PM--keep-sandboxes=on_failure
(or --keep-sandboxes=always
), which will preserve the sandbox dirs that Pants runs processes in, and log their locations.refined-gigabyte-6122
11/09/2022, 9:08 PM$ tree <sandbox dir>
<sandbox dir>
├── local_dists.pex
...
├── project
│ ├── __init__.py
│ ├── proto
│ │ ├── __init__.py
│ │ ├── generated_pb2.py
...
└── script.pex
...
$ PYTHONPATH=<sandbox dir> python3 -c "from project.proto import generated_pb2"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: cannot import name 'generated_pb2' from 'project.proto'
happy-kitchen-89482
11/09/2022, 9:14 PM__run.sh
inside the sandbox? (that is a script that ~replicates the process Pants ran)refined-gigabyte-6122
11/09/2022, 9:18 PM__run.sh
file. This is the log I’m using to get the <sandbox dir> above:
16:16:24.99 [INFO] Preserving local process execution dir /private/var/folders/tn/0sw9hrgd0zl_py1s8gtmfznr0000gp/T/pants-sandbox-N1XHGI for interactive process
project
lives), and it searched first in the current directory (where the generated file wouldn’t exist) before checking PYTHONPATH. When I first changed directories to the sandbox directory, then ran the import test, it worked.
I verified the sandbox dir is in sys.path
too, so I’m not sure why it’s not getting resolved when run via pants
. Anyway, I’ll continue debugging. Thanks for your help.happy-kitchen-89482
11/09/2022, 11:59 PM./pants run path/to/script.py
and it failed?./pants run path/to:script_target
with the address of the pex_binary target for the script?refined-gigabyte-6122
11/10/2022, 1:30 AMpython_source
, if that matters.run
a python_source
?happy-kitchen-89482
11/10/2022, 1:51 AMrun_goal_use_sandbox
on your python_sources
target?python_source
actually makes senserun_goal_use_sandbox=True
then I think the codegen should have workedrefined-gigabyte-6122
11/10/2022, 2:35 PMrun_goal_use_sandbox=True
set explicitly, and had the same issue.
However, I think I may have sorted this out. The problem seems to mirror my mistake above. Namely, that python will try to resolve modules in the execution directory before looking to the PYTHONPATH. I had my project set up with the top-level python module at the root of the repository. When I simply moved that top-level module directory under a src
directory, updated the root_patterns
config in pants.toml
(from ["/"]
to ["/src"]
), and the various build targets to add the src
prefix, it worked.
Is it possible that pants
is replicating/preserving the semantics of python, looking first in the local directory even though it won’t contain generated files? I can try to put together a minimal reproducible example if that would be helpful.happy-kitchen-89482
11/10/2022, 5:08 PMrefined-gigabyte-6122
11/10/2022, 7:40 PMhappy-kitchen-89482
11/11/2022, 12:34 AMrefined-gigabyte-6122
11/11/2022, 12:35 AMhappy-kitchen-89482
11/11/2022, 1:58 AMrefined-gigabyte-6122
11/11/2022, 3:53 AMfancy-daybreak-62348
01/19/2023, 5:39 PM