rules_python icon indicating copy to clipboard operation
rules_python copied to clipboard

Using strip_path_prefixes in py_wheel can lead to import errors when running the installed wheel

Open sfriedowitz opened this issue 2 years ago • 0 comments

🚀 feature request

Relevant Rules

This request pertains to the py_wheel rule.

Description

This is somewhat of a feature request, but I am also just seeking some guidance on any current workarounds to this issue.

I've encountered an issue when trying to use the py_wheel rule with the strip_path_prefixes argument. My repository has a directory structure along the lines of projects/python/namespace/..., and I am trying to distributed a wheel for the Python code within namespace. The Python source code is written with absolute imports from the workspace root, e.g., from projects.python.namespace import X, so that the imports align with the runfile structure for py_binary targets.

When creating a wheel for this package, it is desirable to use strip_path_prefixes to remove the top-level projects/python folders from the distribution. However, using this feature leads to broken wheels because the absolute imports in the source code do not align with the modified directory structure in the resulting wheel.

Describe the solution you'd like

I am curious about a feature that would allow for replacing the imports in the Python source code when using strip_path_prefixes. It seems like a flag along the lines of rewrite_imports = True/False could be passed down to wheelmaker.py to control some replacement logic.

Albeit, this may be a terrible idea because of potential pitfalls of source code string replacements, which is why I am also curious to hear suggestions for workarounds.

Describe alternatives you've considered

A potential workaround is to maintain the repository without such nested directories. So rather than having the module live at projects/python/namespace, we could just place the namespace folder at the root of the workspace. The strip_path_prefixes functionality would no longer be necessary, and Python imports would be aligned with the directory structure of the packaged code.

I would appreciate any other suggestions for workarounds, both in terms of functionality of the bazel rules as well as other patterns for how to structure our repository. I'd imagine that this is a common issue that people have run into when trying to package their Python code with py_wheel.

Thank you!

sfriedowitz avatar Jan 15 '24 16:01 sfriedowitz