Repository navigation
Install the mkdocs package out of upstream MkDocs' way - #115
Merged
Merged
Conversation
Almost every plugin and theme depends on the 'mkdocs' distribution, so installing one also installs upstream MkDocs into site-packages/mkdocs/, the same directory MkDocs NG installed to. Whichever was installed last won, and 'pip uninstall mkdocs' deleted most of MkDocs NG. The wheel now installs the package to site-packages/mkdocs_ng/mkdocs/. mkdocs_ng.pth runs a tiny bootstrap at interpreter startup that puts a meta path finder first, so 'import mkdocs' loads MkDocs NG in any install order, with module names, logger names and entry points unchanged. The 'mkdocs' console script also installs the finder itself, so the command works even where .pth files are skipped. Editable installs add the repository root to sys.path (hatch can't map the prefix rewrite), and the same hook keeps the checkout ahead of an installed upstream package, so the PYTHONPATH workarounds in the docs and integration environments are gone. Builtin themes registered by mkdocs-ng now count as builtin, and the same themes registered by upstream MkDocs are no longer reported as a conflict.
Contributor
EntryPoint.dist.name parses the distribution's metadata on every access, and get_themes() runs for every config load because Theme clears its cache. Counting mkdocs-ng's themes as builtin made the loop read the name twice more, slowing load_config by about 15% (CodSpeed: test_load_config 34.3 ms -> 39.8 ms). Read each name once.
Python 3.15 deprecates import lines in .pth files in favour of .start files that name entry points. With a matching mkdocs_ng.start, 3.15+ runs mkdocs_ng:install from it and suppresses the .pth import line (no deprecation notice under -v); older versions ignore the .start file and keep running the import line. Without it, 3.18+ would silently skip the hook.
2 of 3 tasks
This was referenced Nov 1, 2026
This was referenced Nov 1, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Almost every plugin and theme (Material for MkDocs included) depends on the
mkdocsdistribution. Installing one alongside MkDocs NG therefore also installs upstream MkDocs intosite-packages/mkdocs/, the same directory MkDocs NG installs to. Whichever package is written last wins, often silently: a plainpip install mkdocs-ng mkdocs-materialends up running upstream 1.6.1.pip uninstall mkdocs, which the README recommended, deletes most of MkDocs NG along with it.This PR fixes the root cause. MkDocs NG no longer shares files with upstream MkDocs. It supersedes #114, whose warning and pip workarounds aren't needed once the files are separate.
How it works
The wheel installs the package to
site-packages/mkdocs_ng/mkdocs/instead ofsite-packages/mkdocs/(hatchsourcesrewrite). The source tree is unchanged.mkdocs_ng.start(Python 3.15+, PEP 829) andmkdocs_ng.pth(older versions) run a tiny bootstrap (mkdocs_ng/__init__.py, ~80 lines, stdlib only) at interpreter startup. It inserts a meta path finder at the front ofsys.meta_paththat resolves the top-levelmkdocspackage frommkdocs_ng/. Submodules then resolve throughmkdocs.__path__, so they are never mixed with upstream files.The
mkdocsconsole script now points tomkdocs_ng:main, which installs the finder itself. The command therefore works even where.pthfiles are skipped. If upstream's launcher was written last, it runsfrom mkdocs.__main__ import cli, which the hook routes to MkDocs NG.The module name stays
mkdocs, somkdocs.plugins, logger names (mkdocs.plugins.*, which strict mode counts), class__module__s,mkdocs.yml, themkdocscommand and themkdocs.plugins/mkdocs.themesentry point groups are all unchanged. There is nothing to migrate for users or plugin authors.Builtin themes:
get_themes()only counted themes registered by themkdocsdistribution as builtin. It now also countsmkdocs-ng's, so a third-party package can't silently replacemkdocs/readthedocswhen upstream isn't installed. The same themes registered by both distributions are no longer reported as a duplicate.Editable installs: hatch can't map a prefix rewrite in dev mode, so
dev-mode-dirs = ["."]adds the repo root tosys.path.mkdocs_ng.pthis force-included in the editable wheel as well, and the bootstrap finds<repo>/mkdocs. The checkout therefore wins over an installed upstream package, and thePYTHONPATH = "{root}"workarounds in thedocsandintegrationhatch environments are removed.Startup cost of the
.pthhook is about 0.4 ms (-X importtime). It only looks upPathFinderonsys.meta_pathwhenmkdocsis actually imported, so it doesn't importimportlib.machineryorwarningsat startup.Python 3.15+ (PEP 829): 3.15 deprecates
importlines in.pthfiles, and 3.18 will ignore them silently. With the matchingmkdocs_ng.start(mkdocs_ng:install), 3.15.0 reports "import lines in mkdocs_ng.pth are suppressed due to matching mkdocs_ng.start file" and runs the entry point instead. Older versions ignore the.startfile. The.pthkeeps the path line plus the PEP's recommended straddling import line.Precedent: setuptools ships the same mechanism,
distutils-precedence.pth+_distutils_hack.DistutilsMetaFinder, to makeimport distutilsload its own copy.Why not a ProperDocs-style rename
ProperDocs ships a
properdocspackage plus aproperdocscommand, and aliasesmkdocs.*imports only inside its own CLI process. To leave upstream's files alone it has to drop themkdocscommand, becausebin/mkdocsis also a shared file. That breaks every CI script, Makefile and tutorial.import mkdocsin user scripts also still gets upstream. Here themkdocslauncher may be shared because both launchers run the same code, the import hook covers every process, and nothing gets renamed.Before / After
Same Material +
search+minifysite withstrict: true, fresh venv, pip. Before =mkdocs-ng==1.8.0from PyPI; after = a wheel built from this branch (version bumped to2.0.0.dev0so upgrades can be tested).Before:
After:
Behavior
pip install mkdocs-ng mkdocs-materialimport mkdocs/mkdocs.build()in a user scriptBasePlugincomes frommkdocs_ng/mkdocs/plugins.pypip uninstall mkdocsmkdocsandpython -m mkdocsbrokenpython -m mkdocsworks; only the sharedmkdocslauncher is removed (pip install --force-reinstall --no-deps mkdocs-ngrestores it)pip uninstall mkdocs && pip install mkdocs-ng, breaks on the next plugin installpip install -U mkdocs-ngpip install -U mkdocs-ng→ MkDocs NG runs from the new locationPYTHONPATH={root}Verification
Unit tests (
mkdocs/tests/mkdocs_ng_tests.py, 11 tests, plus 2 inutils_tests.py):_find_roothandles the wheel layout and the source checkout, and never falls back to upstream'ssite-packages/mkdocs/.mkdocs.install()is idempotent.SitePackagesTestsstart a freshpython -I -Son a fake site-packages that contains both packages. Withsite.addsitedir,import mkdocsgives MkDocs NG,mkdocs.pluginsloads frommkdocs_ng/mkdocs/, an upstream-only module can't be imported (no mixing), upstream's launcher (from mkdocs.__main__ import cli) runs MkDocs NG,mkdocs_ng.main()works without.pthprocessing, and it works without upstream installed.mkdocs_ng.pth, the two key tests fail, as they should.Full suite and checks:
.pth, the subprocess tests still pass: the.startfile alone drives the hook. The new tests andutils_testsalso pass on PyPy 3.11.uvx prek run -apasses, andhatch run types:check(nowmypy mkdocs mkdocs_ng) passes.hatch run integration.py3.12-default:testpasses without thePYTHONPATHworkaround.hatch run docs:mkdocs build --strictpasses. In that environment upstream 1.6.1 is installed, andmkdocs --versionreports the checkout.mkdocs_ng/mkdocs/...,mkdocs_ng/__init__.pyandmkdocs_ng.pthin the wheel, and fails if the wheel contains a top-levelmkdocs/. I ran it locally againstuv buildoutput.End to end with real installers (fresh venvs, the site above with
--strict):mkdocs.build()from a script;python -m mkdocskeeps working, reinstalling MkDocs NG restores the launcher);mkdocs+ Material + minify withpip install -U;mkdocs-ng==1.8.0with plugins (before the upgrade it was running upstream 1.6.1);mkdocs-ng==1.8.0alone.override-dependencies, anduvx --overrides.mkdocs serve(page and Material CSS served),mkdocs new,-t readthedocs,multiprocessingspawn children,python -I, and uninstallingmkdocs-ng(upstream works again, no leftovers).Follow-up commits
5d87976: CodSpeed flaggedtest_load_config(34.3 → 39.8 ms, −13.75%), and I reproduced it locally (min 1.69 → 1.98 ms). Root cause:Themeclearsget_themes()'s cache on every config load, andEntryPoint.dist.namere-parses METADATA on every access. Countingmkdocs-ng's themes as builtin doubled those parses. Each name is now read once, and timing is back to main (min 1.69 ms vs 1.69 ms).00ca6d8: addedmkdocs_ng.startfor Python 3.15+ (see above).Docs
mkdocspackage. It explains why upstream MkDocs gets installed and why that's harmless, says to leave it installed (and how to restore the command if it was uninstalled), adds an optional Keeping upstream MkDocs out (uvoverride-dependencies/--overrides, tested), and Switching from upstream MkDocs (justpip install -U mkdocs-ng). Themkdocs --versionexample shows the new path.pip install -U mkdocs-ng.Limitations
import mkdocsfrom your own code relies on Python processing.pthfiles, which it does by default (including-I). Themkdocscommand doesn't rely on it. Where.pthfiles are skipped (python -S,pip install --targetdirs added viaPYTHONPATH, some bundlers), a script'simport mkdocscan find upstream instead. The docs note this.pip uninstall mkdocsstill removes themkdocslauncher, because both distributions recordbin/mkdocs. The package is unaffected, and the docs give the one-line restore.mkdocsto upstream's sources, while runtime uses MkDocs NG. With only MkDocs NG installed, mypy finds it through the.pthpath line. This only matters to plugin authors; uv's override keeps upstream out.