{"id":9956,"library":"monorepo","title":"Monorepo Path Management Library","description":"The `monorepo` Python library (version 0.2.0) simplifies importing packages and modules from the root of a monorepo structure. It achieves this by programmatically adding the monorepo root to `sys.path`, allowing sub-packages to reference each other easily without complex relative imports. The library has an active release cadence, with the latest release in early 2024.","status":"active","version":"0.2.0","language":"en","source_language":"en","source_url":"https://github.com/myleott/monorepo","tags":["monorepo","imports","path management","development tools","sys.path"],"install":[{"cmd":"pip install monorepo","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"Monorepo","correct":"from monorepo import Monorepo"}],"quickstart":{"code":"from pathlib import Path\nfrom monorepo import Monorepo\nimport sys\n\n# Simulate a monorepo structure for demonstration\n# In a real monorepo, __file__ would be inside a sub-package\n# For this example, let's create a dummy structure\n\n# Create a dummy monorepo root and a sub-package\nmonorepo_root = Path('./my_monorepo_root')\nmonorepo_root.mkdir(exist_ok=True)\n(monorepo_root / 'service_a').mkdir(exist_ok=True)\n(monorepo_root / 'service_b').mkdir(exist_ok=True)\n\n# Create a dummy module in service_b\n(monorepo_root / 'service_b' / '__init__.py').touch(exist_ok=True)\n(monorepo_root / 'service_b' / 'my_module.py').write_text(\"MY_VALUE = 'Hello from service_b!'\")\n\n# Now, simulate being inside 'service_a/main.py'\n# The 'root' should point to 'my_monorepo_root'\nMonorepo.setup(root=monorepo_root.absolute())\n\n# Now you can import from other services/packages in the monorepo root\n# This would typically be 'from service_b import my_module'\n# We need to ensure 'service_b' is discoverable, which Monorepo.setup does.\n# If running from a different directory, adjust sys.path for the quickstart\n\ntry:\n    from service_b import my_module\n    print(f\"Successfully imported my_module: {my_module.MY_VALUE}\")\nexcept ModuleNotFoundError as e:\n    print(f\"Failed to import module: {e}\")\nfinally:\n    # Clean up dummy files\n    import shutil\n    if monorepo_root.exists():\n        shutil.rmtree(monorepo_root)","lang":"python","description":"This quickstart demonstrates how to initialize `monorepo` by calling `Monorepo.setup()` with the absolute path to your monorepo's root directory. Once set up, you can perform direct imports of packages located anywhere under the specified root, such as `from service_b import my_module`, regardless of the current working directory's depth. The example creates a temporary monorepo structure for demonstration."},"warnings":[{"fix":"Ensure `root` points to the absolute path of your monorepo's root directory, where all top-level packages/services are located. Use `Path(__file__).parent.parent.parent` or similar for robust relative pathing from within a sub-package.","message":"Incorrect `root` path specified for `Monorepo.setup()`.","severity":"gotcha","affected_versions":"All"},{"fix":"Be aware that `monorepo` modifies `sys.path`. If you're also using `PYTHONPATH` or other `sys.path.insert()` calls, carefully test the order of operations. `monorepo` inserts its path at the beginning of `sys.path`, giving it high precedence.","message":"Conflicts with existing `PYTHONPATH` environment variable or other programmatic `sys.path` modifications.","severity":"gotcha","affected_versions":"All"},{"fix":"IDEs like VS Code or PyCharm might not immediately pick up the `sys.path` modifications from `Monorepo.setup()` for static analysis. Configure your IDE's Python interpreter paths or source roots to include the monorepo root manually for correct linting and autocompletion.","message":"IDE/Linter issues with recognizing monorepo paths.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Ensure `Monorepo.setup(root=...)` is called early in your application's lifecycle, typically in `__init__.py` or the main entry point, and that the `root` argument correctly points to the absolute path of your monorepo's top-level directory.","cause":"`monorepo.setup()` was not called, or the `root` path was configured incorrectly, preventing Python from finding modules in your monorepo.","error":"ModuleNotFoundError: No module named 'my_monorepo_package'"},{"fix":"Provide the `root` argument with the absolute path to your monorepo's root directory, e.g., `Monorepo.setup(root=Path(__file__).parent.parent.parent)`.","cause":"The `root` argument was omitted when calling `Monorepo.setup()`, which is a mandatory parameter.","error":"TypeError: Monorepo.setup() missing 1 required keyword-only argument: 'root'"}]}