How can I import odeint and arange in Python? - python

I am trying to import odeint in python by doing:
from scipy.integrate import odeint
but there is an error note:
ImportError: No module named scipy.integrate
I have looked through some websites and there is a module called scipy.integrate, I am not sure what is going wrong with my code?
I also tried
from scipy import arange
which also shows similar error message:
ImportError: No module named scipy
I am using Python 2.7.8. Is it due to the version of Python? How can I fix it then?
If I could not even import them, I won't be able to continue writing my code.
Could somebody please give some advice?
Thanks.

This happens when scipy is not installed.
Download & Install SciPy from : Scientific Python
Ubuntu & Debian : sudo apt-get install python-numpy python-scipy python-matplotlib ipython ipython-notebook python-pandas python-sympy python-nose
Fedora: sudo yum install numpy scipy python-matplotlib ipython python-pandas sympy python-nose
Windows : Unofficial Windows Binaries for Python Extension Packages
SciPy source

Related

Python: Imported matplotlib but cannot use imshow

I'm working in Ubuntu as Virtual-Box guest on top of Windows machine (as host), and I am attempting to run my python script from the terminal. I had difficulty installing matplotlib using pip install. I managed to install it using
sudo apt-get install python-matplotlib
However, I am unable to bring up an image I have created in my code:
import numpy as np
import matplotlib.pyplot as plt
import random as random
from random import randrange
image = plt.imshow(mymatrix)
plt.show()
If I import matplotlib as:
import matplotlib as plt
I am recieving the following error on attempting to run the script:
AttributeError: 'module' object has no attribute 'imshow'
If I import matplotlib as:
import matplotlib.pyplot as plt
I recieve the following error:
raise ImportError, str(msg) + ', please install the python-tk package'
ImportError: No module named _tkinter, please install the python-tk
package
On attempting to install python-tk using 'pip install python-tk' this is what I'm getting:
~/ising $ pip install python-tk Collecting python-tk Could not find
a version that satisfies the requirement python-tk (from versions: )
No matching distribution found for python-tk
I'm unsure if I have in fact incorrectly installed matplotlib from the beginning. I am aware pyplot is not automatically imported with matplotlib, could the same be true for installing it from the console? Seems like I have tried everything at this stage.
The problem appears to be that you do not have a graphical backend installed. The error you are getting about Python Tk is happening because normally Tk comes with any Python distribution, so you should have at least that. You can install any Python bindings for Tk, Pyqt4, Pyqt5, Wx, GTK, (possibly others) to get a working interactive graphical backend. Check the package repository for the actual package names to install.
Keep in mind that functions like imshow are part of the (sub)package matplotlib.pyplot, not matplotlib itself. import matplotlib as plt is simply wrong if you intend to do plt.imshow(...). The correct import is either from matplotlib import pyplot as plt or import matplotlib.pyplot as plt.
According to the documentation here try this
python -mpip install -U pip
python -mpip install -U matplotlib

Error with scipy: No module named `imsave`

