3

This might be a stupid question. I don't understand why the first one throws an error while the other one works. Is there a simple explanation here?

In[2]: import scipy
In[3]: help(scipy.optimize)
Traceback (most recent call last):
  File "C:.....lib\site-packages\IPython\core\interactiveshell.py", line 3035, in run_code
  File "<ipython-input-3-87bd9e5565b6>", line 1, in <module>
    help(scipy.optimize)
AttributeError: 'module' object has no attribute 'optimize'



In[4]: from scipy import optimize
In[5]: help(optimize)
Help on package scipy.optimize in scipy:..........................

1 Answer 1

1

This has nothing to do with help(), importing it in a terminal yeilds the same result.

scipy is a package, optimize is a sub-package of scipy. In order for a module to be imported from a package it must be specified in the __init__.py file in the top level package scipy.

scipy -
       |- optimize
       |- __init.py__

It does not so you must specify that it does exist by importing it directly.

import scipy

Gets scipy but it does not say in the __init__.py file optimize is part of it. So it does not load it.

from scipy import optimize

Goes into scipy package and loads optimize without using the __init__.py file. In other words you tell it to get optimize yourself.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.