question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

To me, this seems like very odd behavior

See original GitHub issue

THIS works as expected:

#Import to good stuff
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt

from scipy.special import gamma     # NOTE: Why ius this necessary?

# Print to confirm that the imports were done successfully
print("Skeleton stuff imported OK")

# Start program instructions here
for x in range(5):
    y = sp.special.gamma(x)
    putstr = '   ' + str(x) + ' -> ' + str(y)
    print(putstr)

print('==========================')
print()
while True:
    xs = input('Enter x = ')
    if 'Q' in xs.upper():
        break
    x = eval(xs)

    z = sp.special.entr(x)
    print("entr(%s) = %s" %(x, z))

    w = sp.special.erf(x)
    print("erf(%s) = %s" %(x, w))
        
    y = sp.special.gamma(x)
    print("gamma(%s) = %s" %(x, y))

    print()

BUT, if I delete the line “from scipy.special import gamma”, and try to run, I get the following output:

Traceback (most recent call last):
  File "D:/PYTHON/MyPrograms/Gamma Test-0000.py", line 14, in <module>
    y = sp.special.gamma(x)
AttributeError: module 'scipy' has no attribute 'special'

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
rkerncommented, Apr 1, 2020

Just to add an explanation of that implicit side effect:

When we import a subpackage, the Python import system places it in the namespace of the parent package. If you just do import scipy; print(scipy.special), that scipy.special access won’t work. If you do import scipy; from scipy import special; print(scipy.special), that scipy.special access will work because the from scipy import special (or import scipy.special or from scipy.special import gamma) will import the special sub-package and place it in the scipy namespace as a side effect.

This is mostly for caching purposes. I would consider it an implementation detail. I wouldn’t write my code to routinely rely on that implicit side effect. As I stated previously, I would explicitly import the sub-packages that I need and use them explicitly.

1reaction
matthew-brettcommented, Apr 1, 2020

It’s fairly standard in big libraries, like Scipy, not to import everything with a top level import like import scipy, because this would make it necessary to import everything from Scipy even if you were only, for example, using the scipy.special sub-package. This in turn would make all Scipy imports slower, as we do (in this case) an unnecessary import of scipy.optimize, scipy.io etc.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Oppositional Defiant Disorder (ODD): Symptoms & Treatment
Oppositional defiant disorder (ODD) is a condition in which your child displays a pattern of uncooperative, defiant and angry behavior ...
Read more >
What Is ODD or Oppositional Defiant Disorder?
Oppositional defiant disorder (ODD) is diagnosed in kids who are unusually angry, throw tantrums, don't follow rules, or purposefully harm ...
Read more >
Signs & Causes of Oppositional Defiant Disorder
Oppositional defiant disorder (ODD) is a behavior disorder that is characterized by an ongoing pattern of uncooperative, defiant, hostile, and annoying behavior ......
Read more >
What Does Oppositional Defiant Disorder Look Like in Adults?
Adults with ODD are more than just aggressive and irritating from time to time. They feel mad at the world every day, and...
Read more >
Oppositional Defiant Disorder (ODD) - Healthline
a frequently irritable and angry mood or short temper. ODD can make it very challenging to interact with other people. Again, the behaviors...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found