Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #68415

Re: Correct idiom for determining path when frozen in 3.4

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.014
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'else:': 0.03; 'executable': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'try:': 0.09; 'suggest': 0.14; 'false):': 0.16; 'finney': 0.16; 'getattr(sys,': 0.16; 'grasp': 0.16; 'pythonic': 0.16; 'readable': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:when': 0.16; 'top-level': 0.16; 'exception': 0.16; 'module': 0.19; 'slightly': 0.19; 'header:User-Agent:1': 0.23; 'satisfying': 0.24; 'looks': 0.24; 'this:': 0.26; 'header:X -Complaints-To:1': 0.27; 'correct': 0.29; 'raise': 0.29; "i'm": 0.30; 'code': 0.31; 'getting': 0.31; 'writes:': 0.31; 'moment': 0.34; 'except': 0.35; 'test': 0.35; 'but': 0.35; 'really': 0.36; 'skip:o 20': 0.38; 'ben': 0.38; 'work?': 0.38; 'to:addr:python- list': 0.38; 'does': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; '8bit%:29': 0.60; 'is.': 0.60; 'skip:o 30': 0.61; "you're": 0.61; 'more': 0.64; 'skip:\xe2 10': 0.65; '8bit%:21': 0.69; 'default': 0.69; '8bit%:46': 0.78; '3.4': 0.84; 'idiom': 0.84; 'lastly,': 0.84; 'received:125': 0.84; 'universe': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ben Finney <ben+python@benfinney.id.au>
Subject Re: Correct idiom for determining path when frozen in 3.4
Date Mon, 17 Mar 2014 20:02:15 +1100
References <8a0ca549-7bce-4210-8f9e-3465a769fc3c@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host jigong.madmonks.org
X-Public-Key-ID 0xBD41714B
X-Public-Key-Fingerprint 9CFE 12B0 791A 4267 887F 520C B7AC 2E51 BD41 714B
X-Public-Key-URL http://www.benfinney.id.au/contact/bfinney-gpg.asc
X-Post-From Ben Finney <bignose+hates-spam@benfinney.id.au>
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux)
Cancel-Lock sha1:+WIO5JZQpHUazUeYeM00iXqYDro=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.8180.1395046954.18130.python-list@python.org> (permalink)
Lines 43
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1395046954 news.xs4all.nl 2913 [2001:888:2000:d::a6]:53066
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:68415

Show key headers only | View raw


Mark Summerfield <list@qtrac.plus.com> writes:

> What is the correct idiom for getting the path to a top-level module

I'm not sure I understand what this concept is. What do you mean by
“top-level module”?

> in 3.3 and 3.4 when the module might be frozen?
>
> At the moment I'm using this:
>
>     if getattr(sys, "frozen", False):
>         path = os.path.dirname(sys.executable)
>     else:
>         path = os.path.dirname(__file__)

That looks okay. Does it work?

The code is readable and Pythonic as is. But I would suggest several
improvements::

    if getattr(sys, "frozen"):    # ‘getattr’ will return None by default

Also, why test for “sys.frozen” when you're about to use
“sys.executable”?

    if getattr(sys, "executable"):

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)

-- 
 \     “It is far better to grasp the universe as it really is than to |
  `\    persist in delusion, however satisfying and reassuring.” —Carl |
_o__)                                                            Sagan |
Ben Finney

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Correct idiom for determining path when frozen in 3.4 Mark Summerfield <list@qtrac.plus.com> - 2014-03-17 01:44 -0700
  Re: Correct idiom for determining path when frozen in 3.4 Ben Finney <ben+python@benfinney.id.au> - 2014-03-17 20:02 +1100
  Re: Correct idiom for determining path when frozen in 3.4 Devin Jeanpierre <jeanpierreda@gmail.com> - 2014-03-17 02:16 -0700
  Re: Correct idiom for determining path when frozen in 3.4 Ben Finney <ben+python@benfinney.id.au> - 2014-03-17 20:23 +1100
  Re: Correct idiom for determining path when frozen in 3.4 Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2014-03-17 09:52 +0000
  Re: Correct idiom for determining path when frozen in 3.4 Mark Summerfield <list@qtrac.plus.com> - 2014-03-17 08:31 -0700
    Re: Correct idiom for determining path when frozen in 3.4 Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-03-17 11:03 -0500

csiph-web