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

bulky-evening-62934

04/16/2020, 2:16 PM
Another question, python_binary could only accept 1 file in sources ?
ERROR: The 'sources' field in target XXXX:XXXX must have 0 or 1 files, but it had 2 files.
could I include more than 1 file for pants binary ?
h

hundreds-father-404

04/16/2020, 2:22 PM
Hi, for
binary
targets, if it uses more than 1 source file, you’ll want to create a
python_library
with the other files and include it in
dependencies
.
python_binary
should either have no source files or one, such as
app.py
or
main.py
. This allows Pants to infer what the
entry_point
is if you end up leaving off that field. For example
Copy code
# example/BUILD

# this will use the default sources of `*.py`
# and the default target name of `example:example`
python_library()

python_binary(
  name="app",
  dependencies=[':example'],
)
b

bulky-evening-62934

04/16/2020, 2:27 PM
got it, so for example, I could only put main.py in my binary and all other codes and resources in library ?
h

hundreds-father-404

04/16/2020, 2:28 PM
Exactly. More specifically, you’d put the other Python code in `python_library`(s) and you would put resource files in the
resources()
target
Btw, not sure if I told you this already, but you can run
./pants target-types
to get a list of all the targets and
./pants target-types --details='resources'
to see all of its fields, including which fields are required etc (If that doesn’t work, try
target-types2
)
👍 1
b

bulky-evening-62934

04/16/2020, 2:42 PM
Thanks, it works!
💯 1
h

happy-kitchen-89482

04/16/2020, 6:01 PM
You can also have the
main.py
in the
python_library
and have no sources at all in the
python_binary
(but you then must explicitly specify the
entry_point=
in the
python_binary
). If you have a single source in your
python_binary
then you can leave off the
entry_point=
because Pants will assume that the single source is the entry point module.
👍 2
5 Views