{"id":19951412,"url":"https://github.com/grimen/python-rootpath","last_synced_at":"2025-05-03T18:34:08.063Z","repository":{"id":48936162,"uuid":"158073568","full_name":"grimen/python-rootpath","owner":"grimen","description":"Python project/package root path detection.","archived":false,"fork":false,"pushed_at":"2022-09-09T11:03:01.000Z","size":32,"stargazers_count":23,"open_issues_count":7,"forks_count":14,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-09-12T22:15:09.348Z","etag":null,"topics":["auto-detect","autodetect","common","detect","package-root","package-root-path","project-root","project-root-path","python","root","root-path","rootpath","utlity"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/rootpath","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/grimen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"MIT-LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-11-18T10:47:08.000Z","updated_at":"2024-01-19T10:25:53.000Z","dependencies_parsed_at":"2022-09-08T11:32:25.672Z","dependency_job_id":null,"html_url":"https://github.com/grimen/python-rootpath","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grimen%2Fpython-rootpath","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grimen%2Fpython-rootpath/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grimen%2Fpython-rootpath/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grimen%2Fpython-rootpath/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/grimen","download_url":"https://codeload.github.com/grimen/python-rootpath/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224370068,"owners_count":17299969,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["auto-detect","autodetect","common","detect","package-root","package-root-path","project-root","project-root-path","python","root","root-path","rootpath","utlity"],"created_at":"2024-11-13T01:07:56.824Z","updated_at":"2024-11-13T01:07:57.569Z","avatar_url":"https://github.com/grimen.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# `rootpath` [![PyPI version](https://badge.fury.io/py/rootpath.svg)](https://badge.fury.io/py/rootpath) [![Build Status](https://travis-ci.com/grimen/python-rootpath.svg?branch=master)](https://travis-ci.com/grimen/python-rootpath) [![Coverage Status](https://codecov.io/gh/grimen/python-rootpath/branch/master/graph/badge.svg)](https://codecov.io/gh/grimen/python-rootpath)\n\n*Python project/package root path detection.*\n\n\n## Introduction\n\nAuto-magic project/package root path detection - from a child module file for Python libraries/projects.\n\nIt does this by detecting typical package/project root files/folders (e.g. `.git`, `requirements.txt`, etc.), but it can also be overriden easily if needed.\n\nAs a little bonus it exposes an optional helper for adding root path to the Python load path (`sys.path`) for resolving Python module import hell (which is terribly broken by design).\n\n\n## Install\n\nInstall using **pip**:\n\n```sh\npip install rootpath\n```\n\n\n## Use: Basic\n\nDetect a project/package root path:\n\n**1.** Assuming we have a **python** library/application project...\n\n```\n/home/me/projects\n    └── py-foo\n            └── foo\n                └── utils\n                    └── __init__.py\n                    └── baz.py\n                    └── say.py\n                └── __init__.py\n                └── bar.py\n            README.md\n            requirements.txt\n            setup.py\n```\n\n`foo/bar.py` - top level package module\n\n```python\nimport rootpath\n\ndef bar():\n    path = rootpath.detect()\n\n    assert path == '/home/me/projects/py-foo'\n\n    print('---')\n    print('FILE:', __file__)\n    print('ROOT:', path)\n    print('---')\n\nif __name__ == '__main__':\n    bar()\n```\n\n`foo/utils/baz.py` - nested level package module (dependency)\n\n```python\nimport rootpath\n\ndef baz():\n    path = rootpath.detect()\n\n    assert path == '/home/me/projects/py-foo'\n\n    print('---')\n    print('FILE:', __file__)\n    print('ROOT:', path)\n    print('---')\n\nif __name__ == '__main__':\n    baz()\n```\n\n`foo/utils/say.py` - nested level package module (dependency)\n\n```python\nimport rootpath\n\ndef say():\n    print('---')\n    print('SAY: {0}'.format(rootpath.detect()))\n    print('---')\n\nif __name__ == '__main__':\n    say()\n```\n\n**2.** Let's run the files individually - they should both with successful assertions and output accurately detected root paths...\n\n```sh\n$ cd /home/me/projects/py-foo\n\n$ python ./foo/bar.py\n\n---\nFILE: /home/me/projects/py-foo/foo/bar.py\nROOT: /home/me/projects/py-foo\n---\n\n$ python ./foo/utils/baz.py\n\n---\nFILE: /home/me/projects/py-foo/foo/utils/baz.py\nROOT: /home/me/projects/py-foo\n---\n\n$ python ./foo/utils/say.py\n\n---\nSAY: /home/me/projects/py-foo\n---\n\n```\n\n\n## Use: Painless Python module imports\n\nUsing the above example code project as a reference, as and example to enable painless Python module imports:\n\n**1.** Let's make use of the load path helper in the higher order modules...\n\n`foo/bar.py`\n\n```python\nimport rootpath\n\n# 1. prepends root path to `sys.path`\nrootpath.append()\n\n# 2. will import correctly without errors no matter if imported/executed from same path or any other system path - which is not true for the native Python 3 relative import\nimport foo.utils.say as say\n\ndef bar():\n    say()\n\nif __name__ == '__main__':\n    bar()\n```\n\n`foo/utils/baz.py`\n\n```python\nimport rootpath\n\n# 1. prepends root path to `sys.path`\nrootpath.append()\n\n# 2. will import correctly without errors no matter if imported/executed from same path or any other system path - which is not true for the native Python 3 relative import\nimport foo.utils.say as say\n\ndef baz():\n    hello()\n\nif __name__ == '__main__':\n    baz()\n```\n\n**2.** Let's run the files individually - `say` module should be imported correctly without any errors from any module path namespace...\n\n```sh\n$ cd /home/me/projects/py-foo\n\n$ python ./foo/bar.py\n\n---\nSAY: /home/me/projects/py-foo\n---\n\n$ python ./foo/utils/baz.py\n\n---\nSAY: /home/me/projects/py-foo\n---\n\n$ python ./foo/utils/say.py\n\n---\nSAY: /home/me/projects/py-foo\n---\n\n$ cd /home/me/projects/py-foo/foo\n\n$ python ./bar.py\n---\nSAY: /home/me/projects/py-foo\n---\n\n$ python ./utils/baz.py\n---\nSAY: /home/me/projects/py-foo\n---\n\n$ python ./utils/say.py\n\n---\nSAY: /home/me/projects/py-foo\n---\n\n$ cd /home/me/projects/py-foo/foo/utils\n\n$ python ./utils/baz.py\n\n---\nSAY: /home/me/projects/py-foo\n---\n\n$ python ./utils/say.py\n\n---\nSAY: /home/me/projects/py-foo\n---\n```\n\n## Change Root Detection Pattern\nAs stated earlier, the detection of the package/project's root directory\nis done by searching for typical package files (e.g. `.git`,\n`requirements.txt`, etc.).\n\nTo override this behavior and have `rootpath` use a different file for\nroot detection, set the `pattern` kwarg to the desired filename.\n\n### Example: Pipfile Root Detection\n```\nrootpath(pattern='Pipfile')\n```\n\n## About\n\nThis project was mainly initiated - in lack of well tested and reliable existing alternatives - to be used at our work at **[Markable.ai](https://markable.ai)** to have common code conventions between various programming environments where **Python** (research, CV, AI) is heavily used.\n\n\n## License\n\nReleased under the MIT license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgrimen%2Fpython-rootpath","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgrimen%2Fpython-rootpath","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgrimen%2Fpython-rootpath/lists"}