I am using the scipy module's imread and imsave utilities. I get the following error:
No module named imsave.
I did a little googling and figured that the error was due to PIL/Pillow not being installed. I do:
sudo pip install Pillow.
I get the following message:
Requirement already satisfied: Pillow in /usr/local/lib/python2.7/dist-packages.
I am importing scipy's misc functionality to use the imread and imsave function.
import scipy.misc
import numpy as np
I = np.load('image.npy')
scipy.misc.imsave('test_image.jpg',I) #The error pops up here
J = scipy.misc.imread('test_image.jpg')
I reinstalled scipy after this. I still get the No module named error.
EDIT 1: To make things clear, I uninstalled PIL by following this link. I then uninstalled scipy. But, when I run sudo apt install python-scipy python-pil, it says that pil is already the latest. However, it is not in the path /usr/local/lib/python2.7/dist-package.
EDIT 2: To answer Mark Mikofski's questions:
I am using Python from the Terminal. I run the file from the Terminal.
`which python`
gives me the following output
`/home/raghuram/bin/python`.
Importing sys and doing what you tell gives the list of following outputs:
/home/raghuram/lib/python2.7
/home/raghuram/lib/python2.7/plat-x86_64-linux-gnu
/home/raghuram/lib/python2.7/lib-tk
/home/raghuram/lib/python2.7/lib-old
/home/raghuram/lib/python2.7/lib-dynload
/usr/lib/python2.7
/usr/lib/python2.7/plat-x86_64-linux-gnu
/usr/lib/python2.7/lib-tk
/home/raghuram/local/lib/python2.7/site-packages
/home/raghuram/lib/python2.7/site-packages
Scipy's version is 0.19.0
Try to install Pillow through conda via:
conda install Pillow
Hi #Raghuram,
Welcome to StackOverflow! Thanks you for asking your question, and I hope that you find an answer. Here are some links to asking questions from the StackOverflow help center:
Your question might be off-topic because it has to do software on a particular plaform, so it might be better suited to askUbuntu or SuperUser
Your question doesn't have any code in it, and it also doesn't have minimum, complete and verifiable example. See the comments that ask about what version of Python and scipy you're using, how you installed them (Anaconda, system, etc?) and what platform you are on (Ubuntu? 16.04LTS?) and what code you used (how did you import scipy?).
Although you mentioned you did some Googling, your question lacks a sense of rigor - see "How do I ask a good question?. EG. Provide links to the sites you searched from which you obtained your attempted solution (to install the Pillow/PIL fork) - this link could be useful to someone else with a similar issue, even though it didn't help you.
Suggested solution
From your answer it appears that you are not using the system Python in /usr/lib/python2.7 and that your packages have been installed using the --prefix installation scheme into /home/raghuram and /home/raghuram/local/.
Unfortunately, pip will not use wheels if it gets --install-option so you will have to install BLAS first.
$ sudo apt install gfortran libblas-dev liblapack-dev libatlas-dev
Then try to use the --install-option with pip to pass the --prefix option to install.
$ pip install --install-option="--prefix=/home/raghuram/" numpy scipy pillow
Another perhaps easier option is to see where your python interpreter thinks site-packages should go. To do this, import site and call site.getsitepackages(). If /home/raghuram is in that list, then chances are you can just call pip from Python as a module using the -m option.
$ python -m pip install numpy scipy pillow
Finally, if all else fails, you can fall back on distutils, but this is tricky because you can't mix the scipy/numpy BLAS dependencies. They can only be either ATLAS, OpenBLAS, MKL, or etc., not a mix. To see what you are using, first import scipy numpy and then call numpy.show_configs() and scipy.show_configs(). It get's even trickier from here because you need to edit the setup.cfg to tell numpy/scipy where your BLAS is, so let's assume that you can remove both of these and start from scratch. First install the dependencies from your distro's repo; I think by default they will always build with ATLAS.
$ sudo apt install gfortran libblas-dev liblapack-dev libatlas-dev
Then download the numpy and scipy zip files from PyPI and extract. For each you need to enter the extracted folder and run:
$ python setup.py install --prefix=~
Now try to use scipy.misc.imsave like their help docstring example
>>> import numpy as np
>>> from scipy.misc import imsave
>>> help(imsave) # view docstring
>>> # then hit q key to return to interpreter
>>> x = np.zeros((255, 255))
>>> x = np.zeros((255, 255), dtype=np.uint8)
>>> x[:] = np.arange(255)
>>> imsave('gradient.png', x)
>>> rgb = np.zeros((255, 255, 3), dtype=np.uint8)
>>> rgb[..., 0] = np.arange(255)
>>> rgb[..., 1] = 55
>>> rgb[..., 2] = 1 - np.arange(255)
>>> imsave('rgb_gradient.png', rgb)
NB: you can always search for Ubuntu packages online or using apt search.
PS IMO you should probably remove any packages you've installed to system python using sudo pip and IMO never do that again. Check in /usr/local/lib/python2.7/dist-packages.
PPS IMHO you should never install Python packages on Linux using sudo, instead either install from the software repository of your distro using apt or yum, install using the pip --user option or create a Python virtual environment with virtualenv. See my AskUbuntu answer.

error importing numpy in python 3.5

