python/django - "Cannot use ImageField because Pillow is not installed"

43,866

Solution 1

I tried :

  • Reinstall globaly PIL by compiling "Imaging-1.1.7" using some instructions here, but didn't work
  • Reinstall Pillow and it's dependency globally using that link, but didn't work
  • Reinstall GCC4.2 using this link, but it didn't work

I finally figured out I was in the case described in the wonderfull answer to this post. In other words, I am running a mac whose CPU is capable of 64bit but whose kernel firmware is set to 32bit. Which is a problem as the project I'm working on was built for 64bit.

As explained in that post, when you install python3 using an installer (DMG) it will sniff if the kernel is set to 32 bit and install 32bit version of python 3 accordingly. But if you just download the tarball source from python's website and install it with :

cd Python-3.4.1
./configure
make
sudo make install

Then the 64bit version of python3 should be installed. Which you can verify by doing :

file /usr/local/bin/python3
/usr/local/bin/python3: Mach-O 64-bit executable x86_64

That done, all problems are gone with PIL/Pillow in the virtualenv using this 64bit version of python3. Even the pip downgrade became unnecessary.

Solution 2

I was having this problem on a Mac with Python 3.6.4. The solution was to uninstall Pillow 5.1.0 and instead install 5.0.0.

pip uninstall Pillow
pip install Pillow==5.0.0

Solution 3

I was too getting same problem while implememnting Image Upload using CLoudinary , but found the Above answer, but in some other way.

 sudo pip uninstall PIL

 sudo pip uninstall Pillow

 sudo pip install Pillow

After that mine Problem was solved !

Solution 4

Had a similar problem, and my solution was much simpler:

Apparently packages PIL and Pillow can't coexist. If you want to use Pillow you first have to uninstall PIL and then install Pillow.

If you are on Mac, you have to install a few libraries as well using brew. Mentioned below is the sequence of steps:

$pip uninstall PIL
$brew install libtiff libjpeg webp little-cms2
$pip install Pillow

To test if pillow is installed and ready to use, open python interpreter and try to import the following:

>>> from PIL import Image

*note that the library still says PIL but now it is importing from Pillow instead of PIL.

If you are able to successfully import then you are good to go (in all probability you won't have to worry about setting PYTHONPATH or 32/64-bit installations)

Source: https://pillow.readthedocs.io/en/latest/installation.html

Solution 5

For those who faced this issue with alpine-based image in docker;

Pillow requires these os level modules(for installation)

gcc musl-dev jpeg-dev zlib-dev libjpeg

You probably delete these modules after install your python packages but, for use Pillow without issue, it requires libjpeg Unless, Django raise with error:

Cannot use ImageField because Pillow is not installed.

So add this module in Dockerfile(or don't uninstall):

RUN apk add libjpeg

works for me

Share:
43,866
lapin
Author by

lapin

Updated on July 12, 2022

Comments

  • lapin
    lapin almost 2 years

    I'm joining a project, so I want to set up the environnment, so what I did is :

    pip install -r requirements.txt
    

    This fully installed all requirements including django 1.7.0, Pillow 2.4.0 and some others.

    Then I want to build the database :

    python manage.py migrate
    

    And boom, error, I get the following :

    CommandError: System check identified some issues:
    
    ERRORS:
    stu.chan.icon: (fields.E210) Cannot use ImageField because Pillow is not installed.
        HINT: Get Pillow at https://pypi.python.org/pypi/Pillow or run command "pip install pillow".
    stu.chan.image: (fields.E210) Cannot use ImageField because Pillow is not installed.
        HINT: Get Pillow at https://pypi.python.org/pypi/Pillow or run command "pip install pillow".
    stu.Piec.icon: (fields.E210) Cannot use ImageField because Pillow is not installed.
        HINT: Get Pillow at https://pypi.python.org/pypi/Pillow or run command "pip install pillow".
    

    ... like I didn't install Pillow. So I checked the installed package (with this technique), and Pillow 2.4.0 IS installed.

    Then, I also tried to force reinstall : pip install --upgrade --force-reinstall Pillow==2.4.0

    But, nothing to do I get the same error when running migrate.

    I'm using python 3.4.0 and django 1.7.0 on a mac OS X 10.6.7 wrapped in virtualenv 1.11.6 with pip downgraded to pip 1.2.1 (because of some well-known-yet-not-fully-resolved-nor-understood issue with pip and ssl).

    All of the code above is within virtualenv (bin/activate done).

    Do you have any ideas on why this problem and how to resolve it?

    - - - - - - EDIT - - - - - -

    When I run the above force-reinstall command, (so many code gets outpouted I can't paste it all but) although it finishes with "Successfully installed Pillow", there's some warnings in the code :

    building 'PIL._imaging' extension
     (blabla code)
        _imaging.c:975:13: warning: array index of '1' indexes past the end of an array (that contains 1 elements) [-Warray-bounds]
            value = PyTuple_GET_ITEM(xy, 1);
                    ^~~~~~~~~~~~~~~~~~~~~~~
        /usr/local/include/python3.4m/tupleobject.h:58:34: note: instantiated from:
        #define PyTuple_GET_ITEM(op, i) (((PyTupleObject *)(op))->ob_item[i])
                                         ^
        /usr/local/include/python3.4m/tupleobject.h:27:5: note: array 'ob_item' declared here
            PyObject *ob_item[1];
            ^
        1 warning generated.
    
    (blabla code)
       libImaging/Unpack.c:867:1: warning: unused function 'copy3' [-Wunused-function]
        copy3(UINT8* out, const UINT8* in, int pixels)
        ^
        1 warning generated.