Hi, I'm trying to run a script with `pants run .....
# general
h
Hi, I'm trying to run a script with
pants run ...
. When the script is a single file, it works perfectly. However, I can't figure out how to configure it to work with relative imports. Will appreciate any help. Details in ๐Ÿงต.
โœ… 1
Error:
Copy code
...
UnownedDependencyError: Pants cannot infer owners for the following imports in the target scripts/pants/utils/render.py:render-script-src:

  * .lib (line: 1)
...
Command:
pants run ./scripts/pants/utils/render.py
BUILD:
Copy code
python_sources(
    name="render-script-src",
    sources=["render.py", "lib.py", "__init__.py"],  # These files are in the same directory. Tried both with and without the init.py file.
    interpreter_constraints=["==3.9.*"],
    run_goal_use_sandbox=False,
    resolve="render-script",
)

python_requirement(
    name="render-script-requirements",
    requirements=[],
    resolve="render-script",
)
In pants.toml,
root_patterns
has
"/scripts/pants/utils"
. Tried the following import forms:
Copy code
from scripts.pants.utils import lib
from pants.utils import lib
from utils import lib
from . import lib
c
As the files live in the source root, have you tried
import lib
?
h
Putting your own code directly in the source root without any namespacing is risky, due to possible collisions with stdlib or 3rd party package names. I'd recommend nesting everything under
mynamespace
and then
from my namespace import ...
h
๐Ÿคฆ
Moving it to a sub dir fixed it. Thanks! โค๏ธ
๐ŸŽ‰ 1
๐Ÿ‘ 1