On Tue, Aug 9, 2016 at 4:48 PM, Thomas Haslwanter <
[email protected]> wrote:

Hi,
> I am somewhat stuped about the behavior of scipy in a jupyter console:
>
> The following code runs properly as a python program:
>
> import scipy as sp
> print(sp.constants.g)
>
> But the same code produces an
>
> AttributeError: module 'scipy' has no attribute 'constants'
>
> when run in a QtConsole, or a Jupyter Notebook.
>
> My setup:
> Jupyter QtConsole 4.2.1
> Python 3.5.2
> Win10
> jupyter 1.0.0
> jupyter-console 5.0.0
> ipython 5.0.0
>
> Can anyone explain why the behavior in the jupyter notebook/qtconsole is
> different than in a python module?
>
In a standalone script with only those two lines, it raises the attribute
error on constants that you see interactively. I’m guessing when you are
using it in a program, some other part of your code is causing
scipy.constants to be imported, either directly or indirectly.

The key here is that scipy.constants isn’t a regular attribute of the scipy
module, it is itself a module. In general, modules do not have their
submodules available as attributes until those submodules have been
imported. Once they have been imported (anywhere), then the submodule will
be available as an attribute on its parent.

Try the following script:

import scipy
print('constants immediately?', hasattr(scipy, 'constants'))from
scipy.constants import g
print('after constants import?', hasattr(scipy, 'constants'))

And scipy.constants would be defined if *any code* had imported
scipy.constants at any time during the program, not just in your current
script.

So in general, if you want scipy.constants, you should import
scipy.constants or from scipy import constants.

-MinRK

Thanks, thomas
>
> --
> You received this message because you are subscribed to the Google Groups
> "Project Jupyter" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/jupyter/a170d2c8-37f0-473b-8450-cb6a7a218013%40googlegroups.com
> <https://groups.google.com/d/msgid/jupyter/a170d2c8-37f0-473b-8450-cb6a7a218013%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
​

-- 
You received this message because you are subscribed to the Google Groups 
"Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jupyter/CAHNn8BUkD3uF5Y3B4J1O1W0j0jga%2BeOnQptH5Yt00tX3dMRoeQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to