Skip to content

[Python] flatc --python silently overwrites existing __init__.py files #9229

Open

Description

@d3r1n

flatc version: 25.12.19 (also older)
Generator: --python

What

flatc --python creates an empty __init__.py in every folder of the
namespace path. If one of those folders already has a hand-written
__init__.py with code in it, flatc replaces it with an empty file without warning.

Why

I wanted the generated files to live under my apps ipc folder which deals with the websocket server and the flatbuffer stuff

Repro

Project already has these real files:

src/myapp/__init__.py         # e.g. `def main(): ...`
src/myapp/ipc/__init__.py     # e.g. re-exports

schema/foo.fbs:

namespace myapp.ipc.generated;
table Thing { x: int; }

Run:

flatc --python -o src schema/foo.fbs

Now src/myapp/__init__.py and src/myapp/ipc/__init__.py are empty.
I found it happened through basedpyright lsp:
ImportError: cannot import name 'main' from 'myapp'.

Expected

flatc should not destroy source files it did not generate. Any of:

  • don't touch an __init__.py in a folder that already exists
  • never overwrite a non-empty __init__.py
  • only write __init__.py for the innermost namespace folder
  • add a flag like --python-no-init

Notes

  • --gen-onefile skips the __init__.py chain but doesn't emit
    cross-schema imports, so it breaks for schemas that use include. Not a
    workaround.
  • --ts is fine — it writes into a fresh tree and doesn't clobber
    hand-written files.
  • flatc --help (25.12.19) has no flag to control __init__.py output.

Workarounds

  1. Generate into a temp dir, move only the leaf namespace folder into place.
  2. Give the generated code its own top-level package, with a namespace
    prefix no hand-written module shares.

Activity

  1. added a commit that references this issue on Sep 9, 2026
    21f608b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions