If I run the following two lines of code:
import numpy
numpy.linalg
I do not get an error, and I get the output <module 'numpy.linalg' from /Users/...>.
However, if I run the following two lines of code
import scipy
scipy.linalg
I get the error
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-7-7ed633b28ccc> in <module>
1 import scipy
----> 2 scipy.linalg
AttributeError: module 'scipy' has no attribute 'linalg'
However, the following code seems to work fine:
import scipy.linalg
scipy.linalg
My question is: how come after importing numpy, I can use numpy.linalg, but I can't do the same for scipy?
For context, I am using numpy version 1.19.2, and I am using scipy version 1.4.1.
There is a line in "numpy/__init__.py", which is the module initialization file, that explicitly imports linalg:
from . import linalg
There is no such line in "scipy/__init__.py". That is why you must import scipy.linalg yourself.
Related
I'm trying to import autograd with the following line of code:
import autograd.numpy as np
However, I'm getting the following error when trying to run the script:
Traceback (most recent call last):
File "autograd.py", line 1, in <module>
import autograd.numpy as np
File "/home/hakon/Documents/FYS_STK4155/project2/code and plots/test/autograd.py", line 1, in <module>
import autograd.numpy as np
ModuleNotFoundError: No module named 'autograd.numpy'; 'autograd' is not a package
I've tried installing autograd through pip, pip3 and conda, but the error remains the same.
The problem is that your module (the one that you're running) has the same name that you're trying to import: autograd (.py). Try renaming your file and running it again.
aaossa's answer worked for me. If changing the module name that you are running doesn't work, please check if there is any other autograd(.py) that you created existing in your current directory. If so, you also need to change that file's name or delete it so that you can import "autograd".
Hello l tried to run my python code and the errors is:
Traceback (most recent call last):
File "/home/anelmad/Desktop/simulation/Theano-Tutorials-master/3_net.py", line 5, in <module>
from foxhound.utils.vis import grayscale_grid_vis, unit_scale
ImportError: No module named vis**
knowing that l have correctly installed foxhound.
import theano
from theano import tensor as T
import numpy as np
from load import mnist
from foxhound.utils.vis import grayscale_grid_vis, unit_scale
from scipy.misc import imsave
is Foxhound a scikit? if yes, it doe's not contains utils module. You can clone it on github https://github.com/IndicoDataSolutions/Foxhound
I have met with the same problem. from foxhound vis import grayscale_grid_vis is enough. And I just comment out the other part. And the program works well. It seems that the module is not used.
I have an issue with importing the scipy.special package. It isn't harmful, just annoying/interesting.
When I import scipy using import scipy as sp and then try to access sp.special I get:
>>> import scipy as sp
>>> sp.special
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'special'
>>>
but if I then do import scipy.special I can access the special module through scipy.special and sp.special:
>>> import scipy as sp
>>> import scipy.special
>>> scipy.special
<module 'scipy.special' from 'C:\Python27\lib\site-packages\scipy\special\__init__.pyc'>
>>> sp.special
<module 'scipy.special' from 'C:\Python27\lib\site-packages\scipy\special\__init__.pyc'>
>>>
So I now have the special module accessible through both sp and scipy namespaces. The interesting bit is that I can access the rest of scipy through the scipy namespace.
First question: Why does the special module not import first time round?
Second question: How can I get access to the special module through the sp namespace only, without defining the scipy namespace?
Edit: using Python 2.7.2 and scipy 0.10.1
By default, "import scipy" does not import any subpackage. There are too many subpackages with large Fortran extension modules that are slow to load. I do not recommend doing import scipy or the abbreviated import scipy as sp. It's just not very useful. Use from scipy import special, from scipy import linalg, etc.
When I am trying to import ConvexHull from scipy I am getting an error.
from scipy.spatial import ConvexHull
**Traceback (most recent call last):
File "<input>", line 1, in <module>
ImportError: cannot import name ConvexHull**
Here what I am doing wrong?
I am using scipy0.12.0 version.But when I am importing scipy and scipy.spatial I am not getting any error.
I think this is what you are looking for
from scipy import spatial
spatial.qhull?
Type: module
Base Class: <type 'module'>
String Form:<module 'scipy.spatial.qhull' from '/usr/lib/python2.7/dist-packages/scipy/spatial/qhull.so'>
Namespace: Interactive
File: /usr/lib/python2.7/dist-packages/scipy/spatial/qhull.so
Docstring:
Wrappers for Qhull triangulation, plus some additional N-D geometry utilities
.. versionadded:: 0.9
But it is true, you should be able to import the ConvexHull directly from version 0.12.0 on.
New in version 0.12.0.
I'm using 0.9 so that is why I have to use the Qhull library.
I'm trying to run the contourf example from the matplotlib documentation.
When I run it from a python shell, everything works well, but when I run it as a script (python contourf_demo.py), the following error rises:
Traceback (most recent call last):
File "matplotlib.py", line 3, in
import matplotlib.pyplot as plt
File "/home/user/dir/contourf_demo.py", line 3, in
import matplotlib.pyplot as plt
ImportError: No module named pyplot
I'm using Ubuntu 12.04, and the problem comes when importing:
import matplotlib.pyplot as plt
I have tried using different ways to call the import (such as from matplotlib import pyplot), but it doesn't work neither.
You can't import a module named XYZ from a file called XYZ.py, because Python would interpret that you are trying to import the file itself.
Change your file from matplotlib.py to anything else.