Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'example:': 0.03; 'syntax': 0.04; 'explicitly': 0.05; 'importing': 0.05; 'interpreter': 0.05; 'context': 0.07; "'python": 0.09; '[1]:': 0.09; '[2]:': 0.09; 'matplotlib': 0.09; 'mess': 0.09; 'modes': 0.09; 'typed': 0.09; 'runs': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; 'suggest': 0.14; "'from": 0.16; 'cc:name:python list': 0.16; 'magic': 0.16; 'mean,': 0.16; 'pylab': 0.16; 'script,': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'bit': 0.19; 'basically': 0.19; 'normally': 0.19; 'passing': 0.19; "python's": 0.19; 'typing': 0.19; 'work,': 0.20; 'seems': 0.21; 'import': 0.22; 'cc:addr:python.org': 0.22; 'creating': 0.23; 'enhanced': 0.24; 'module,': 0.24; 'script.': 0.24; 'fine': 0.24; 'cc:2**0': 0.24; 'sort': 0.25; 'script': 0.25; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'appear': 0.29; 'testing': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'getting': 0.31; 'usually': 0.31; 'bunch': 0.31; 'convenience': 0.31; 'fine,': 0.31; 'trace': 0.31; 'this.': 0.32; 'stuff': 0.32; 'run': 0.32; 'up.': 0.33; 'becomes': 0.33; 'problem': 0.35; "can't": 0.35; 'editor': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'in.': 0.36; 'like,': 0.36; 'doing': 0.36; 'should': 0.36; 'rather': 0.38; 'short': 0.38; 'bad': 0.39; 'use.': 0.39; 'sure': 0.39; 'system.': 0.39; 'how': 0.40; 'even': 0.60; 'read': 0.60; 'commands': 0.60; 'details.': 0.61; "you're": 0.61; 'back': 0.62; "you've": 0.63; 'kind': 0.63; 'real': 0.63; 'more': 0.64; 'great': 0.65; 'by:': 0.65; 'to:addr:gmail.com': 0.65; 'details': 0.65; 'introduction': 0.68; 'hand': 0.80; 'commands.': 0.84; 'oscar': 0.84; '2013,': 0.91; 'apparent': 0.91; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=EXMN8rLYnkV2ktD6iL7te6DXkYnYN+6l8FjIwW0q7Yg=; b=ReT06t2YA3VmZvSxPDKBwqkrVHRADAXBO6JW0XbpgTLCF+os4BLDUvxjoPi5mFRahs FgYxH7t6S+eaG3Q1qsXtNWjrZ6nD+g1zdvnLdPYnXWMxc4EdB4W/Bz8mO5mGpBq6GyCx XGwI6AaaS5KeFE1rP7kX6mMsMD5wqtALw1wNRZ7PNdWy4bEm23NvaX3jQjppvHs7MWj0 9z28IqgOOQXZ6d4wcYvIl+f9MEo1eBsS6ncrff0DbhIdX+cHcKBYxJA/UCnazWZlRp0j /XBLFvFK+Uf1WstaWkf3AMDjlo406CIP9pqz0B+zeKa1Z0ix+/YTnj26KObbr9wGs4Op 0M5A== X-Received: by 10.58.67.9 with SMTP id j9mr11511185vet.3.1380879401507; Fri, 04 Oct 2013 02:36:41 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Oscar Benjamin Date: Fri, 4 Oct 2013 10:36:21 +0100 Subject: Re: ipy %run noob confusion To: jshrager@gmail.com Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: Python List 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: 72 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1380879410 news.xs4all.nl 15980 [2001:888:2000:d::a6]:41031 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:55460 On 3 October 2013 18:42, wrote: > I have some rather complex code that works perfectly well if I paste it i= n by hand to ipython, but if I use %run it can't find some of the libraries= , but others it can. The confusion seems to have to do with mathplotlib. I = get it in stream by: > > %pylab osx > > and do a bunch of stuff interactively that works just fine, for example: > > clf() > > But I want it to run on a %run, but %pylab is (apparently) not allowed fr= om a %run script, and importing matplotlib explicitly doesn't work...I mean= , it imports, but then clf() is only defined in the module, not interactive= ly. The % commands are ipython magic commands. They are not valid Python syntax so you can't use them in a Python script. In a Python script you would use 'from pylab import *' for (roughly) the same effect: $ ipython Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] Type "copyright", "credits" or "license" for more information. IPython 0.13.2 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details. In [1]: from pylab import * In [2]: clf Out[2]: > More confusing, if I do all the setup interactively, and the try to just = run my script, again, clf() [etc] don't work (don't appear to exist), even = though I can do them interactively. When you use %run it runs the script "externally". This is basically the same as typing 'python myscript.py' in the system terminal. In that case the script needs to import everything it wants to use. > There seems to be some sort of scoping problem ... or, put more correctly= , my problem is that I don't seem to understand the scoping, like, are %run= eval'ed in some closed context that doesn't work the same way as ipython i= nteractive? Is there any way to really do what I mean, which is: Please jus= t read in commands from that script (short of getting out and passing my sc= ript through stdin to ipython?) I'm not sure if I understand what you mean but I usually %edit the script and closing the editor seems to just run the commands as if I typed them directly in. If you really want this kind of semi-interactive Matlab-style approach I suggest having a look at the Spyder IDE. Personally though I think it's bad to work this way in Python (and in Matlab) and I discourage my students from doing this. The interactive interpreter modes are great for testing short snippets of code or introspecting modules etc. However any real code should go in a real script. Using %edit for convenience while you write the script is fine but make sure that what you're creating is a real Python script that you can run normally with 'python myscript.py'. Spyder is also good for doing this. Otherwise all of your work, computation and plots are a mess and it becomes impossible to trace back to exactly how you produced everything to check your work or to fix it when it becomes apparent that you've screwed up. Oscar