[IPython-User] question about extensions
Michael Kuhlen
mqk@astro.berkeley....
Wed Oct 17 14:00:34 CDT 2012
You're the best!
Thanks for patiently explaining the basics.
Mike
On Wed, Oct 17, 2012 at 11:52 AM, MinRK <benjaminrk@gmail.com> wrote:
>
>
> 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
>
>
>
> _______________________________________________
> IPython-User mailing list
> IPython-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-user
>
--
*********************************************************************
* *
* Dr. Michael Kuhlen Theoretical Astrophysics Center *
* email: mqk@astro.berkeley.edu UC Berkeley *
* cell phone: (831) 588-1468 B-116 Hearst Field Annex # 3411 *
* skype username: mikekuhlen Berkeley, CA 94720 *
* *
*********************************************************************
More information about the IPython-User
mailing list