https://pantsbuild.org/ logo
#general
Title
# general
s

square-pharmacist-60243

05/26/2022, 8:54 AM
Hi. Here is my sub-project structure. I have added
src/py/sub-project
to the roots.
Copy code
src/py/sub-project
    sub_project/
        __init__.py
        __main__.py
        ...
    tests/
        test_sub_project.py
When I run
./pants tailor
, I see two
BUILD
files added, one in
src/py/sub-project/sub_project
, the other in
src/py/sub-project/tests
. Why isn’t there a
BUILD
file in
src/py/sub-project
if that is the root? Now, in
src/py/sub-project/sub_project/BUILD
it has generated a
pex_binary
entry with name
__main__
and
entry_point="__main__.py"
. When I as a developer add a
__main__.py
file under a Python package (
sub_project
in this case), its goal is to provide a standard entrypoint for the package, not entrypoint in itself as if
__main__.py
was in
PYTHONPATH
(which is what name
__main__
for
pex_binary
looks to me). If I want to make
sub_project
package into something that other subprojects can depend on, where do I declare that it “publishes” the sources under name
sub_project
? Intuitively, I would put it under
src/py/sub-project/BUILD
because that directory (
src/py/sub-project
) sees the Python package
sub_project
in its entirety. But this seems to not match what
./pants tailor
generates. If I put it under
src/py/sub-project/sub_project/BUILD
in
python_sources
, will the contents be under
sub_project
Python package namespace?
c

curved-television-6568

05/26/2022, 9:04 AM
Hi Jāzeps 👋
Why isn’t there a
BUILD
file in
src/py/sub-project
if that is the root?
Pants tailor will only create
BUILD
files where there are “targets” that needs to be declared. If you don’t have any files there, then there are no targets to declare there (you may want to create a BUILD file there for a
python_distribution
perhaps, but that will be a manual operation)
Now, in
src/py/sub-project/sub_project/BUILD
it has generated a
pex_binary
entry with name
__main__
and
entry_point="__main__.py"
. When I as a developer add a
__main__.py
file under a Python package (
sub_project
in this case), its goal is to provide a standard entrypoint for the package, not entrypoint in itself as if
__main__.py
was in
PYTHONPATH
(which is what name
__main__
for
pex_binary
looks to me).
I’m not sure about the
PYTHONPATH
thing, but this usually does the right thing. When you package that
pex
you will have an executable that when you run it, will execute your
__main__.py
as expected, and any sources that it depends on will be included in the pex. https://www.pantsbuild.org/docs/pex-files https://www.pantsbuild.org/docs/reference-pex_binary#codeentry_pointcode
If I want to make
sub_project
package into something that other subprojects can depend on, where do I declare that it “publishes” the sources under name
sub_project
?
You should just be able to import the sources as-is. When it comes to packaging, you may split your sources into different distributions, in which case you will get proper install requires dependencies between those distributions as required. https://www.pantsbuild.org/docs/python-distributions So, to be clear, the BUILD files placements does not affect your package names to use for importing your python modules. That is solely the source roots, and where you have your
__init__.py
files (for namespace packages etc) and how the source files are laid out in your source tree. Then all your sources needs to be “owned” by
python_sources
targets in order for Pants to be able to do its dependency inference magic, and later also for packaging/testing/formatting/linting etc…
Hope this gives some material to get you further.. 🙂
s

square-pharmacist-60243

05/26/2022, 9:42 AM
Thanks @curved-television-6568 The note about BUILD file placement not having any effect on Python packages is very useful. I am still not convinced how the package name will be worked out though. Maybe I will reformulate my vague question after a few experiments.
h

happy-kitchen-89482

05/26/2022, 1:42 PM
I think I see the source of the confusion: BUILD files just say "here is some code", and maybe provide metadata to describe that code. You can have many BUILD files inside a single package tree (one per directory is a common way to do it).
👍 1
As @curved-television-6568 mentions, what determines the import path of your code is the source roots, which you configure in pants.toml
In your case you would set
src/py/sub-project
as a source root
And then code both inside and outside that source root can
import sub_project
3 Views