[Tutor] What is the best way for a program suite to know where it is installed?
robertvstepp at gmail.com
Mon Oct 22 20:26:46 EDT 2018
On Mon, Oct 22, 2018 at 9:47 AM Mats Wichmann <mats at wichmann.us> wrote:
> On 10/20/18 9:00 PM, boB Stepp wrote:
> > So far the best method I've come up with is to make use of "__file__"
> > for the initiating program file. But from past discussions I am not
> > certain this is the *best* way. Is the *best* way going to get me
> > into best techniques for installing and distributing programs? If so,
> > this strikes me as a huge mess to dive into!
> > TIA!
> As you've concluded by now, this isn't a completely trivial exercise,
> and you'll possibly get asked the question "why do you think you need to
> know that?".
Actually no one has yet asked me this question, and you have me
intrigued. Importing the various program modules/module contents is
no issue. Where I believe I need to know the paths to things are to
get to data folders, config files, and occasionally utility programs
that I have written that are on my hard drive, but not copied to my
current program suite. Unless there is some built-into-Python-easy
way to find such things, I do not see any other alternative than
determining the path to the desired files. Is there a better way?
> Probably though this is considered one of the more reliable ways:
> import os
> if __name__ == '__main__':
> script_path = os.path.dirname(os.path.realpath(__file__))
> print("Main module at", script_path)
Despite the past thread I mentioned in this thread's originating post,
I have switched to using pathlib.Path for doing this sort of thing.
Something I read on the main list in the past year I found convincing,
though I now don't remember now why I was so convinced.
More information about the Tutor