Skip to main content

Python project/package root path detection.

Project description

rootpath PyPI version Build Status Coverage Status

Python project/package root path detection.

Introduction

Auto-magic project/package root path detection - from a child module file for Python libraries/projects.

It 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.

As 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).

Install

Install using pip:

pip install rootpath

Use: Basic

Detect a project/package root path:

1. Assuming we have a python library/application project...

/home/me/projects
    └── py-foo
            └── foo
                └── utils
                    └── __init__.py
                    └── baz.py
                    └── say.py
                └── __init__.py
                └── bar.py
            README.md
            requirements.txt
            setup.py

foo/bar.py - top level package module

import rootpath

def bar():
    path = rootpath.detect()

    assert path == '/home/me/projects/py-foo'

    print('---')
    print('FILE:', __file__)
    print('ROOT:', path)
    print('---')

if __name__ == '__main__':
    bar()

foo/utils/baz.py - nested level package module (dependency)

import rootpath

def baz():
    path = rootpath.detect()

    assert path == '/home/me/projects/py-foo'

    print('---')
    print('FILE:', __file__)
    print('ROOT:', path)
    print('---')

if __name__ == '__main__':
    baz()

foo/utils/say.py - nested level package module (dependency)

import rootpath

def say():
    print('---')
    print('SAY: {0}'.format(rootpath.detect()))
    print('---')

if __name__ == '__main__':
    say()

2. Let's run the files individually - they should both with successful assertions and output accurately detected root paths...

$ cd /home/me/projects/py-foo

$ python ./foo/bar.py

---
FILE: /home/me/projects/py-foo/foo/bar.py
ROOT: /home/me/projects/py-foo
---

$ python ./foo/utils/baz.py

---
FILE: /home/me/projects/py-foo/foo/utils/baz.py
ROOT: /home/me/projects/py-foo
---

$ python ./foo/utils/say.py

---
SAY: /home/me/projects/py-foo
---

Use: Painless Python module imports

Using the above example code project as a reference, as and example to enable painless Python module imports:

1. Let's make use of the load path helper in the higher order modules...

foo/bar.py

import rootpath

# 1. prepends root path to `sys.path`
rootpath.append()

# 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
import rootpath.utils.say as say

def bar():
    say()

if __name__ == '__main__':
    bar()

foo/utils/baz.py

import rootpath

# 1. prepends root path to `sys.path`
rootpath.append()

# 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
import rootpath.utils.say as say

def baz():
    hello()

if __name__ == '__main__':
    baz()

2. Let's run the files individually - say module should be imported correctly without any errors from any module path namespace...

$ cd /home/me/projects/py-foo

$ python ./foo/bar.py

---
SAY: /home/me/projects/py-foo
---

$ python ./foo/utils/baz.py

---
SAY: /home/me/projects/py-foo
---

$ python ./foo/utils/say.py

---
SAY: /home/me/projects/py-foo
---

$ cd /home/me/projects/py-foo/foo

$ python ./bar.py
---
SAY: /home/me/projects/py-foo
---

$ python ./utils/baz.py
---
SAY: /home/me/projects/py-foo
---

$ python ./utils/say.py

---
SAY: /home/me/projects/py-foo
---

$ cd /home/me/projects/py-foo/foo/utils

$ python ./utils/baz.py

---
SAY: /home/me/projects/py-foo
---

$ python ./utils/say.py

---
SAY: /home/me/projects/py-foo
---

About

This project was mainly initiated - in lack of well tested and reliable existing alternatives - to be used at our work at Markable.ai to have common code conventions between various programming environments where Python (research, CV, AI) is heavily used.

License

Released under the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rootpath-0.1.1.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

rootpath-0.1.1-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

Details for the file rootpath-0.1.1.tar.gz.

File metadata

  • Download URL: rootpath-0.1.1.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.4.2 requests/2.20.1 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.5

File hashes

Hashes for rootpath-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ecc3f9de280ff11c6ade0e956229e4dcb74c1ead5efde8579ba721e6459c518f
MD5 316a106b7644b562ad3175397f4e327c
BLAKE2b-256 95e0b7876f1572456fac44210d53b30fe5250172d0300bed65ca23d577f7246c

See more details on using hashes here.

File details

Details for the file rootpath-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: rootpath-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 15.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.4.2 requests/2.20.1 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.5

File hashes

Hashes for rootpath-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 98f417747b3e25d2ad1e94a555d7562b48f642cbdf993dabc61259170720ed45
MD5 a915f6a6607c80f73ddd7706b505c384
BLAKE2b-256 4ff9959835686c78b7a95d8d806a97fa0be020c2deccb96de2b60659744319b9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page