aniruddha das
aniruddha das

Reputation: 1

Generate Markdown file without __init__.py in subfolders with MkDocstrings

So this is my project structure :

- packages
  --pkg1
  ---subpkg1
  ----wrkpkg1
  ------file1.py
  ----wrkpkg2
  ------file2.py
  ----__init__.py

mkdocs.yaml: plugins:

when I am doing mkdocs build am getting the following error ERROR - Could not collect 'packages.pkg1.subpkg1.wrkpkg1.file1'

Aborted with a BuildError!

ERROR - Could not collect 'packages.pkg1.subpkg1.wrkpkg2.file2'

Aborted with a BuildError!

I know the issue is with wrkpkg1 is not having init.py

My question is, is there any way to generate md files when I am having subfolders without init files, as in standard I will not be needing init in all my subfolders

I tried subpackages: true in mkdocs.yaml files, tried adding init file (it worked but having 20+ init files when I dont need to act them as a package is purely not a good structure)

I need to generate md files automatically and the subfolders should not be having the init files. My main folder subpkg1 is having the necessary init file

Upvotes: 0

Views: 85

Answers (1)

pawamoy
pawamoy

Reputation: 3806

My question is, is there any way to generate md files when I am having subfolders without init files, as in standard I will not be needing init in all my subfolders

I tried subpackages: true in mkdocs.yaml files, tried adding init file (it worked but having 20+ init files when I dont need to act them as a package is purely not a good structure)

It's definitely a correct structure though. The presence or absence of __init__.py modules in folders means something to Python. Without, your folder is a native namespace package:

Python 3.3 added implicit namespace packages from PEP 420. All that is required to create a native namespace package is that you just omit __init__.py from the namespace package directory.

While using native namespace packages in a parent package that is not a namespace package itself works in the simple case (you're only using this very package, and not the namespace feature), it will lead to issues as soon as you actually try to use the namespace feature: only the parent non-namespace package and its namespace children will be found, and other locations will be discarded.

Griffe, the tool that is used by mkdocstrings to extract Python API data from sources, chooses not to support such layouts to prevent future errors and confusion. It logs debug messages that tells you __init__ modules might be missing. These messages can be enabled with MkDocs' verbose option: mkdocs serve -v.

So the solution is to create these __init__.py modules 😄

Upvotes: 0

Related Questions