28

I've been trying to get started with scipy, but the package is giving me some problems. The tutorial leans heavily on scipy.io, but when I import scypi and try to use scipy.io, I get errors:

In [1]: import scipy

In [2]: help(scipy.io)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/home/chris/dev/scipy/<ipython-input-2-ef060398b31c> in <module>()
----> 1 help(scipy.io)

AttributeError: 'module' object has no attribute 'io'

I've run system updates and I uninstalled scipy then installed it again.

Interestingly enough, I can import the module this way:

In [1]: import scipy.io

But then when I try to use it, I get an error as soon as I use a method:

In [2]: arr = scipy.array([[1.0,2.0],[3.0,4.0],[5.0,6.0]])
In [3]: outFile = file('tmpdata1.txt', 'w')
In [4]: scipy.io.write_array(outFile, arr)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/home/chris/dev/scipy/<ipython-input-4-46d22e4ff485> in <module>()
----> 1 scipy.io.write_array(outFile, arr)

AttributeError: 'module' object has no attribute 'write_array'

I'm sure I'm missing something embarrassingly basic, but I've not been able to find an answer to this problem on Google or in the stackoverflow archives.

Monica Heddneck
  • 2,973
  • 10
  • 55
  • 89
Chris Hanson
  • 2,043
  • 3
  • 16
  • 26
  • `write_array` isn't in `scipy.io`, it's in [`scipy.io.array_import`](http://www.scipy.org/doc/api_docs/SciPy.io.array_import.html#write_array). Does `scipy.io.array_import.write_array` (after `import scipy.io.array_import`) work? What tutorial are you following? – Luke Woodward Jun 23 '12 at 20:22

1 Answers1

37

Two things here. First, you cannot in general access a module in a package by doing import package and then trying to access package.module. You often have to do what you did, import package.module, or (if you don't want to type package.module all the time, you can do from package import module. So you can also do from scipy import io.

Second, the scipy.io module does not provide a write_array function. It looks like maybe it used to, but they got rid of it. You may be looking at an outdated tutorial. (What tutorial are you using?) Googling around, it seems they suggest to use numpy's savetxt function instead, so you might want to look into that.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
  • Yeah, it seems that the tutorial I was using (http://cutter.rexx.com/~dkuhlman/scipy_course_01.html) was terribly outdated. I don't even remember how I came across it now, but I found the one on the scipy site (http://docs.scipy.org/doc/scipy/reference/tutorial/) which I'll be using henceforth. Thanks for clearing up my import confusion as well! – Chris Hanson Jun 23 '12 at 20:41
  • The documentation wasn't terribly outdated at the time. `write_array` was moved from `scipy.io` to `scipy.io.array_import` (very) roughly a year and a half before your original post, then soon after removed it completely in favor of `savetxt` and friends. – Mike McKerns Jul 18 '14 at 13:55
  • Also, you can now just use the `tofile` method that is directly on the `ndarray`. That's even easier. – Mike McKerns Jul 18 '14 at 14:02
  • I'm confused.. why does calling scipy.io.foo fail when only importing scipy but numpy.random.foo suceeds when only importing numpy? Doesn't seem very consistent here! – gota Aug 02 '18 at 16:52
  • 5
    @gota: Because some libraries auto-import their submodules and others do not (and some auto-import some submodules but not others). In this case it looks like numpy does and scipy does not. If there is a library called `blah`, it can do `from . import foo, bar`, and then you'll be able to do `blah.foo.stuff` without explicitly doing `import blah.foo`, because `blah` already imported `foo` for you. But if the library doesn't explicitly do that itself, it won't be done implicitly. – BrenBarn Aug 02 '18 at 18:00
  • Is there any way to know whether a submodule is imported automatically other than trial and error? – gota Aug 21 '19 at 18:02
  • @gota: Not really. But it doesn't really matter. You can always do `import scipy.io` and if it was already imported it won't hurt anything or take any meaningful amount of time. – BrenBarn Aug 21 '19 at 20:24