why cannot I use sp.signal by import scipy as sp? [duplicate]
Asked Answered
P

2

6

I would like to use scipy.signal.lti and scipy.signal.impulse function to calculate the transfer function. I import the scipy module in the following way.

import scipy as sp
import numpy as np
import matplotlib.pyplot as plt
from math import *

However, when I type the following scripts,

tf = sp.signal.lti(numH, denH)

The Kernel gives an error:

---> 10 tf = sp.signal.lti(numH, denH)
AttributeError: 'module' object has no attribute 'signal' 

I tried another way to import the signal module,

from scipy.signal import lti, step, impulse

Then, the script works,

tf = lti(numH, denH)

So, my questions is, must we import every subpackage in the script? Then what's the point of importing the scipy package?

Thanks.

Pereyra answered 27/10, 2015 at 22:27 Comment(1)
See also #21072215; I'm sure there are still more occurrences of this question elsewhere on stackoverflow.Fondle
O
4

From the scipy doc:

Using any of these subpackages requires an explicit import. For example, import scipy.cluster.

or from scipy import cluster.

There isn't much point to doing a simple

import scipy

Look at the site-packages/scipy/__init__.py file for more details. Compare it with the numpy init.

numpy is an integrated package, scipy is a collection of loosely integrated packages. numpy is the basic numeric package that everyone uses. The scipy subpackages are relatively independent of each other. I can load and use sparse without knowning anything about the signal or integrate packages.

Odontograph answered 27/10, 2015 at 22:34 Comment(0)
R
1

The default behavior for which modules are imported in a python package is defined in the __init__.py file in the package directory. You can locate the scipy directory or find this using ipython, import scipy and call scipy? with questionmark to get the path. By default it seems scipy only imports numpy. The signal module is a directory of scripts, which includes its own __init__.py so you need the scipy.signal.lti syntax.

You could edit __init__.py as outlined in this question which would reduce the boilerplate imports, although probably not recommended as it would yield code which is not portable and may result in name clashes.

Rivero answered 27/10, 2015 at 22:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.