[IPython-User] question about extensions

Michael Kuhlen kuhlen@gmail....
Wed Oct 17 13:38:47 CDT 2012


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.

Any insights?

Thanks,

Mike


More information about the IPython-User mailing list