Code Monkey home page Code Monkey logo

bazel_python_namespace_bug's Introduction

Bazel Python Namespace Bug

This repo is documentation to explain this Bazel issue: bazelbuild/bazel#15263

This project was started as an attempt to use a python namespace package acme_corp in a monorepo managed by Bazel.

Here is the directory structure:

├── python_projects
│   └── acme_corp
│       ├── app1
│       │   ├── BUILD.bazel
│       │   ├── pyproject.toml
│       │   └── src
│       │       └── acme_corp
│       │           └── app1
│       │               ├── app1_module1.py
│       │               ├── app1_module2.py
│       │               └── __init__.py
│       └── lib1
│           ├── BUILD.bazel
│           ├── pyproject.toml
│           └── src
│               └── acme_corp
│                   └── lib1
│                       ├── __init__.py
│                       ├── lib1_module1.py
│                       └── lib1_module2.py
└── WORKSPACE.bazel

It is possible to run app1_module1 with the following command on Linux:

$ bazel run python_projects/acme_corp/app1:app1_module1
INFO: Analyzed target //python_projects/acme_corp/app1:app1_module1 (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //python_projects/acme_corp/app1:app1_module1 up-to-date:
  bazel-bin/python_projects/acme_corp/app1/app1_module1
INFO: Elapsed time: 0.365s, Critical Path: 0.01s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
3.7.10 (default, Mar 10 2021, 10:12:32)
[GCC 8.3.0]
app1_func1!
app1_func2!
lib1_func1!
lib1_func3!

This does not work on Windows, however.

Starting local Bazel server and connecting to it...
INFO: Analyzed target //python_projects/acme_corp/app1:app1_module1 (36 packages loaded, 179 targets configured).
INFO: Found 1 target...
Target //python_projects/acme_corp/app1:app1_module1 up-to-date:
  bazel-bin/python_projects/acme_corp/app1/app1_module1.exe
  bazel-bin/python_projects/acme_corp/app1/app1_module1.zip
INFO: Elapsed time: 7.861s, Critical Path: 0.47s
INFO: 6 processes: 5 internal, 1 local.
INFO: Build completed successfully, 6 total actions
INFO: Build completed successfully, 6 total actions
3.7.9 (tags/v3.7.9:13c94747c7, Aug 17 2020, 18:58:18) [MSC v.1900 64 bit (AMD64)]
Traceback (most recent call last):
  File "\\?\C:\Users\ASHLEY~1\AppData\Local\Temp\Bazel.runfiles_j5rp0o76\runfiles\acme_corp\python_projects\acme_corp\app1\src\acme_corp\app1\app1_module1.py", line 4, in <module>
    from acme_corp.app1.app1_module2 import app1_func2
ModuleNotFoundError: No module named 'acme_corp.app1'

This seems to be due to an extra __init__.py file that gets added to the zip file that bazel creates. When I extract the app_module.zip, I see this:

└───runfiles
    └───acme_corp
        │   __init__.py
        │
        └───python_projects
            └───acme_corp
                ├───app1
                │   └───src
                │       └───acme_corp
                │           └───app1
                │                   app1_module1.py
                │                   app1_module2.py
                │                   __init__.py
                │
                └───lib1
                    └───src
                        └───acme_corp
                            └───lib1
                                    lib1_module1.py
                                    lib1_module2.py
                                    __init__.py

If I delete the __init__.py file at runfiles/acme_corp/__init__.py and rezip the contents, the file runs like normal when I invoke it directly.

bazel-bin\python_projects\acme_corp\app1\app1_module1.exe
3.7.9 (tags/v3.7.9:13c94747c7, Aug 17 2020, 18:58:18) [MSC v.1900 64 bit (AMD64)]
app1_func1!
app1_func2!
lib1_func1!
lib1_func3!

bazel_python_namespace_bug's People

Contributors

1-900-mixalot avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.