9

I got an error,ModuleNotFoundError: No module named 'rest_framework' when I run command python manage.py runserver . Traceback says

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x108193ae8>
Traceback (most recent call last):
  File "/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 113, in inner_run
    autoreload.raise_last_exception()
  File "/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages/django/apps/config.py", line 116, in create
    mod = import_module(mod_path)
  File "/Users/xxx/anaconda/envs/env/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 948, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'rest_framework'

I already run command pip3 install djangorestframework,when I run this command again, Requirement already satisfied: djangorestframework in /usr/local/lib/python3.6/site-packages shows in terminal.Python version is 3.6.2.What is wrong in my code?How should I fix this?I use Anaconda virtual environment.

user7676799
  • 105
  • 1
  • 1
  • 8

9 Answers9

15

It looks as if pip3 install has installed the package into /usr/local/lib/python3.6/site-packages, instead of your environment /Users/xxx/anaconda/envs/env/lib/python3.6/site-packages.

If you use python -m pip, then the package will be installed in the same version of python that you use when you run python manage.py runserver. This is the suggested command in the Python docs.

python -m pip install djangorestframework
Alasdair
  • 298,606
  • 55
  • 578
  • 516
6

Did you add rest_framework in your setup?

INSTALLED_APPS = [
'rest_framework',
'django.contrib.contenttypes',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

May be this post will help you. Have a look.

Abhijit
  • 1,728
  • 1
  • 14
  • 25
  • 2
    `rest_framework` is already in the `INSTALLED_APPS`. The error is because it is in the `INSTALLED_APPS` but the package is not installed. – Alasdair Jan 03 '18 at 10:29
2

(depending on the version of python you are)

pip/pip3 install djangorestframework

This solved it for me.

alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46
Codebender
  • 195
  • 1
  • 10
2

First thing you need to check your project interpreter: if you are using Pycharm: you go to Pycharm -> preferences/settings -> project interpreter and check if it's there among other installed libraries?

if it's not you need to add it manually. To do so there is + button where you can add a new package, search for "djangorestframework" and download it. restart the server and you are good to go

suleiman
  • 21
  • 1
1

This command python -m pip install djangorestframework says that djangorestframework is already installed.

DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: djangorestframework in /Users/rana.singh/Library/Python/2.7/lib/python/site-packages (3.9.4)

Then I tried this python3 -m pip install djangorestframework which started the installation.

Collecting djangorestframework
  Downloading djangorestframework-3.11.0-py3-none-any.whl (911 kB)
     |████████████████████████████████| 911 kB 1.6 MB/s 
Requirement already satisfied: django>=1.11 in /usr/local/lib/python3.7/site-packages/Django-3.1-py3.7.egg (from djangorestframework) (3.1)
Requirement already satisfied: asgiref>=3.2 in /usr/local/lib/python3.7/site-packages/asgiref-3.2.7-py3.7.egg (from django>=1.11->djangorestframework) (3.2.7)
Requirement already satisfied: pytz in /usr/local/lib/python3.7/site-packages/pytz-2020.1-py3.7.egg (from django>=1.11->djangorestframework) (2020.1)
Requirement already satisfied: sqlparse>=0.2.2 in /usr/local/lib/python3.7/site-packages/sqlparse-0.3.1-py3.7.egg (from django>=1.11->djangorestframework) (0.3.1)
Installing collected packages: djangorestframework
Successfully installed djangorestframework-3.11.0

After this, I was able to run python3 manage.py makemigrations

Rana Ranvijay Singh
  • 6,055
  • 3
  • 38
  • 54
0

Add the module you intend to use in your django project by telling the django to collect it from lib by including them in your settings file something like this

INSTALLED_APPS = [

'django.contrib.contenttypes',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',                # now django knows you will use it in your project.
]

But let me tell you one of the best practices I follow to keep it neat.

I create another list called THIRD_PARTY_LIBS which I'll concatenate to the original INSTALLED_APPS something like this

THIRD_PARTY_APPS = ['rest_framework'] # this list shall contain many more of those useful apps and stuff.

INSTALLED_APPS += THIRD_PARTY_APPS  # Boom.. the things goes skraa.. pop.. pop.. 
Ruli
  • 2,592
  • 12
  • 30
  • 40
d-coder
  • 12,813
  • 4
  • 26
  • 36
  • `rest_framework` is already in the `INSTALLED_APPS`. The error is because it is in the `INSTALLED_APPS` but the package is not installed. – Alasdair Jan 03 '18 at 10:28
0

If you are using pipenv shell, make sure you reinstall the "missing packages" using pipenv For example pipenv install djangorestframework This solved my problem

0

exiting virtual environment and then restarting solved the issue for me. First run pip freeze in your virtual environment to confirm that the pip install djangorestframework did actually install.

If you see it listed after running pip freeze, then try exiting the virtual environment and then reactivating the virtual environment and then running server again etc.

Ruli
  • 2,592
  • 12
  • 30
  • 40
JVDB
  • 11
  • 3
0

At the first point you must makemigrations by python manage.py then migrate