```|directory_a -|directory_b -|a_python_file.py ...
# general
l
Copy code
|directory_a
-|directory_b
 -|a_python_file.py
 -|BUILD
-|directory_c
 -|b_python_file.py
 -|BUILD
b_python_file.py imports a function from a_python_file.py using a relative import. Pantsbuild says it can't infer the dependency. What do I need to do or add to the BUILD file in directory_c so pants knows about this dependency?
✅ 1
e
You shouldn't need to add to the build files (beyond specifying
python_sources()
in each of them). My guess is that you need to ensure you have source roots configured appropriately
l
Copy code
# directory_a/directory_b/BUILD

python_sources(
    name=f"test_package_b.lib",
    sources=["**/*.py"],
)
Copy code
# directory_a/directory_c/BUILD
python_sources(
    name=f"test_package_c.lib",
    sources=["**/*.py"],
)
So I shouldn't need to specify the dependency of test_package_b anywhere in directory_a/directory_c/BUILD if the source roots are correctly configured?
e
That is correct. Pants is able to read your
import
statements, and determine the dependencies automatically. You can test this by running
pants dependencies directory_a/directory_c/b_python_file.py
This will find all the dependencies of b, and you should be able to observe that a is included. (if you use the
--transitive
flag you should also be able to see the dependencies of a, (ie. that b transitively depends on a's dependencies)
l
Okay. Let me look into the source roots link you provided and try and fix that then. Thank you for pointing me in the right direction!
I've resolved the issue. The source roots were correct, but I needed to create a BUILD and python_sources() in the particular directory of the dependency. Once I did this, it resolved the "can't infer the owner of this dependency" error. I tried to run the pex, but it output
Copy code
Failed to resolve all requirements for cp310-cp310-manylinux_2_35_x86_64 interpreter...
I added the newly generated python sources from the dependency into the pex_binary target dependency list, and it resolved it. Thanks!