Skip to content
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

[Bug]: Module not found (in this case pytest) #371

Closed
njlr opened this issue Jul 25, 2024 · 4 comments
Closed

[Bug]: Module not found (in this case pytest) #371

njlr opened this issue Jul 25, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@njlr
Copy link

njlr commented Jul 25, 2024

What happened?

I have a complex project but I will try to capture the import parts.

requirements.txt contains pytest, among other things:

pytest==7.3.1

My Bazel target imports pytest:

load("@aspect_rules_py//py:defs.bzl", "py_binary", "py_pytest_main", "py_test")
load("@pypi//:requirements.bzl", "requirement")

# ...

py_test(
  name = "tests",
  srcs = [
    "tests/**/*.py",
  ],
  main = "test.py",
  python_version = "3.10.6",
  deps = [
    requirement("pytest"),
  ],
)

WORKSPACE parses the requirements:

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

pip_parse(
  name = "pypi",
  requirements_lock = "//:requirements.txt",
  python_interpreter_target = python_3_10_6_interpreter,
)

load("@pypi//:requirements.bzl", pypi_install_deps = "install_deps")

pypi_install_deps()

By when I run the test, I get:

ModuleNotFoundError: No module named 'pytest'

Version

Development (host) and target OS/architectures:

Output of bazel --version: bazel 6.4.0

Version of the Aspect rules, or other relevant rules from your
WORKSPACE or MODULE.bazel file:

http_archive(
  name = "rules_python",
  sha256 = "d71d2c67e0bce986e1c5a7731b4693226867c45bfe0b7c5e0067228a536fc580",
  strip_prefix = "rules_python-0.29.0",
  url = "https://github.com/bazelbuild/rules_python/archive/refs/tags/0.29.0.tar.gz",
)
http_archive(
  name = "aspect_rules_py",
  sha256 = "04278ce23cc5c91a24b62ea00ac04c553fe40ca390943acf6684d367a681a871",
  strip_prefix = "rules_py-0.7.4",
  url = "https://github.com/aspect-build/rules_py/releases/download/v0.7.4/rules_py-v0.7.4.tar.gz",
)

Language(s) and/or frameworks involved: -

How to reproduce

See above

Any other information?

No response

@njlr njlr added the bug Something isn't working label Jul 25, 2024
@njlr
Copy link
Author

njlr commented Jul 25, 2024

It appears that the order of my toolchains was wrong.

This order works:

python_register_toolchains(
  name = "python3_10_6",
  python_version = "3.10.6",
  set_python_version_constraint = True,
)

load("@python3_10_6//:defs.bzl", python_3_10_6_interpreter = "interpreter")

# Default toolchain must go last
python_register_toolchains(
  name = "python3_9_16",
  python_version = "3.9.16",
)

load("@python3_9_16//:defs.bzl", python_interpreter = "interpreter")

Perhaps this needs a better error message?

@mattem
Copy link
Collaborator

mattem commented Aug 20, 2024

That error message is from Python itself, so I'm not sure what rules_py could have done here. However, the toolchain ordering looks like it might be related to #363

@njlr
Copy link
Author

njlr commented Aug 20, 2024

It's generally quite hard to know which toolchain is being used and if you have configured things incorrectly.

From the usage, I was under the impression that 3.10.6 had been selected:

py_test(
  name = "tests",
  srcs = [
    "tests/**/*.py",
  ],
  main = "test.py",
  python_version = "3.10.6", # <----- Set here
  deps = [
    requirement("pytest"),
  ],
)
pip_parse(
  name = "pypi",
  requirements_lock = "//:requirements.txt",
  python_interpreter_target = python_3_10_6_interpreter, # <----- Matches here
)

But it actually depends on the order of toolchain registration.

If you declare a py_binary / py_test target that depends on a PIP package whose Python toolchain doesn't match, I think there should be an error or at least a warning.

@njlr
Copy link
Author

njlr commented Aug 20, 2024

Duplicate of #363

@njlr njlr closed this as completed Aug 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants