Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Support python -m invocation style #2539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rickeylev opened this issue Dec 27, 2024 · 2 comments · Fixed by #2671
Closed

Support python -m invocation style #2539

rickeylev opened this issue Dec 27, 2024 · 2 comments · Fixed by #2671

Comments

@rickeylev
Copy link
Collaborator

Python supports two ways to start programs: by file, or by module name.

Today, only file is supported. This is a FR for -m to also be supported. There's two main use cases for this style:

  1. Using a file not in the same Bazel package as main.
  2. Specifying a main that comes from dependencies, and referencing the actual main file directly is problematic.

Some example use cases:

  • When using a macro to generate a test/binary.
  • pytest: a common main is used and the tests to execute are passed as arguments or discovered at runtime. This basically a special case of the above.

This should be fairly easy to implement using bootstrap script. It just requires passing the name of the main module to the stage2 bootstrap instead of the path to the main module.

Docs for -m: https://docs.python.org/3.10/using/cmdline.html#cmdoption-m

See also: runpy.run_module: https://docs.python.org/3.10/library/runpy.html#runpy.run_module

@aignas
Copy link
Collaborator

aignas commented Jan 3, 2025

A note for a possible cleanup - the py_console_script_binary could diructly leverage this feature. It could be also harvested for the initial implementation to support this.

github-merge-queue bot pushed a commit that referenced this issue Mar 19, 2025
…m) (#2671)

This implements the ability to run a module name instead of a file path,
aka
`python -m` style of invocation.

This allows a binary/test to specify what the main module is without
having to
have a direct dependency on the entry point file.

As a side effect, the `srcs` attribute is no longer required.

Fixes #2539
@aignas
Copy link
Collaborator

aignas commented Mar 19, 2025

This has made it possible to integrate pytest-bazel so much easier:

"""A small wrapper for pytest-bazel.

Taken from: https://pytest-bazel.readthedocs.io/latest/usage.html
"""

load("@rules_python//python:py_test.bzl", "py_test")

def py_pytest_test(*, deps = [], data = [], **kwargs):
    """Use pytest-bazel for running pytest tests

    Args:
        deps: Deps to pass to the test.
        data: The data to pass to the test rule.
        **kwargs: Extra args passed to the py_test.
    """

    py_test(
        deps = deps + [
            "@pypi//pytest_bazel",  # assuming your hub repo name is `pypi`.
        ],
        main_module = "pytest_bazel",
        data = data + [
            "//:pyproject.toml",
        ],
        **kwargs,
    )

See aignas/pytest-bazel#14

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants