<br><br><div class="gmail_quote">On Wed, Oct 17, 2012 at 11:38 AM, Michael Kuhlen <span dir="ltr"><<a href="mailto:kuhlen@gmail.com" target="_blank">kuhlen@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi there<br>
<br>
A question about IPython extensions. From reading the documentation it<br>
looks like extension are meant to modify the behaviour of the shell,<br>
and so my question might not make any sense, but let me ask anyway. Is<br>
there some way to use extensions inside an externally defined<br>
function?<br>
<br>
More specifically, I've been using the ipython-physics<br>
(<a href="https://bitbucket.org/birkenfeld/ipython-physics" target="_blank">https://bitbucket.org/birkenfeld/ipython-physics</a>) extension to handle<br>
physical quantities with units. This works great in the IPython shell<br>
and in notebooks, but it doesn't seem to work when I try to use it<br>
inside externally defined functions. Here's what I mean:<br>
<br>
In [1]: %load_ext physics<br>
Unit calculation and physics extensions activated.<br>
<br>
In [2]: print Q('1 hplanck').base<br>
6.6260696e-34 m^2*kg/s<br>
<br>
In [3]: def test():<br>
...: print Q('1 hplanck').base<br>
...:<br>
<br>
In [4]: test()<br>
6.6260696e-34 m^2*kg/s<br>
<br>
In [5]: !cat test.py<br>
def test():<br>
print Q('1 hplanck').base<br>
<br>
In [6]: import test as t<br>
<br>
In [7]: t.test()<br>
---------------------------------------------------------------------------<br>
NameError Traceback (most recent call last)<br>
<ipython-input-6-f69a6e70fa4a> in <module>()<br>
----> 1 t.test()<br>
<br>
/home/mqk/test.py in test()<br>
1 def test():<br>
----> 2 print Q('1 hplanck').base<br>
<br>
NameError: global name 'Q' is not defined<br>
<br>
<br>
<br>
Perhaps this is unavoidable, but I'd love to find a way to use<br>
physical quantities also inside externally defined functions.<br></blockquote><div><br></div><div>extensions are just modules on a special path. They do not (generally) affect anything other than the user namespace and IPython itself.</div>
<div><br></div><div>To get a name from an extension, you treat it just like any other module (at least after it has been loaded):</div><div><br></div><div># load the extension first, so that the 'physics' module exists</div>
<div>%load_ext physics</div><div><br></div><div># then its names become importable:</div><div>from physics import Q</div><div><br></div><div>-MinRK</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Any insights?<br>
<br>
Thanks,<br>
<br>
Mike<br>
_______________________________________________<br>
IPython-User mailing list<br>
<a href="mailto:IPython-User@scipy.org">IPython-User@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/ipython-user" target="_blank">http://mail.scipy.org/mailman/listinfo/ipython-user</a><br>
</blockquote></div><br>