Repository navigation
Description
A minimal repro needs multiple files (also in the attachment funcsigs-repro.tar.gz for convenience):
WORKSPACE:
git_repository(
name = "io_bazel_rules_python",
remote = "https://github.com/bazelbuild/rules_python.git",
commit = "346b898e15e75f832b89e5da6a78ee79593237f0"
)
load("@io_bazel_rules_python//python:pip.bzl", "pip_import")
pip_import(
name = "requirements",
requirements = "//:requirements.txt",
)
load("@requirements//:requirements.bzl", "pip_install")
pip_install()
BUILD:
exports_files(["requirements.txt"])
load("@requirements//:requirements.bzl", "requirement")
py_binary(
name="run",
srcs=["run.py"],
deps=[
requirement('mock'),
],
)
requirements.txt:
mock==2.0.0
run.py:
import mock
Then to reproduce run:
$ bazel run //:run
INFO: Found 1 target...
Target //:run up-to-date:
bazel-bin/run
INFO: Elapsed time: 1.138s, Critical Path: 0.03s
INFO: Running command line: bazel-bin/run
Traceback (most recent call last):
File "/private/var/tmp/_bazel_nik/5a08842075e4d3de340bcb5074db6110/execroot/__main__/bazel-out/darwin_x86_64-fastbuild/bin/run.runfiles/__main__/run.py", line 1, in <module>
import mock
File "/private/var/tmp/_bazel_nik/5a08842075e4d3de340bcb5074db6110/execroot/__main__/bazel-out/darwin_x86_64-fastbuild/bin/run.runfiles/pypi__mock_2_0_0/mock/__init__.py", line 2, in <module>
import mock.mock as _mock
File "/private/var/tmp/_bazel_nik/5a08842075e4d3de340bcb5074db6110/execroot/__main__/bazel-out/darwin_x86_64-fastbuild/bin/run.runfiles/pypi__mock_2_0_0/mock/mock.py", line 80, in <module>
import funcsigs
ImportError: No module named funcsigs
ERROR: Non-zero return code '1' from command: Process exited with status 1.
Important: This doesn't reproduce if funcsigs is globally installed (which it will be on many Linuxes). So you may have to do the following:
$ virtualenv --no-site-packages venv
$ source venv/bin/activate
$ bazel run //:run
...
I can reproduce this with bazel 0.7 on OS X Sierra and Ubuntu 16.04. As far as I can tell, the funcsigs wheel gets pulled when requirements.txt is processed but when the Python dependencies for the build target are put together, it's somehow not included. I can see it's not in bazel-bin/run.runfiles, even though the other dependencies (six and pbr) are.
I did notice that the version specification at https://github.com/testing-cabal/mock/blob/master/requirements.txt#L1 is a bit funky (funcsigs>=1;python_version<"3.3"). Maybe it's failing to parse that correctly?
Activity
nikhaldi commentedon Dec 5, 2017
Oh wow, after I wrote this I discovered there is a test which explicitly asserts the existence of this bug: https://github.com/bazelbuild/rules_python/blob/master/rules_python/whl_test.py#L63-L64
Not sure what the motivation was here. I guess it this makes sure that things work correctly on Python 3.3+ but it happens to be broken at lower versions.
nikhaldi commentedon Dec 5, 2017
And I now understand that any support for Python version restriction is missing: https://github.com/bazelbuild/rules_python/blob/master/rules_python/whl.py#L89-L93
I'm happy to try to provide a fix, this seems like quite an important feature. Any ideas how to best approach this welcome though. It seems there must be some piece of code maybe in wheel that can interpret the version restrictions.
mattmoor commentedon Dec 5, 2017
Yeah I think there were two main reasons I didn't want to handle this when I originally put this together.
There is probably a lib somewhere in pip/wheel for the latter, but I still don't know about the former.
@duggelz FYI
nikhaldi commentedon Dec 5, 2017
For 2 the correct answer appears to be
pkg_resources.evaluate_marker()(https://github.com/pypa/setuptools/blob/b2e99f6731c6ba6fe6bd60f79f2166640f3db21d/pkg_resources/__init__.py#L1449).The underlying
Markerparser does allow passing in a customized environment, so we can override the version of the Python interpreter that's executing right now, if need be. But I'm not sure I understand this issue. Is the Python interpreter building a Python target ever expected to be different from the one that will run the built target? I don't see how that would even work.Evaluate PEP 508 environment markers for package dependencies
Evaluate PEP 508 environment markers for package dependencies
nikhaldi commentedon Dec 8, 2017
@mattmoor I tested my fix a bunch, as far as I can tell it works.
Can you elaborate on your concerns about the intended Python version? Maybe you are referring to the scenario where you're building packages/binaries (e.g., pars) on one machine that are supposed to be portable to other machines maybe with a different interpreter?
First, I think this only works if you have no dependencies with C extensions. If you are building portable executables you have to very consciously choose dependencies to be pure-Python and you have to make explicit decisions about anything that depends on specific Python versions. I seems if you're doing that you're probably not in conflict with my fix, as you've already made those explicit choices in your requirements file.
mattmoor commentedon Dec 9, 2017
I don't feel like I'm the right person to make a judgement call on this. @duggelz is probably significantly better qualified, so I'll defer to his wisdom here.
I will leave this flagged and try to review it next week, apologies for the delay (I was at Kubecon last week).
Evaluate PEP 508 environment markers for package dependencies
duggelz commentedon Dec 15, 2017
It's actually common for the tools to run under a different Python version than the result .par file runs under. Right now, for example, Bazel will often/always run tools under Python2 even when the resulting .par file is intended to run under Python3. Even when that's fixed, cross compile, remote execution, and iOS/Android scenarios need to be handled somehow.
duggelz commentedon Dec 15, 2017
Medium term, Bazel will be adding Python toolchains. I'm not sure what the short-term answer is.
nikhaldi commentedon Dec 15, 2017
@duggelz ok, that makes sense.
To make sure I got the ground rules correct, I see essentially two cases:
I think it's clear that in the future there will have to be some way to target a specific Python version/platform. Once there is support for that, my change is easily adapted to use that environment when parsing environment markers.
hwright commentedon Dec 16, 2017
@duggelz I'm unconvinced, given the fact that things like google/containerregistry#42 exist. If anything, Bazel appears to be a bit schizophrenic about what python version it uses, because sometimes it uses the specified
py_runtimeto run internal tools (as above), but other times it won't (as in #33).I do agree that getting proper toolchain support would be nice (similar to what Go already uses, iiuc).
nikhaldi commentedon Jan 4, 2018
@duggelz @mattmoor #50 now addresses this in a way I think is reasonable. Any other thoughts?
Evaluate PEP 508 environment markers for package dependencies (#50)
Update example with extra_pip_args, pip_data_exclude and quite options (