I have a program that relies on the __file__
built-in global to provide the path to data associated with the program, as in path_to_stuff = os.path.join(os.path.dirname(__file__),'stuff')
. The problem is that when I run this with python -m cProfile myprog
, the value of __file__
is no longer the path to myprog
but rather (apparently) the path to the cProfile module, where my stuff definitely isn't.
I've read the manual and searched here and don't see anything about this. Is there a way to either (a) get cProfile to leave __file__
alone or (b) know at runtime that I'm running under cProfile so I could initialize the path with a literal string in this special case?
Edit: or, I guess, (c), is there a better way to find a dir that will always be right next to the program.py?