Correct idiom for determining path when frozen in 3.4

Ben Finney ben+python at benfinney.id.au
Mon Mar 17 10:23:59 CET 2014


Devin Jeanpierre <jeanpierreda at gmail.com> writes:

> On Mon, Mar 17, 2014 at 2:02 AM, Ben Finney <ben+python at benfinney.id.au> wrote:
> > Mark Summerfield <list at qtrac.plus.com> writes:
> >     if getattr(sys, "frozen"):    # ‘getattr’ will return None by default
>
> No it won't.
> […]
> Sure, but sys.executable always exists.

My apologies for posting untested code without making that clear. Thanks
to Devin for picking up my mistakes.

> > Lastly, it's slightly more Pythonic to execute the normal path
> > unconditionally, and let it raise an exception if there's a problem::
> >
> >     try:
> >         executable = sys.executable
> >     except AttributeError:
> >         executable = __file__
> >     path = os.path.dirname(executable)

> sys.frozen doesn't [necessarily exist], and the existence or
> nonexistence is apparently meaningful; so your code does something
> different than the original problem statement.

Right. I didn't understand why ‘__file__’ is a suitable substitute for
‘sys.executable’; they're always (?) different values. So that probably
is what led to the confusion in the code behaviour.

I hope the Pythonic idioms are helpful to the original poster
nevertheless.

-- 
 \      “Anyone who believes exponential growth can go on forever in a |
  `\        finite world is either a madman or an economist.” —Kenneth |
_o__)                                                         Boulding |
Ben Finney




More information about the Python-list mailing list