Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'exists.': 0.07; 'executable': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sure,': 0.09; 'try:': 0.09; 'behaviour.': 0.16; 'believes': 0.16; 'clear.': 0.16; 'exponential': 0.16; 'finite': 0.16; 'finney': 0.16; 'getattr(sys,': 0.16; 'pythonic': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'statement.': 0.16; 'subject:when': 0.16; 'substitute': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'slightly': 0.19; 'header:User- Agent:1': 0.23; 'helpful': 0.24; 'mon,': 0.24; 'right.': 0.26; 'header:X-Complaints-To:1': 0.27; 'am,': 0.29; 'raise': 0.29; "doesn't": 0.30; 'code': 0.31; 'posting': 0.31; 'apparently': 0.31; 'existence': 0.31; 'values.': 0.31; 'writes:': 0.31; 'probably': 0.32; 'problem': 0.35; 'except': 0.35; 'something': 0.35; 'but': 0.35; 'picking': 0.36; "didn't": 0.36; 'thanks': 0.36; 'skip:o 20': 0.38; 'ben': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; '8bit%:29': 0.60; 'hope': 0.61; 'making': 0.63; 'skip:n 10': 0.64; 'more': 0.64; 'different': 0.65; 'skip:\xe2 10': 0.65; 'world': 0.66; 'mar': 0.68; 'default': 0.69; 'led': 0.72; '8bit%:43': 0.74; '8bit%:46': 0.78; '2:02': 0.84; 'lastly,': 0.84; 'mistakes.': 0.84; 'received:125': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ben Finney Subject: Re: Correct idiom for determining path when frozen in 3.4 Date: Mon, 17 Mar 2014 20:23:59 +1100 References: <8a0ca549-7bce-4210-8f9e-3465a769fc3c@googlegroups.com> <85ha6xp59k.fsf@benfinney.id.au> 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 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux) Cancel-Lock: sha1:RGS34Yib3PhzkgKxJcYg33SXBmU= X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1395048251 news.xs4all.nl 2862 [2001:888:2000:d::a6]:58647 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68417 Devin Jeanpierre writes: > On Mon, Mar 17, 2014 at 2:02 AM, Ben Finney wrote: > > Mark Summerfield 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