Hi all, I'm having a problem with imports not bein...
# general
a
Hi all, I'm having a problem with imports not being picked up by Pants, is anyone able to help please? Directory structure:
Copy code
/src/my_app/my_app.py
/src/my_app/utils.py
/tests/test_my_app.py
my_app.py
Copy code
from utils import func1, func2, func3
test_my_app.py
Copy code
from my_app.my_app import function_to_test
pants.toml
Copy code
[source]
root_patterns = [
  ".",
  "/cdk",   # AWS CDK stuff in Python, not relevant to this
  "/src",
  "/tests",
]
/src/my_app/BUILD
Copy code
python_sources()
pants dependees src/my_app/utils.py
Copy code
20:06:56.42 [WARN] Pants cannot infer owners for the following imports in the target src/my_app/my_app.py:

  * utils.func1 (line: 8)
  * utils.func2 (line: 8)
  * utils.func3 (line: 8)
pants dependencies src/my_app:my_app
Copy code
src/my_app/my_app.py
src/my_app/utils.py
c
have you tried
from my_app.utils import func1, func2, func3
a
Sorry, I meant to add that. I've tried that and pants picks it up. However, when I run my app in a docker container I get a module not found error. I've not got as far as managing docker builds with pants yet.
c
ok, so thatโ€™s is another issue, and is the correct solution for this issue.
can you share more details about the docker issue?
a
It's a basic Dockerfile:
Copy code
ARG FUNCTION_DIR="/function"

FROM ubuntu:20.04

RUN ... # install some dependencies etc.

ARG FUNCTION_DIR

COPY ./my_app.py ${FUNCTION_DIR}/
COPY ./utils.py ${FUNCTION_DIR}/
COPY ./entrypoint.sh /entrypoint.sh

WORKDIR ${FUNCTION_DIR}

ENTRYPOINT [ "/entrypoint.sh" ] # python my_app.py
It works fine if I do
from utils import ...
but not with
from my_app.utils import ...
I suspect this is a beginner-level python problem, but I came up through the "ops" side of DevOps ๐Ÿ˜„
I get the same result if I run
python my_app.py
locally as well
c
yes, that is a rather crude way of getting your sources into the container ๐Ÿ˜› you need to preserve the directory name of
my_app
for the sources
I would recommend either to
COPY my_app $FUNCTION_DIR/
or even to package the whole thing up as a โ€œpexโ€ and run that.
but may want to postpone the pex bit until things are a bit more clear first ๐Ÿ˜‰
a
So if I do
COPY my_app $FUNCTION_DIR/
my entrypoint script should run
python my_app/my_app.py
is that correct?
c
yes, that seems right
a
Great, thanks for taking the time to help me! I'll let you know how I get on.
c
great!
a
Unfortunately that didn't work ๐Ÿ˜ž
I'll keep plugging away though!
๐Ÿ‘ 1