I tried installing numpy with homebrew, but instead it installed in the Python 2.7 version. I found this out by following the answer on this post: Can't import numpy
Which is very strange especially since I specifically asked for python3:
M$ brew install numpy --with-python3
Warning: homebrew/python/numpy-1.11.1 already installed
However, when I try importing it in Python 3.5 I always get the same error:
File "Dataframe.py", line 1, in <module>
import numpy as np
ImportError: No module named 'numpy'
How can I get it to work as intended? I am on mac os 10.11.3
Check that you have Python 3.5 installed by running which python3 and which python (to make sure your versions aren't mixed up).
Then, to install for Python 3, you should run pip3 install numpy. It's a good idea to use the given Python tools to install Python-related packages.

Importing opencv and getting numpy.core.multiarray failed to import

Trying to install OpenCV and running into an issue where attempting to import cv2 results in this output -
RuntimeError: module compiled against API version 9 but this version of numpy is 7
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
import cv2
ImportError: numpy.core.multiarray failed to import
I'm running on Windows 7 x64, Python v 2.7.9
Thanks!
The error is telling you that you have an out of date version of numpy. If you used pip to install things you can simply run pip install numpy -U, or download the appropriate version from their website.
In case
pip install -U numpy
doesn't work (even with sudo), you may want to make sure you're using the right version of numpy. I had the same "numpy.core.multiarray failed to import" issue, but it was because I had 1.6 installed for the version of Python I was using, even though I kept installing 1.8 and assumed it was installing in the right directory.
I found the bad numpy version by using the following command in my Mac terminal:
python -c "import numpy;print numpy.version;print numpy.file";
This command gave me the version and location of numpy that I was using (turned out it was 1.6.2). I went to this location and manually replaced it with the numpy folder for 1.8, which resolved my "numpy.core.multiarray failed to import" issue. Hopefully someone finds this useful!
I had a similar problem and I solved it by downgrading my numpy version.
What I did was:
pip install opencv-python
pip uninstall numpy
pip install numpy=1.18
This has worked for me using
Python 3.7
opencv-python 4.4.0.46
numpy 1.18.0
linux: sudo apt-get install python-numpy
if you are using ubuntu bionic beaver then try running: sudo apt-get install python-numpy
had the same issue, resolve by running the above command.
Hope it helps
In your environment you can try this command:
conda uninstall numpy
conda install -c conda-forge numpy
I use Python 3.7 # RPI 4.
For opencv to install properly I had to install the listed libraries below.
(Not every package was actually installed, after request)
Regarding Numpy, I think one should stick to the latest version.
For me what worked is to uninstall the existing version 1.16.2 and stick with the current stable 1.21.2.
Stackoverflow topic at missing libraries here: ImportError: libcblas.so.3: cannot open shared object file: No such file or directory.

ImportError: No module named mpmath. But mpmath has been installed. What's wrong?

I have anaconda installed and many libraries of python, between those mpmath.
When I try to run powerlaw package I get the following error:
-> 1466 from mpmath import erfc
1467 # from scipy.special import erfc
1468 from scipy.constants import pi
ImportError: No module named mpmath
Furthermore when I simply try to do import mpmath I get the same error.
I'm using Fedora, I have already tried yum remove python-mpmath and then yum install python-mpmath. But I'm getting the same error.
I have seen in other questions that this might be because I have multiple paths and that I must add them both with a sys.path=['', etc].
What does "sys.path=['', etc]" mean?
When I do:
print(sys.path)
I get:
['', '/home/rm/anaconda/bin', '/home/rm/anaconda/lib/python27.zip', '/home/rm/anaconda/lib/python2.7', '/home/rm/anaconda/lib/python2.7/plat-linux2', '/home/rm/anaconda/lib/python2.7/lib-tk', '/home/rm/anaconda/lib/python2.7/lib-old',
'/home/rm/anaconda/lib/python2.7/lib-dynload', '/home/rm/anaconda/lib/python2.7/site-packages', '/home/rm/anaconda/lib/python2.7/site-packages/PIL', '/home/rm/anaconda/lib/python2.7/site-packages/runipy-0.1.0-py2.7.egg', '/home/rm/anaconda/lib/python2.7/site-packages/setuptools-3.6-py2.7.egg', '/home/rm/anaconda/lib/python2.7/site-packages/IPython/extensions',
'/home/rm/.ipython']
You should use conda to install Python packages into anaconda, or pip if they are not available via conda. conda install mpmath should fix your issue.

Categories

Resources