0

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

Community
  • 1
  • 1
pseudomonas
  • 423
  • 2
  • 7
  • 22
  • What version of `scipy`? The current documentation suggests the `imread` and `imsave` methods are inside `scipy.misc`, so the correct method call may be [scipy.misc.imsave()](https://docs.scipy.org/doc/scipy-0.16.0/reference/generated/scipy.misc.imsave.html) – chickity china chinese chicken Apr 25 '17 at 17:38
  • How did you import `scipy`? How are you calling `imread()` and `imsave()`? Any code snippets you have may be helpful to providing a useful answer. – chickity china chinese chicken Apr 25 '17 at 17:50
  • Yes, I am using scipy.misc.imsave after importing it. – pseudomonas Apr 26 '17 at 01:00
  • How are you starting python? What do you get if you type `which python`? What do you get if in Python you import `sys` and then type `print "\n".join(sys.path)`? I want to see if you are using Python 2 or 3 and if `/usr/lib/python2.7/dist-packages/` is on your `PYTHONPATH`. What version of scipy is it? What do you get if you import scipy and type `print scipy.__version__`? – Mark Mikofski Apr 26 '17 at 03:10
  • 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: – pseudomonas Apr 29 '17 at 05:48
  • @MarkMikofski I couldn't answer your questions fully in the comment. I have edited the question to add the answers to your questions. – pseudomonas Apr 29 '17 at 05:52
  • Your Python interpreter and site-packages are in a custom location (`/home/raghuram`) so you can install packages by either calling pip from Python as a module like `python -m pip install numpy scipy pillow` or by usind the pip `--install-option` with the distutils `--prefix` option like `pip install --install-option="--prefix=~" numpy scipy pillow` Good luck! I update my answer below, please let me know if that solves your problem by selecting it as the correct answer. – Mark Mikofski Apr 29 '17 at 07:01
  • weird that your pip can't figure out that it shouldn't install into `/usr/local/` can you type `which pip` and see if it is in `/usr/local/bin` or /usr/bin` if it is, then you should upgrade by typing `wget https://bootstrap.pypa.io/get-pip.py | python` altho you might need to add `--no-check-certificate` if it fails because of HTTPS – Mark Mikofski Apr 29 '17 at 07:24
  • I just realized that you will need to install blas first because pip won't use wheels if you use `--instal-option` so run `sudo apt install gfortran libblas-dev liblapack-dev libatlas-dev` if using `--prefix`. – Mark Mikofski Apr 29 '17 at 07:44
  • Also you could just add `/usr/local/lib/python2.7/dist-packages` and `/usr/lib/python2.7/dist-packages` to your `PYTHONPATH` using `sys.path.insert()` if you already have SciPy and Pillow in those folders – Mark Mikofski Apr 29 '17 at 07:47
  • Which version of Linux are you using? Is it Ubuntu? Is it 16.04 LTS? Also, can you tell me what you get if you type `which pip`? – Mark Mikofski Apr 29 '17 at 14:19
  • `which pip` gives me `/home/raghuram/bin/pip`. Yes, I am using Ubuntu 16.04 LTS. – pseudomonas Apr 30 '17 at 08:08
  • 1
    Your inputs were of great help!! sys.path.insert for `/usr/local` and `/usr/lib' along with the `--prefix` option worked. – pseudomonas Apr 30 '17 at 09:58

2 Answers2

2

Try to install Pillow through conda via:

conda install Pillow

dopexxx
  • 2,298
  • 19
  • 30
1

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:

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.

Community
  • 1
  • 1
Mark Mikofski
  • 19,398
  • 2
  • 57
  • 90
  • @Raghuram you will need to use the `--prefix=/home/raghuram` option since your `PYTHONPATH` points to `/home/raghuram`. Sorry, the `--home` option won't work because it puts site-packages in `/lib/python/site-package` _not_ `python2.7/site-packages` note the "2.7" is missing. Also the `--user` options installs into `~/.local` with an extra "." (dot) before "local". The `--root` option is weird, not sure where it installs to. – Mark Mikofski Apr 29 '17 at 06:58
  • One option I hadn't considered is to let Python tell pip where to go by calling pip as a module using `python -m pip install numpy scipy pillow` that might be easiest. Inside python import `site` and type `print site.getsitepackages()` and see what it says. If it includes `/home/raghuram/lib/python2.7/site-packages` then you know that `python -m pip install ` will always work. – Mark Mikofski Apr 29 '17 at 06:59