0

I am trying to understand the syntax logic of Python all about imports. Take for example the function scipy.signal.ricker. The function should not matter.

To use this function of Python (without any imports at all) I have to

import scipy

and then I can use it via

scipy.signal.ricker(...)

Alternatively I can

from scipy import signal

and use it via

signal.ricker(...)

Is there any option to import ricker directly so that I just had to write ricker(...) and how would I import something else if it would be nested one, two or 10 levels further?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
lmixa
  • 49
  • 5

2 Answers2

1

Use the . operator to search the contents of a module/submodule.

In your case

from scipy.signal import ricker

And then simply use it by ricker(...)

For more information: What does a . in an import statement in Python mean?

Abishek V Ashok
  • 513
  • 3
  • 17
  • 1
    Thanks a lot. For the first option there is displayed an error: ModuleNotFoundError: No module named 'scipy.signal.ricker' Your explanation arises another question. What do you mean with "search" for the contents of a module/submodule? Will there be the entire (sub)module content listed? – lmixa Nov 25 '21 at 13:09
  • Yeah removed it. That works only for local packages. – Abishek V Ashok Nov 25 '21 at 13:10
  • What do you mean by entire submodule content listed? – Abishek V Ashok Nov 25 '21 at 13:13
  • You wrote: "search the contents..." I have wondered if there will be displayed a list with the content of for example scipy. – lmixa Nov 25 '21 at 13:18
  • Some IDEs can reference stubs and figure this out. For a pragmatic implementation see https://stackoverflow.com/a/65193885/4861361 – Abishek V Ashok Nov 25 '21 at 13:20
0

You can use:

from scipy.signal import ricker
Jeroen Vermunt
  • 672
  • 1
  • 6
  • 19