0

I'm currently learning how to use Django to develop a web service. As I go through an online course on Udemy, I ran into a problem using ImageField in models.py. My problem is that, when I excute the runserver command, Django shows the following error although I am pretty sure the library Pillow is installed.

In models.py, I have the following code:

from django.db import models
from django.contrib.auth.models import User 
class UserProfileInfo(models.Model):
    user = models.OneToOneField(User, on_delete=models.PROTECT) 
    portfolio = models.URLField(blank=True)
    picture = models.ImageField(upload_to="profile_pics")
    def __str__(self):
     return user.self.username

When I runserver, I get the following error: python manage.py runserver Performing system checks...

Unhandled exception in thread started by <function check_errors<locals>.wrapper at 0x10407b6a8>
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run
self.check(display_num_errors=True)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site- 
packages/django/core/management/base.py", line 410, in check
 raise SystemCheckError(msg)
django.core.management.base.SystemCheckError: SystemCheckError: System check 
 identified some issues:

ERRORS:
first_app.UserProfileInfo.picture: (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".

System check identified 1 issue (0 silenced).

So, by following the message I checked the installed modules by using the method introduced here: How can I get a list of locally installed Python modules?. The following code is run in a file in the same directory as manage.py. I'm using Pycharm to use a virtual environment, so I am assuming I'm using the same environment as the Django project uses when I run server.

As the result of the method above, I could see Pillow is installed, like this:

['argon2==0.1.10', 'bcrypt==3.1.4', 'cffi==1.11.5', 'django==2.0.5', 'faker==0.8.15', 'olefile==0.45.1', 'pillow==5.0.0', 'pip==9.0.1', 'pycparser==2.18', 'python-dateutil==2.7.3', 'pytz==2018.4', 'setuptools==28.8.0', 'six==1.11.0', 'text-unidecode==1.2']

How can I fix this problem? Or, is there any alternative to use images in Django without Pillow?

Goma0925
  • 31
  • 2
  • 6
  • are you sure that you are running django on that specific python installation? – Simas Joneliunas Jul 04 '18 at 02:11
  • Did you install Pillow from pycharm? – icyfire Jul 04 '18 at 03:59
  • are you using virtualenv? if yes, use "pip freeze" command in order to display installed module of virtualenv. maybe you install pillow on your machine not on virtualenv – Ali Jul 04 '18 at 10:04
  • @MohammadAli I ran pip freeze in the same directory as my Django project and saw Pillow is installed although it is in the older version than the one I meant to use. My question is, are these libraries the ones that my Django project uses? If yes, how can I manipulate the libraries for my Django project? I've been assuming that importing and exporting libraries though PyCharm would change the libraries for my project. Also, is it the case that the interpreter that is set as the project interpreter in PyCharm is not the interpreter that my Django project uses? – Goma0925 Jul 04 '18 at 23:52
  • @icyfire Yes, I installed Pillow through PyCharm. I went to Preference, project interpreter, and then installed Pillow by clicking the "+" button. Is this the right way to add a library to my Django project? – Goma0925 Jul 04 '18 at 23:58
  • It seems like PyCharm CE which I'm currently using does not support Django framework and I think that is causing the problem. If I had a professional edition, I can create a Django project in PyCharm but the current version of PyCharm CE doesn't allow me to create a Django project.(https://stackoverflow.com/questions/23870365/how-to-setup-django-project-in-pycharm) PyDev seems to support Django, so I guess I will try that one to develop my application. – Goma0925 Jul 05 '18 at 00:50

3 Answers3

2

I ran into this issue in a docker container. Pillow won't install without jpeg and zlib header and library support, so I added zlib-dev and jpeg-dev to the build, and then removed them after installing Pillow. Of course then Pillow refused to import, because libjpeg and friends are missing. The solution was to add back the relevant (non-dev) packages, and everything started working as desired.

tbm
  • 1,142
  • 12
  • 22
0

In order to define a virtualenv for your project in pycharm, first go to this path :

file -> settings -> Project -> Project Interpreter

now you can see all project that are opened in pycharm. select your desired project, and click Project Interpreter config button, then select Add local python interpreter. in this form, select Existing environment. now in button inside Interpreter you can select your desired virtualenv (myvirtualenv -> bin -> python3.x)

Ali
  • 2,541
  • 2
  • 17
  • 31
0

I don't know the specific reasons but it seems like I found a solution for the situation where the virtualenv created by PyCharm is not recognized by the terminal in PyCharm. The solution is to create a virtualenv by using virtualenv library in plain python and set it as the project interpreter in PyCharm, rather than creating a virtualenv through PyCharm.

Goma0925
  • 31
  • 2
  • 6