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

Update documentation for utilizing py_library from nested folder #54

Closed
PranavSathy opened this issue Dec 24, 2017 · 5 comments
Closed
Assignees

Comments

@PranavSathy
Copy link

PranavSathy commented Dec 24, 2017

Hi Everyone,

My current folder structure is as follows:

WORKSPACE
analysis/
analysis/analysis.py
analysis/BUILD
analysis/analyzers/average.py
analysis/analyzers/BUILD

I am trying to import all the analyzers in my analysis.py file, and the two BUILD rules look as follows:

py_binary(
    name = "analysis",
    srcs = ["analysis.py"],
    deps = [
      "//analysis/analyzers",
      requirement("pandas"),
      requirement("plotly"),
    ],
)
py_library(
    name = "analyzers",
    srcs = ["average.py"],
    deps = [
       requirement("pandas"),
    ],
)

It seems like I have tried every way of attempting to import analyzers into my analysis.py file, including;

import analyzers
import analysis.analyzers
from analysis import analyzers
from analysis.analyzers import analyzers
from analysis.analyzers import average

along with those combinations with my WORKSPACE name.

Am I missing something obvious? thanks!

@PranavSathy
Copy link
Author

Ok, I figured it out, it seems that because the name of my py_binary is analysis as well as the name of the file analysis.py, the import statement in the file could not find the submodules (folders) within because it seems to have been interpretting the actual analysis.py file.

If I simple do import analysis in my analysis.py file, it actually executes twice.

Is there some rationale as to why py_binary/py_library do this? I can escalate this into the actual bazelbuild/bazel repo since this is behavior exhibited by the native rules.

@mattmoor
Copy link
Contributor

@ulfjack Who owns the native Python rules?

@ulfjack
Copy link

ulfjack commented Jan 11, 2018

lberki does, although we're planning on transitioning them to the python team this year. @duggelz

@duggelz
Copy link

duggelz commented Feb 16, 2018

This should be fixed once this change is released in Bazel: bazelbuild/bazel@e9c885a

You'll need to add legacy_create_init=false to your py_binary and py_library targets.

Meanwhile, my practical advice is to rename the directory or the file. You'll keep running into similar problems, even in non-Bazel contexts, because of how Python implicitly adds the main script directory to the start of the Python import path.

@brandjon
Copy link
Member

Regarding the Python behavior of adding the main script's directory to the import path, this is something we'd like to workaround to eliminate a class of hermeticity problems. See bazelbuild/bazel#7091.

alexeagle pushed a commit to alexeagle/rules_python that referenced this issue Aug 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants