Django Error: Cannot use ImageField because Pillow is not installed

会有一股神秘感。 提交于 2020-04-30 10:37:30

问题


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?


回答1:


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)




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/51164887/django-error-cannot-use-imagefield-because-pillow-is-not-installed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!