{"id":22903977,"url":"https://github.com/json2d/dotrelay","last_synced_at":"2025-10-14T04:29:56.047Z","repository":{"id":57423735,"uuid":"339213022","full_name":"json2d/dotrelay","owner":"json2d","description":"a utility library for declaring relative imports in Python","archived":false,"fork":false,"pushed_at":"2021-09-24T17:14:16.000Z","size":31,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-25T07:23:42.803Z","etag":null,"topics":["boilerplate-reduction","module-import","workflow"],"latest_commit_sha":null,"homepage":"","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/json2d.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-02-15T21:33:26.000Z","updated_at":"2021-09-24T17:14:07.000Z","dependencies_parsed_at":"2022-08-30T03:50:12.803Z","dependency_job_id":null,"html_url":"https://github.com/json2d/dotrelay","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/json2d/dotrelay","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/json2d%2Fdotrelay","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/json2d%2Fdotrelay/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/json2d%2Fdotrelay/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/json2d%2Fdotrelay/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/json2d","download_url":"https://codeload.github.com/json2d/dotrelay/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/json2d%2Fdotrelay/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279017943,"owners_count":26086213,"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","status":"online","status_checked_at":"2025-10-14T02:00:06.444Z","response_time":60,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["boilerplate-reduction","module-import","workflow"],"created_at":"2024-12-14T02:39:39.947Z","updated_at":"2025-10-14T04:29:56.031Z","avatar_url":"https://github.com/json2d.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 📡 dotrelay\n[![PyPI version](https://badge.fury.io/py/dotrelay.svg)](https://badge.fury.io/py/dotrelay)\n[![Build Status](https://travis-ci.com/json2d/dotrelay.svg?branch=main)](https://travis-ci.com/json2d/dotrelay) [![Coverage Status](https://coveralls.io/repos/github/json2d/dotrelay/badge.svg?branch=main)](https://coveralls.io/github/json2d/dotrelay?branch=main)\n\na utility library for declaring relative imports in Python\n\n## Quick install\n```bash\npip install dotrelay\n```\n\n## Basic usage\nout-of-the-box here's how to use `dotrelay` to import a module from an ancestor directory containing a `.relay` file:\n\n```py\nimport dotrelay\n\nwith dotrelay.Radio(__file__): # 📻\n  import some_relatively_external_module\n```\n\n## Problem\n\nimporting _relatively external_ modules is hard (in Python 🐍)\n\ndon't believe? just check out this 10+ years of discussion on the internet:\n- https://stackoverflow.com/questions/6323860/sibling-package-imports\n\n\nso forget about importing modules from _another galaxy_:\n\n```\n.\n├── andromeda\n│   └── ufos.py -- 🛸🛸🛸\n└── milky_way\n    └── sol\n        └── earth\n            ├── animals\n            │   ├── __init__.py\n            │   ├── birds.py\n            │   └── fish.py\n            ├── lands\n            │   ├── __init__.py\n            │   └── deserts.py\n            └── waters\n                ├── __init__.py\n                └── oceans.py\n```\n\nin order to import `ufos` into `deserts` you'd need this bit of boilerplate:\n\n```py\n# deserts.py\nimport sys\nimport os\n\n# get directory path containing `andromeda` (relatively from this module's file path)\nroot_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__) ) ) ) ) ) \n\nsys.path.append(root_path) # extend module import context \nfrom andromeda import ufos # import that thing we need\nsys.path.remove(root_path) # cleanup \n\n# finally the real work can begin\ndef lose_cattle_mysteriously(cattle):\n  ufos.abduct_cattle(cattle, mode='random')\n\n```\n\ncommonly referred to as a \"`sys.path` hack\", this is what we want to provide a better alternative for - it's fairly low level, fairly ugly, noisy and just plain makes the code smelly 👃🏽\n\n\n\n## Solution\n\nso let's make this better - we have the technology!\n\nfor starters let's create a `.relay` file in the directory containing `andromeda`, the module we want to import into `oceans`\n\n\u003e **_NOTE:_* this `.relay` file must be in one of `oceans` ancestor directories to be discoverable\n\n```\n.\n├── .relay -- 📡\n├── andromeda\n│   └── ufos.py -- 🛸🛸🛸\n└── milky_way\n    └── sol\n        └── earth\n            ├── animals\n            │   ├── __init__.py\n            │   ├── birds.py\n            │   └── fish.py\n            ├── lands\n            │   ├── __init__.py\n            │   └── deserts.py\n            └── waters\n                ├── __init__.py\n                └── oceans.py\n```\n\nnow in `oceans` we can use a `dotrelay.Radio` to _discover the `.relay` file above it_ and _establish a kind of temporary bridge_ for us to import `andromeda` and/or other modules in the relay directory\n\n```py\n# deserts.py\nimport dotrelay\nwith dotrelay.Radio(__file__): # 📻\n  from andromeda import ufos\n\ndef lose_cattle_mysteriously(cattle):\n  ufos.abduct_cattle(cattle, mode='psuedo-random') # yes it happened\n```\n\nnow the boilerplate has been reduced to something fairly high level, fairly clean, short and sweet\n\n## Common scenarios\nfun example aside, lets see how this fits into real world scenarios\n### Testing modules\nso here's a typical file structure for most python lib projects where there's the main module and some test modules\n\n```\n.\n├── pything\n│   ├── __init__.py\n│   └── main.py\n└── tests\n    └── units.py\n```\n\nin order to test `pything` it needs to be imported into `units`, and you end up with more of that \"`sys.path` hack\" bloat:\n\n```py\n# units.py\nimport sys\nimport os\nroot_path = os.path.dirname( os.path.dirname( path.abspath(__file__) ) ) # the directory that contains pything\nsys.path.append(root_path)\n\nimport pything\n\nsys.path.remove(root_path) # cleanup\n\nimport unittest\n\n# ...\n```\n\nan awkward thing to have to include in every single test module\n\nwith `dotrelay` we simply add the `.relay` file:\n\n```\n.\n├── .relay -- 📡\n├── pything\n│   ├── __init__.py\n│   └── main.py\n└── tests\n    └── units.py\n```\n\nand the boilerplate is reduced to:\n\n```py\n# tests/units.py\nimport dotrelay\nwith dotrelay.Radio(__file__): # 📻\n  import pything\n```\n\n### Organizing modules\n\nbuilding off the previous example, say `units` were to be moved deeper into the project file structure:\n\n```\n.\n├── .relay -- 📡\n├── pything\n│   ├── __init__.py\n│   └── main.py\n└── tests\n    └── basic\n        └── units.py\n```\n\nwith a \"`sys.path` hack\" the code for getting the `root_path` would need to be updated since again it's relative to the module's own file path\n\nso really then, overtime, as a project matures, this hack becomes something that needs to be manage. \n\nbut that can all be avoided with `dotrelay`. no changes need to be made as long as the `.relay` file remains with one of `units` ancestor directories\n\n### Reading static files\n\nsometimes it's also useful just having the path of the relay directory\n\n```\n.\n├── .relay -- 📡\n├── pything\n│   ├── __init__.py\n│   └── main.py\n└── fixtures\n│   └── data.json -- 📝\n└── tests\n    └── units.py\n```\n\nso to read `fixtures/data.json` from `units`:\n\n```py\n# tests/units.py\nimport dotrelay\nwith dotrelay.Radio(__file__) as rad: # 📻\n  ROOT_PATH = rad.relay_path\n\nimport os, json\nDATA_PATH = os.path.join(ROOT_PATH, 'fixtures', 'data.json')\nwith open(DATA_PATH, 'r') as fp: \n  DATA = json.load(fp)\n\nimport unittest\n# ...\n```\n\nechoing the point from the previous example, this feature is pretty usefule when you need to move the static files around in a project\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjson2d%2Fdotrelay","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjson2d%2Fdotrelay","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjson2d%2Fdotrelay/lists"}