Correct idiom for determining path when frozen in 3.4

Devin Jeanpierre jeanpierreda at
Mon Mar 17 10:16:47 CET 2014

On Mon, Mar 17, 2014 at 2:02 AM, Ben Finney <ben+python at> wrote:
> Mark Summerfield <list at> writes:
>     if getattr(sys, "frozen"):    # ‘getattr’ will return None by default

No it won't.

> 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)

Sure, but sys.executable always exists. sys.frozen doesn't, and the
existence or nonexistence is apparently meaningful; so your code does
something different than the original problem statement.

Also, if that weren't the case, I'd really replace that try-except
with getattr(sys, 'executable', __file__)

-- Devin

More information about the Python-list mailing list