I developed a stable path assignment a few years back.
The following file is always kept in the topmost source directory of a project, and symbolic links
are created in sub-source directories that point to this file.
Every module in the project imports this pathfile, thus allowing changes to be made in one place and immediately broadcast to every source file in the project.
I have never had an issue with relative files since I started using this method.
An additional benefit is that when a project is copied to a new computer, and I need to create the directory structure (with empty directories), I just run ProjectPaths.py and the directory structure is created
The following file is always kept in the topmost source directory of a project, and symbolic links
are created in sub-source directories that point to this file.
Every module in the project imports this pathfile, thus allowing changes to be made in one place and immediately broadcast to every source file in the project.
I have never had an issue with relative files since I started using this method.
An additional benefit is that when a project is copied to a new computer, and I need to create the directory structure (with empty directories), I just run ProjectPaths.py and the directory structure is created
import os from pathlib import Path class ProjectPaths(): ''' This file contains paths used throughout the system. ** WARNING ** -- Never modify this file without keeping a backup. Never modify this file if you're not absolutely sure how it works! After modification: **TEST**, **TEST**, and **TEST AGAIN** ''' def __init__(self): # Anchor Base Path -- Assures relative stability os.chdir(os.path.abspath(os.path.dirname(__file__))) self.homepath = Path('.') self.rootpath = self.homepath / '..' / '..' self.datapath = self.rootpath / 'data' self.datapath.mkdir(exist_ok=True) # ========================== National ========================== self.national_datapath = self.datapath / 'National' self.national_datapath.mkdir(exist_ok=True) self.natlgeopath = self.national_datapath / 'Geo' self.natlgeopath.mkdir(exist_ok=True) self.natlhtmlpath = self.national_datapath / 'html' self.natlhtmlpath.mkdir(exist_ok=True) self.natlbackuppath = self.national_datapath / 'backup' self.natlbackuppath.mkdir(exist_ok=True) self.natlcsvpath = self.national_datapath / 'csv' self.natlcsvpath.mkdir(exist_ok=True) self.natldatabasepath = self.national_datapath / 'database' self.natldatabasepath.mkdir(exist_ok=True) self.natlexcelpath = self.national_datapath / 'excel' self.natlexcelpath.mkdir(exist_ok=True) self.natlhtmlpath = self.national_datapath / 'html' self.natlhtmlpath.mkdir(exist_ok=True) self.natljsonpath = self.national_datapath / 'json' self.natljsonpath.mkdir(exist_ok=True) self.natllogpath = self.national_datapath / 'logs' self.natllogpath.mkdir(exist_ok=True) self.natlpdfpath = self.national_datapath / 'pdf' self.natlpdfpath.mkdir(exist_ok=True) self.natlprettypath = self.national_datapath / 'pretty' self.natlprettypath.mkdir(exist_ok=True) self.natlreportpath = self.national_datapath / 'reports' self.natlreportpath.mkdir(exist_ok=True) self.natltextpath = self.national_datapath / 'text' self.natltextpath.mkdir(exist_ok = True) self.natltmppath = self.national_datapath / 'tmp' self.natltmppath.mkdir(exist_ok=True) if __name__ == '__main__': ProjectPaths()