Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine config file path in python?

I have a module that needs to initialize some settings by reading a config file. The directory structure looks something like this:

root\
    config\
        conf.cfg
    src\
        module1.py

I'm fine when I set the relative path to ../config/conf.cfg and run the module in its current directory. But when I import the module somewhere else and run it in another directory I run into problems.

How should I set the path so the module always looks in the same relative location (eg one directory up from where the module is located) and how do I ensure this works for other people that download my repo (who may not have root in the same place)?

like image 863
drbeans345 Avatar asked Oct 29 '22 17:10

drbeans345


1 Answers

os.path.realpath(__file__) 

will give you the path of the current file, resolving any symlinks in the path. This works fine on my machine.

like image 154
Manuel Taber Avatar answered Nov 15 '22 06:11

Manuel Taber