square-pharmacist-60243
05/26/2022, 8:54 AMsrc/py/sub-project
to the roots.
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?curved-television-6568
05/26/2022, 9:04 AMWhy isn’t there aPants tailor will only createfile inBUILD
if that is the root?src/py/sub-project
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)curved-television-6568
05/26/2022, 9:07 AMNow, inI’m not sure about theit has generated asrc/py/sub-project/sub_project/BUILD
entry with namepex_binary
and__main__
. When I as a developer add aentry_point="__main__.py"
file under a Python package (__main__.py
in this case), its goal is to provide a standard entrypoint for the package, not entrypoint in itself as ifsub_project
was in__main__.py
(which is what namePYTHONPATH
for__main__
looks to me).pex_binary
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_pointcodecurved-television-6568
05/26/2022, 9:14 AMIf I want to makeYou 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 yourpackage into something that other subprojects can depend on, where do I declare that it “publishes” the sources under namesub_project
?sub_project
__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…curved-television-6568
05/26/2022, 9:16 AMsquare-pharmacist-60243
05/26/2022, 9:42 AMhappy-kitchen-89482
05/26/2022, 1:42 PMhappy-kitchen-89482
05/26/2022, 1:43 PMhappy-kitchen-89482
05/26/2022, 1:43 PMhappy-kitchen-89482
05/26/2022, 1:44 PMsrc/py/sub-project
as a source roothappy-kitchen-89482
05/26/2022, 1:45 PMimport sub_project