Forums

Django rest framework import

I got my rest_framework installed but when I add 'rest_framework' to my INSTALLED_APPS I got the next error in my error.log.

2018-04-25 19:11:43,793: Error running WSGI application
2018-04-25 19:11:43,794: ModuleNotFoundError: No module named 'rest_framework'
2018-04-25 19:11:43,794:   File "/var/www/kambo_pythonanywhere_com_wsgi.py", line 25, in <module>
2018-04-25 19:11:43,794:     application = get_wsgi_application()
2018-04-25 19:11:43,794: 
2018-04-25 19:11:43,794:   File "/usr/local/lib/python3.6/dist-packages/django/core/wsgi.py", line 13, in get_wsgi_application
2018-04-25 19:11:43,795:     django.setup(set_prefix=False)
2018-04-25 19:11:43,795: 
2018-04-25 19:11:43,795:   File "/usr/local/lib/python3.6/dist-packages/django/__init__.py", line 27, in setup
2018-04-25 19:11:43,795:     apps.populate(settings.INSTALLED_APPS)
2018-04-25 19:11:43,795: 
2018-04-25 19:11:43,795:   File "/usr/local/lib/python3.6/dist-packages/django/apps/registry.py", line 85, in populate
2018-04-25 19:11:43,795:     app_config = AppConfig.create(entry)
2018-04-25 19:11:43,796: 
2018-04-25 19:11:43,796:   File "/usr/local/lib/python3.6/dist-packages/django/apps/config.py", line 94, in create
2018-04-25 19:11:43,796:     module = import_module(entry)

A similar problem happens when I try to import the rest_framework from my urls.py, but in the other hand when I import rest_framework with IPython -> "python manage.py shell" it works fine.
Any clue to solve this? Thanks

[edit by admin: formatting]

Are you sure that you've installed it for the right version of Python? Each version keeps its own set of installed packages, so pip install --user djangorestframework (which installs stuff for Python 2.7) won't install it for, say Python 3.6. You need to specify the Python version in the command, for example:

pip3.6 install --user djangorestframework

i am getting the same problem,please someone help, also getting ModuleNotFoundError: No module named 'django.urls'

that possibly sounds like an error with the wrong django version installed vs the code

I am facing the same problem Everything works on LocalHost but same code does not work on pythonanywhere.com. python version - Python 3.6.9 django version - 3.0.6 django rest framework version - 3.11.0.
What should i do now?

If you're using Django 3.0 then I recommend that you use a virtualenv for your website -- then you should use pip inside the virtualenv to install the djangorestframework package.

Hi Giles!

I am getting the same error: ModuleNotFoundError: No module named 'rest_framework'

What did I do:

First I created Web app with Django 3.7 and named it mysite.

Then:

1) mkvirtualenv --python=python3.7 pollsapi

2) workon pollsapi

3) pip3.7 install django

4) which django-admin.py -----> /.virtualenvs/pollsapi/bin/django-admin.py

5) django-admin.py --version ------> 3.1

6) django-admin.py startproject pollsapi

7) Changed

path = '/home/myusername/mysite' -------> path = '/home/myusername/pollsapi'

and

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings' ------> os.environ['DJANGO_SETTINGS_MODULE'] = 'pollsapi.settings'

8) python manage.py migrate

9) python manage.py startapp polls

Then I reloaded the website and checked it was running.

10) I changed the content of modules.py file into

"from django.db import models

from django.contrib.auth.models import User

class Poll(models.Model):

question = models.CharField(max_length=100)

created_by = models.ForeignKey(User, on_delete=models.CASCADE)

pub_date = models.DateTimeField(auto_now=True)


def __str__(self):
    return self.question

class Choice(models.Model):

poll = models.ForeignKey(Poll, related_name='choices', on_delete=models.CASCADE)

choice_text = models.CharField(max_length=100)

def __str__(self):
    return self.choice_text

class Vote(models.Model):

choice = models.ForeignKey(Choice, related_name='votes', on_delete=models.CASCADE)

poll = models.ForeignKey(Poll, on_delete=models.CASCADE)

voted_by = models.ForeignKey(User, on_delete=models.CASCADE)


class Meta:

    unique_together = ("poll", "voted_by") "

11) Then I added these lines into INSTALLED_APPS = ( ... 'rest_framework', 'polls', )

12) python manage.py makemigrations polls

I am doing this with these tutorials:

1) https://books.agiliq.com/projects/django-api-polls-tutorial/en/latest/setup-models-admin.html

2) https://help.pythonanywhere.com/pages/VirtualEnvForNewerDjango/

13) python manage.py migrate

Then I am having No module named 'rest_framework'

But when I enter this command "pip show djangorestframework" it's giving:

Name: djangorestframework

Version: 3.11.1

Summary: Web APIs for Django, made easy.

Home-page: https://www.django-rest-framework.org/

Author: Tom Christie

Author-email: tom@tomchristie.com

License: BSD

Location: /home/username/.virtualenvs/pollsapi/lib/python3.7/site-packages

Requires: django

Required-by:

How can I solve this Giles?

Do you have your web app configured to use that virtual environment? You can do it on the "Web" configuration page on PythonAnywhere.

Hi fjl,

Yes I had my web app configure to use venv.

But now I found another way without venv.

I created Django web app with python 3.8

Then on the console I typed: pip3 install --user Django==3.1

Then: pip3 install --user djangorestframework

That's all it's working, xaxaxa