0

I would like to have a root path variable that I can use on kronus.py so I can correctly use the database file in my media folder.

I know i can use something like this:

ROOT_DIR = os.path.dirname(os.path.abspath(__file__))

But my question is, where should I put such code so I can then access my ROOT_DIR variable?

This is the project structure:

kronus
├── LICENSE
├── pyproject.toml
├── README.md
├── setup.cfg
├── media
│   └── database.db
├── src
│   └── kronus
│       ├── exception.py
│       ├── __init__.py
│       ├── kronus.py
│       ├── kronus_uteis.py
└── tests

And this is my setup.cfg file:

[metadata]
name = kronus
version = attr: kronus.__version__
author = attr: kronus.__author__
author_email = ran-n@tutanota.com
license = attr: kronus.__license__
description = Timestamp module
long_description = file: README.md
long_description_content_type = text/markdown
url = https://wwww.github.com/Ran-n/kronus
project_urls =
    Bug Tracker = https://wwww.github.com/Ran-n/kronus/issues
classifiers =
    Development Status :: 3 - Alpha
    Environment :: Console
    Intended Audience :: Developers
    License :: OSI Approved :: GNU General Public License v3 (GPLv3)
    Operating System :: OS Independent
    Programming Language :: Python :: 3.8
    Topic :: Software Development :: Libraries
    Topic :: Text Processing :: General
    Natural Language :: Galician
    Natural Language :: Portuguese
    Natural Language :: English
    Natural Language :: Spanish

[options]
python_requires = >= 3.10.1
package_dir =
    = src
packages = find:

[options.packages.find]
where = src
martineau
  • 119,623
  • 25
  • 170
  • 301
Ran
  • 77
  • 1
  • 8
  • You can't have two identically named subdirectories in the same parent directory (i.e. `kronus/src`). – martineau Feb 05 '22 at 20:52
  • @martineau Yeah sorry, my bad. Corrected it already – Ran Feb 05 '22 at 22:23
  • OK, that's better. As for your question, the usual place to create package-wide globals is to do so in the package's `__init__.py` file. That won't help your use the database file in the `media` folder however — perhaps it's misplaced. – martineau Feb 05 '22 at 23:50
  • 1
    In `kronus.py` you might be able to determine the path to the database file in the `media` folder by using relative paths like this (which uses [`pathlib.Path`](https://docs.python.org/3/library/pathlib.html#pathlib.Path)): `media_folder_path = (Path(__file__) / '../../media' ).resolve()` Afterwards the path to the database file would be `database_path = media_folder_path / 'database.db'`. – martineau Feb 06 '22 at 00:12
  • P.S. I do something like that in this accepted [answer](https://stackoverflow.com/a/69888310/355230) of mine. – martineau Feb 06 '22 at 00:17

0 Answers0