37

I've seen both:

import scipy as sp

and:

import scipy as sc

Is there an official preference listed anywhere?

For example, in the Introduction of the Scipy documentation, it is recommended to

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

but a similar abbreviation is not offered for the Scipy package.

In this question, sp is recommended, but the link to the Scipy docs doesn't actually specify sp over sc.

Community
  • 1
  • 1
DanHickstein
  • 6,196
  • 12
  • 50
  • 85
  • 1
    see http://docs.scipy.org/doc/scipy/reference/api.html#guidelines-for-importing-functions-from-scipy – cel Mar 15 '16 at 14:59
  • Oh, so the advice in the linked questions is actually misleading. You basically never want to ``import scipy as sp``. You just want to `from scipy import integrate`. This is interesting, since it seems like there could be potential conflicts between the, somewhat generically, titled scipy submodules. – DanHickstein Mar 15 '16 at 15:01
  • @DanHickstein: you could use `import scipy.integrate as SI` or some such unique identifier to avoid conflicts. – unutbu Mar 15 '16 at 15:11

3 Answers3

52

The "official" answer, according to the Scipy documentation, is that there is really no reason to ever

import scipy

since all of the interesting functions in Scipy are actually located in the submodules, which are not automatically imported. Therefore, the recommended method is to use

from scipy import fftpack
from scipy import integrate

then, functions can be called with

fftpack.fft()

Personally, I always use

import scipy.fftpack

and live with the slightly longer function call

scipy.fftpack.fft(data)

This way I know where the functions are coming from.

Cody Gray
  • 230,875
  • 49
  • 477
  • 553
DanHickstein
  • 6,196
  • 12
  • 50
  • 85
  • 5
    On the other hand, fully qualified names may be preferable in longer scripts. And then a short-hand for `scipy` can be useful. Something like `import scipy as sp` followed by `import scipy.fft` will allow to use in the code: `sp.fft`. – norok2 Sep 27 '17 at 16:02
13

As cel pointed out, the API documentation recommends to not import scipy, but to import specific modules from scipy:

The scipy namespace itself only contains functions imported from numpy. These functions still exist for backwards compatibility, but should be imported from numpy directly.

Therefore, importing only the scipy base package does only provide numpy content, which could be imported from numpy directly.

If somebody still wants the main package, sp for Scipy would be convenient as np usially is used for NumPy.

Finwood
  • 3,659
  • 1
  • 17
  • 36
0

Based on github repositories the most commonly used is:

import scipy as sp

Github usage stats

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 29 '22 at 07:58
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/31888011) – Emi OB Jun 01 '22 at 08:00