To me, this seems like very odd behavior
See original GitHub issueTHIS 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:
- Created 3 years ago
- Reactions:1
- Comments:8 (8 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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)
, thatscipy.special
access won’t work. If you doimport scipy; from scipy import special; print(scipy.special)
, thatscipy.special
access will work because thefrom scipy import special
(orimport scipy.special
orfrom scipy.special import gamma
) will import thespecial
sub-package and place it in thescipy
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.
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 thescipy.special
sub-package. This in turn would make all Scipy imports slower, as we do (in this case) an unnecessary import ofscipy.optimize
,scipy.io
etc.