Skip to main content

Git root to sys.path

A small package that will allow running python scripts (which refer to each other) from any level of the git directory hierarchy.

This can be useful for debugging.

Comment: The root of a submodule is the git root also.

Why

Suppose our project contains many modules and packages in a hierarchical structure like this.

── src
    ├── pk1
    │   └── mod1  
    ├── pk2
    │   ├── mod2
    │   └── mod3
    └── submodule_pk
        ├── mod4
        └── mod5

In each module, we want to refer to other parts of the program via the full path in the git repository.

So something like this in the main repository (refers to main repository):

from src.pk1.mod1 import xyz

or in the main repository (refers to submodule):

from src.submodule_pk.mod4 import qwe

And also in submodule we want to refer to other parts of the submodule like this:

from mod4 import qwe

Because the submodule is in another context (from its point of view) the main repository also.


We also want to have test code close to what is being tested.

For example in mod3.py file (mod3 module):

from src.submodule_pk.mod4 import qwe
from src.pk1.mod1 import xyz

class M:
    def __init()__:
        ...
   
   def a() -> str:
        ...    
...

if __name__ == "__main__":
   
    # for simple test ....
   
    print(M().a())

And he also wants modules to be able to run as standalone scripts from anywhere.

cd src/pk2
python3 mod3.py

Or so that we can simply run them from a development environment (such as PyCharm, VC, etc.) by pressing the "Run" button.

Instalation

Pick one

pip install git+https://gitlab.com/ivo-marvan/git_root_to_syspath
pip install git_root_to_syspath

Usage

Put this line in the header of each file:

from git_root_to_syspath import agr; agr()

Or optionally:

from git_root_to_syspath import agr; PROJECT_ROOT = agr()

Metadata

Release files for git-root-to-syspath 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for git-root-to-syspath 0.1
File Size Uploaded
git_root_to_syspath-0.1.tar.gz 3.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for git-root-to-syspath 0.1
File Interpreter ABI Platform
git_root_to_syspath-0.1-py3-none-any.whl Python 3 none any Details

Total release size: 7.7 kB

Release files / git_root_to_syspath-0.1.tar.gz

Download URL git_root_to_syspath-0.1.tar.gz
Size 3.5 kB
Tags Source
SHA-256 checksum
How to use checksums
9344d3d814804ef0583e7e4221a96429281cf44b3d82c13036ddb648ad2e4a80
BLAKE2b-256 checksum
How to use checksums
c18b1292abe125cea09d6af16fe0e9e35c7337592bc90913af54d615af6e3e65
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.11.4 pkginfo/1.7.0 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.2

Release files / git_root_to_syspath-0.1-py3-none-any.whl

Download URL git_root_to_syspath-0.1-py3-none-any.whl
Size 4.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
799c0a4e682a308a2689ee0f71d9a5b37f5f0028e3613321e3c161814a66f528
BLAKE2b-256 checksum
How to use checksums
e724b21dc119ad079609bdb75dbddb54702549dedc2491285184084cb6e1b8d6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.11.4 pkginfo/1.7.0 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.2

Release history Release notifications | RSS feed

This release

0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page