8

I am using scipy and numpy through Anaconda 2.1.0 distribution. I use Spyder as my Python IDE.

When I run import scipy as sp, I can't access the subpackages, such as optimize, linalg, cluster etc. through sp.

However, when I run import numpy as np, I am able to access all its subpackages, such as linalg, random, matrixlib, polynomial, testing, etc. through np.

Is there a reason why the two imports work in different ways? Why does import scipy as sp not grab all scipy subpackages into sp's namespace?

gg349
  • 21,996
  • 5
  • 54
  • 64
user3317287
  • 429
  • 1
  • 5
  • 14
  • 1
    This is indeed an interesting question. Funny thing is: After `import scipy as sp` typing `sp.optimize` will give you an error. But if you do `from scipy import optimize`, suddenly `sp.optimize` won't give you an error anymore. This problem is not specific to anaconda. – cel Jan 02 '15 at 16:20
  • 3
    @cel, you don't get anymore the error because when you type `from scipy import optimize`, the interpreter realizes that it has already loaded the main package `scipy`, and it will import the submodule `optimize` within that already loaded module. The identifier `sp` is pointing to that same module as before that is now 'updated', so that you can now access the submodule `optimize` through the identifier as well. – gg349 Jan 02 '15 at 16:41
  • Historically, `numpy` is the basic, integrated numeric package. `scipy` is a collection of independently developed scientific packages. They all make use of `numpy`, but don't depend on each other. – hpaulj Jan 02 '15 at 21:24

1 Answers1

7

This possibility of different import behaviour occurs by design of the python language.

An import statement of a module(*) by default only imports the main module, and not the submodules. The main module may (like in the case of numpy) , or may not (like scipy) import some or all the submodules.

The reason behind this is exemplified by scipy: in most cases, you will need only one submodule of the scipy package. This default behaviour will not hang the interpreter at loading submodules that are unnecessary to your code.

EDIT: Notice that numpy does not import by default all the submodules, for example it does not load numpy.f2py, see THIS question/answer for more details.

(*) here I mean an import statement like import scipy or import scipy as sp, where a module is loaded. Of course if you write import scipy.optimize then python will first load the main module, and then the submodule.

Community
  • 1
  • 1
gg349
  • 21,996
  • 5
  • 54
  • 64
  • Thanks. So, what would be one simple expression to load all the submodules for any module under any identifier the user wants? – user3317287 Jan 02 '15 at 22:14
  • 1
    There is for sure a way, but it is a bad idea. Just import the modules you need. I would say that if you don't know in advance which submodules you're going to need from a certain module you have a bigger problem, in the sense that you don't know what you are doing – gg349 Jan 02 '15 at 22:18
  • you may be interested in [this](http://stackoverflow.com/questions/1707709/list-all-the-modules-that-are-part-of-a-python-package) question though – gg349 Jan 02 '15 at 22:19
  • I'm also trying to do this but I can't load the interpolate submodule. It says this "ImportError: DLL load failed: The specified module could not be found." – Ipa Dec 11 '19 at 11:37