Skip to content

Make py_binary have zipped binary as implicit output #3530

Closed

Description

@kamalmarhubi

I'd like to be able to depend on the zipped python binary for distribution, but not build the zipped binary usually. To do this, I'd like to specify //path/to/my/py_binary_target.zip as a dependency of my distribution artifact.

At the moment, it's possible to build with --build_python_zip, but I'd rather not have to use different flags for ordinary builds and distribution builds.

Environment info

  • Operating System:

      $ lsb_release --description 
      Description:    Debian GNU/Linux testing (buster)
    
  • Bazel version (output of bazel info release): release 0.5.3

Have you found anything relevant by searching the web?

Found some info in https://bazel.build/designs/2016/09/05/build-python-on-windows.html

Activity

  1. added
    P2
    on Aug 10, 2017
  2. self-assigned this
    on Aug 10, 2017
  3. duggelz commented on Nov 17, 2017

    @duggelz

    An alternative would be to add an explicit attribute to py_binary/py_test rules that changes the behavior for that single target, from the current behavior (generated "py_binary_target" is a shell script that invokes the "py_binary_target.runfiles/" tree on Linux), to the "--build_python_zip" behavior ("py_binary_target" is a zip file with '#!" line).

  4. added
    P3
    team-Rules-Python
    and removed
    P2
    on Oct 19, 2018
  5. shs96c commented on Mar 26, 2019

    @shs96c
    Contributor

    The implicit output seems a lot more predictable: if you want the zip, ask for the zip. It also allows for faster iteration as bazel run won't require the building of a zip.

    As a final point, if anyone ever lands PEX support (#2038) then that could also be implemented as an implicit output, rather than Yet Another Parameter.

  6. brandjon commented on May 10, 2019

    @brandjon
    Member

    In considering the native rules' zip files vs subpar, I'm thinking we should aim to implement this and deprecate subpar. See google/subpar#44 for more discussion.

    It may also be worth renaming the .zip extension to .pyz after PEP 441.

  7. meteorcloudy commented on May 13, 2019

    @meteorcloudy
    Member

    Sounds good. But I guess our .zip file doesn't work exactly the way .pyz file work?

  8. brandjon commented on May 13, 2019

    @brandjon
    Member

    My understanding is that Python itself shouldn't care about the file extension, but using a different extension may affect file associations in the OS shell. While not all versions of Python would recognize .pyz under windows, you'd think it'd be no worse than leaving it as .zip where the default association would be some extractor tool.

    Pep 441 explicitly mentions that the zip format may be used with shebang lines and self-extracting archives, so I don't see a conflict.

  9. groodt commented on Sep 27, 2019

    @groodt
    Contributor

    Is there any update or movement here?

    It would be great to be able to reference:
    //path/to/my/py_binary_target.zip

    and have the executable zip consumed in other targets, such as rules_docker.

  10. meteorcloudy commented on Sep 30, 2019

    @meteorcloudy
    Member

    I think it's preferred to not have implicit output. How about we add it to a output group called python_zip_file, then you can access it by

    filegroup(
      name = "foo_zip",
      srcs=  ["//bar:foo"],
      output_group = "python_zip_file",
    )
    
  11. groodt commented on Oct 1, 2019

    @groodt
    Contributor

    This would be wonderful @meteorcloudy

    I'd be keen to try help implement this as well if necessary. Or is this something that you are already wishing to take on? I imagine this would need to be added to rules_python and not Bazel itself, however, I have no current knowledge of what --build_python_zip is doing behind the covers in the Bazel code.

  12. meteorcloudy commented on Oct 1, 2019

    @meteorcloudy
    Member

    @groodt I think this should be implemented in the native python rule, because that's what --build_python_zip applies to.
    I sent #9453, please take a look.

  13. brandjon commented on Oct 18, 2019

    @brandjon
    Member

    Thanks @meteorcloudy for the implementation!

  14. mzhaom commented on Oct 25, 2021

    @mzhaom
    Contributor

    I think it's preferred to not have implicit output. How about we add it to a output group called python_zip_file, then you can access it by

    @meteorcloudy , could you give more context why implicit output was not preferred? I really miss the behaviour provided by implicit output.

  15. meteorcloudy commented on Oct 26, 2021

    @meteorcloudy
    Member

    Implicit output requires the rule to always generate an action that outputs the declared file. But in some cases, that's not very nice. For example, when --build_python_zip is disabled, we'll have to generate a fake action for that. With output_group, we don't have that problem, if the flag is disabled, the output group will just be empty without throwing an error.

  16. mzhaom commented on Oct 26, 2021

    @mzhaom
    Contributor

    I see. Thank you @meteorcloudy. It sounds like it's not really that implicit output is bad but the flag controlled zip output plus implicit output it tricky. Why don't we just generate the implicit output unconditionally, probably under a different name like "name.pz"?

  17. meteorcloudy commented on Oct 27, 2021

    @meteorcloudy
    Member

    Why don't we just generate the implicit output unconditionally

    Because the python zip file includes all transitive runtime dependencies, sometimes it could take a long time to build (especially on Windows). I think there are some other disadvantages of implicitly outputs in general, @oquenchil anything you could add?

  18. oquenchil commented on Oct 27, 2021

    @oquenchil
    Contributor

    So it might be that implicit outputs stay in Starlark rules in some form. Not certain about this. The problem here is what Yun said, it is expensive to create the action for that artifact unconditionally, so unfortunately you will have to use the additional flag for distribution.

  19. shs96c commented on Oct 27, 2021

    @shs96c
    Contributor

    An implicit output is really handy when you're working on the command line and want to both run locally (regular target), and then build the zip for deployment elsewhere (the implicit target) Working with an output group is clumsier.

  20. meteorcloudy commented on Oct 27, 2021

    @meteorcloudy
    Member

    You can just define an extra target in the BUILD file:

    filegroup(
      name = "foo_zip",
      srcs=  ["//bar:foo"],
      output_group = "python_zip_file",
    )
    

    Then in the command line you only need to switch between bazel build //:foo and bazel build //:foo_zip, I don't think that's much harder than implicit outputs?

  21. shs96c commented on Oct 27, 2021

    @shs96c
    Contributor

    Indeed, but that is more verbose and clumsier than an implicit target, and also requires the author of the build files to know in advance whether or not all consumers of that file will want a zip or not (it's arguable that they should know, but in a large codebase that may not actually be true)

  22. mzhaom commented on Oct 27, 2021

    @mzhaom
    Contributor

    I miss the experince of building par with blaze, nothing needs to be added in BUILD files to switch between build :foo vs build :foo.par

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P3team-Rules-Pythontype: feature request

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions