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