Package: poetry-multiproject-plugin
Poetry Multiproject PluginThis is a PythonPoetryplugin, adding thebuild-projectandcheck-projectcommands.Thebuild-projectcommand will make it possible to use relative package includes.
This feature is very useful for monorepos and when sharing code between projects.Thecheck-projectcommand is useful to check that dependencies are added properly in a project.
It uses theMyPytool under the hood, and will output any errors from the static type checker.Use casesUsageInstallationWhat does it do?How is it different from the "poetry build" command?Organizing codeUse casesMicroservices and appsThe main use case is to support having one or more microservices or apps in a Monorepo, and share code between the services with namespaced packages.
Thebuild-projectcommand will collect the project-specific packages and build an installable artifact from it (i.e. a wheel or an sdist).A basic building block for the Polylith ArchitectureThe Multiproject plugin makes it possible to organize Python projects according to the Polylith Architecture.
The plugin is the foundation for thePython tools for the Polylith Architecture- also implemented as aPoetryplugin.For more about Polylith, have a look at thePython-specific Polylith documentation.Libraries?Building libraries is also supported, but you will need to consider that the code will likely share the same top namespace with other libraries
built from the same monorepo. It depends on your monorepo structure. This will likely be a problem when more than one of your libraries are installed into the same virtual environment.Since Python libraries by default are installed in a "flat" folder structure, two libraries with the same top namespace will collide.There is a way to solve this issue, by using the--with-top-namespaceflag of thebuild-projectcommand. Seeusage for libraries.UsageNavigate to the project folder (where thepyproject.tomlfile is).Build a project:poetrybuild-projectCheck the code used in a project:poetrycheck-projectCheck the code, with a customMyPyconfiguration to override the defaults:poetrycheck-project--config-file<PATH-TO-MYPY.INI-CONFIG-FILE>Usage for librariesThebuild-projecthas a solution to the problem with top namespaces in libraries forPython 3.9and more.
You can choose a custom namespace to be used in the build process, by using the--with-top-namespaceflag.The command will organize the namespaced packages according to the custom top namespace, and more importantly, re-write the imports made in the actual source code.
The re-organizing and re-writing is performed on the relative includes.Thebuild-projectcommand, with a custom top namespace:poetrybuild-project--with-top-namespacemy_namespaceThe build outputDefault(no flag)/my_package__init__.pymy_module.pyNamespace(--with-top-namespace=my_namespace)my_namespace//my_package__init__.pymy_module.pyNamespace with path(--with-top-namespace=my_namespace/subdir)my_namespace//subdir/my_package__init__.pymy_module.pyAnd will re-write the relevant module(s):Default(no flag)frommy_packageimportmy_functionNamespace(--with-top-namespace=my_namespace)frommy_namespace.my_packageimportmy_functionNamespace with path(--with-top-namespace=my_namespace/subdir)frommy_namespace.subdir.my_packageimportmy_functionHow is this done?The code in this repo uses AST (Abstract Syntax Tree) parsing to modify source code.
The Python built-inastmodule is used to parse and un-parse Python code.InstallationThis plugin can be installed according to the officialPoetry docs.poetryselfaddpoetry-multiproject-pluginWhat does it do?thepoetry build-projectcommand will:copy the actual project into a temporary folder.collect relative includes - such asinclude = "foo/bar", from = "../../shared"-  and copy them into the temprary folder.generate a new pyproject.toml.run thepoetry buildcommand in the temporary folder.copy the builtdistfolder (containing the wheel and sdist) into the actual project folder.remove the temporary folder.thepoetry check-projectcommand will:copy the actual project into a temporary folder.collect relative includes - such asinclude = "foo/bar", from = "../../shared"-  and copy them into the temprary folder.generate a new pyproject.toml.runpoetry installin the temporary folder.runpoetry run mypyin the temporary folder.remove the temporary folder.The default setting for the underlyingMyPyconfiguration is:--explicit-package-bases--namespace-packages--no-error-summary--no-color-outputHow is it different from the "poetry build" command?Poetry does not allow package includes outside of theprojectroot.# Note the structure of the shared folder: namespace/packagepackages=[{include="my_namespace/my_package",from="../../shared"}{include="my_namespace/my_other_package",from="../../shared"}]This plugin will allow relative package includes. You will now be able to share code between projects.Organizing codeAn example Monorepo structure, having the shared code extracted into a separate folder structure:projects/my_app/pyproject.toml(includingselectedsharedpackages)my_service/pyproject.toml(includingselectedsharedpackages)shared/my_namespace/my_package/__init__.pycode.pymy_other_package/__init__.pycode.pyA suggested structure, usingPolylith:workspace/bases/components/development/projects/poetry.lockpyproject.tomlworkspace.tomlREADME.md