bazel-contrib / bazel-contrib/rules_python

pipy dependency generated py_library ignores .pyc files in installed package.

Đang mở
#2,178 6 bình luận 0 reaction 0 người được giao Xem trên GitHub
Can Close? help wanted
Ngôn ngữ chính
Starlark
Star
688
Fork
721
Merge trung bình
15 giờ 7 phút
Pull request đã merge (30 ngày)
76

Mô tả

# 🐞 bug report

### Affected Rule

https://github.com/bazelbuild/rules_python/blob/main/docs/pypi-dependencies.md
Specifically `py_library` with `name = "pkg"` generated by
```
pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
pip.parse(
hub_name = "my_deps",
python_version = "3.11",
requirements_lock = "//:requirements_lock_3_11.txt",
)
use_repo(pip, "my_deps")
```

### Is this a regression?

I don't know.

### Description

I couldn't import a module from pip package that ships python modules with .pyi and .pyc files.
For example, I have a example_package that ships with the following file structure
```
/example_package
__init__.py
foo.pyc
foo.pyi
```
According to https://peps.python.org/pep-3147/#flow-chart,
```
import foo from example_package
```
Should be successful. But the `py_library` generated specifically excludes `"**/*.pyc"` from `data`. If I manually remove the `"**/*.pyc"` exclusion, the import statement works.
```
py_library(
name = "pkg",
srcs = glob(
["site-packages/**/*.py"],
exclude=[],
# Empty sources are allowed to support wheels that don't have any
# pure-Python code, e.g. pymssql, which is written in Cython.
allow_empty = True,
),
data = [] + glob(
["site-packages/**/*"],
exclude=["**/* *", "**/*.py", "**/*.pyc", "**/*.pyc.*", "**/*.dist-info/RECORD"],
),
# This makes this directory a top-level in the python import
# search path for anything that depends on this.
imports = ["site-packages"],
deps = [],
tags = ["pypi_name=example_package", "pypi_version=3.21.0"],
visibility = ["//visibility:public"],
)
```

How
## 🔬 Minimal Reproduction

1. Creating a pip package following https://packaging.python.org/en/latest/ but replace the foo.py with compiled foo.pyc file.
2. Add pip dependency to a bazel workspace following guides in https://github.com/bazelbuild/rules_python/blob/main/docs/pypi-dependencies.md
3. In a py_library or py_binary, `import foo from example_package` should work but errors out with module not found error.

## 🔥 Exception or Error

```
ImportError: cannot import name 'foo' from 'example_package'
```

## 🌍 Your Environment

**Operating System:**

```
Linux
```

**Output of `bazel version`:**

  

Bazelisk version: v1.20.0
Build label: 7.0.2
Build target: @@//src/main/java/com/google/devtools/build/lib/bazel:BazelServer
Build time: Thu Jan 25 16:13:35 2024 (1706199215)
Build timestamp: 1706199215
Build timestamp as int: 1706199215

**Rules_python version:**

  

bazel_dep(name = "rules_python", version = "0.31.0")

**Anything else relevant?**

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Hướng nghiên cứu

Start with docs/pypi-dependencies.md and the pip.parse configuration shown there, then reproduce the generated py_library using a package containing foo.pyc and foo.pyi. Compare the generated data glob with the reported package contents and verify that importing foo from example_package succeeds under Bazel when the issue is fixed.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
build-system
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.