Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!news.tele.dk!news.tele.dk!small.news.tele.dk!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.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'url:pypi': 0.03; 'insert': 0.05; 'imported': 0.09; 'inserted': 0.09; 'namespace': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; 'c):': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'script,': 0.16; 'wrote:': 0.18; 'written': 0.21; 'import': 0.22; 'header:User-Agent:1': 0.23; 'module,': 0.24; 'received:comcast.net': 0.24; "i've": 0.25; 'this:': 0.26; 'defined': 0.27; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'external': 0.29; 'work.': 0.31; '(maybe': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'subject:what': 0.31; 'work:': 0.31; 'run': 0.32; 'url:python': 0.33; 'bugs': 0.33; 'something': 0.35; 'but': 0.35; 'subject:?': 0.36; 'url:org': 0.36; 'should': 0.36; 'wrong': 0.37; 'received:76': 0.38; 'to:addr:python-list': 0.38; 'anything': 0.39; 'does': 0.39; 'quote': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'how': 0.40; 'even': 0.60; 'profile': 0.61; "you've": 0.63; 'name': 0.63; 'results': 0.69; 'repeat': 0.74; 'decorate': 0.84; 'subject:skip:l 10': 0.84; 'profile.': 0.91; 'yourself,': 0.95 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Neal Becker Subject: Re: line_profiler: what am I doing wrong? Date: Tue, 10 Feb 2015 19:45:05 -0500 References: <54daa1d6$0$12979$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: c-76-100-246-88.hsd1.md.comcast.net User-Agent: KNode/4.14.3 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: 77 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1423615521 news.xs4all.nl 2942 [2001:888:2000:d::a6]:41104 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:85494 Steven D'Aprano wrote: > Neal Becker wrote: > >> I inserted >> @profile >> def run(...) >> >> into a module-level global function called 'run'. Something is very wrong >> here. 1. profile results were written before anything even ran >> 2. profile is not defined? > > Well, is it defined? Where does it come from? > > If you defined it yourself, it needs to be defined before you can use it. > This won't work: > > > @profile > def run(...) > > def profile(func): ... > > > Swap the order of profile and run and it should work. (Give or take any > additional bugs in your code.) > > > If you've imported it from an external module, how did you import it? > > > import some_module > > @some_module.profile > def run(...) > > > should work. So will this: > > > from some_module import profile > > @profile > def run(...) > > > But this won't: > > > import some_module > > @profile > def run(...) > > > and will fail with NameError, exactly as you are experiencing. > > > > To quote from https://pypi.python.org/pypi/line_profiler/ $ kernprof -l script_to_profile.py kernprof will create an instance of LineProfiler and insert it into the __builtins__ namespace with the name profile. It has been written to be used as a decorator, so in your script, you decorate the functions you want to profile with @profile. @profile def slow_function(a, b, c): ... I've used it before (maybe 1 year ago), don't know why it isn't working now. -- -- Those who don't understand recursion are doomed to repeat it