41

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,588
  • 13
  • 54
  • 90
  • 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

4 Answers4

57

EDIT: As of version 1.9, scipy has changed so that import scipy now allows access to the modules but only actually loads them when they are used. Please refer to the answer from tupui below (and upvote that answer to make it the top one!). For historical purposes, here is my answer from 2016:

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.

DanHickstein
  • 6,588
  • 13
  • 54
  • 90
  • 8
    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
  • 1
    The situation changed a bit recently, see my answer bellow https://stackoverflow.com/a/76762035/6522112 It's now okay to do `import scipy` and this would not incur any performance issue vs `import scipy.fft`. Note that `fftpack` is legacy and you should move to `scipy.fft` – tupui Jul 25 '23 at 11:02
14

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.

ankit
  • 3
  • 1
  • 11
Finwood
  • 3,829
  • 1
  • 19
  • 36
  • In the same URL, scipy recommends `import scipy.io as spio` for io. So, `sp` makes sense. – ankit Oct 31 '22 at 07:44
1

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
  • That total seems way too low. Has it excluded the tens of thousands that don't use `as`? – OrangeDog Apr 14 '23 at 09:47
1

SciPy maintainer here.

Before SciPy 1.9, we recommended people to not import the main namespace (import scipy) because this would recursively import everything leading to long import time.

Now we have a lazy loading mechanism which does not incur any performance penalty. So you can do import scipy and modules are only loaded when you use them.

To answer the OP, we do not have a recommended way to import SciPy. There are strong opinions among maintainers. Some maintainers advocate for never using aliases while other would welcome one.

My personal take is that, while the project does not recommend the use of an alias, if people are still going to use one, it would be best if everyone doing so would use the same. sp seems to be more used and libraries like NetworkX and Scikit-Image use this convention internally.

See the updated doc: https://scipy.github.io/devdocs/reference/index.html#guidelines-for-importing-functions-from-scipy

tupui
  • 5,738
  • 3
  • 31
  • 52