Skip to content
Advertisement

ModuleNotFoundError: No module named ‘rest_framework’ (tried many solutions but not working)

I have encountered this error.

ModuleNotFoundError: No module named 'rest_framework'

I have a virtual environment setted up and the rest framework is installed correctly.

When I run pip3.10 show djangoframework, I get

Name: djangorestframework
Version: 3.14.0
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: c:userschandesktoptestpylibsite-packages
Requires: django, pytz
Required-by:

My interpreter is Python 3.10.8 which is the same version and it is for the virtual environment. my VSCode shows my interpreter as Python 3.10.8 ("TESTPY":venv) .Scriptspython.exe.

I also have included the rest_framework in the INSTALLED_APPS in the settings.py

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

Below is the full error I get.

(TESTPY) PS C:UsersChanDesktopTESTPY> python3 manage.py runserver
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libthreading.py", line 1016, in _bootstrap_inner
    self.run()
  File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libthreading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "C:UsersChanAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagesdjangoutilsautoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:UsersChanAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagesdjangocoremanagementcommandsrunserver.py", line 125, in inner_run
    autoreload.raise_last_exception()
  File "C:UsersChanAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagesdjangoutilsautoreload.py", line 87, in raise_last_exception 
    raise _exception[1]
  File "C:UsersChanAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagesdjangocoremanagement__init__.py", line 398, in execute     
    autoreload.check_errors(django.setup)()
  File "C:UsersChanAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagesdjangoutilsautoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:UsersChanAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagesdjango__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:UsersChanAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagesdjangoappsregistry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "C:UsersChanAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagesdjangoappsconfig.py", line 193, in create
    import_module(entry)
  File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libimportlib__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'rest_framework'

I have googled to fix this issue so many times now that all the links are purple.. Anyone have any idea why this is happening? Thanks

Edit:

When I run pip list, this is what I get.

(TESTPY) PS C:UsersChanDesktopTESTPY> pip list
Package             Version
------------------- -------
asgiref             3.5.2
Django              4.1.2
djangorestframework 3.14.0
pip                 22.3
pytz                2022.5
setuptools          63.2.0
sqlparse            0.4.3
tzdata              2022.5

Advertisement

Answer

Maybe you should use just the python command instead of python3.

When you activate a virtualenv, what happens is that some shell binaries are added to your PATH. These binaries are typically python and pip, and maybe other binaries from other pip packages that are installed in the virtualenv.

So then when the virtualenv is activated, the python command will execute the python binary inside the virtualenv. But the python3 executable is not overriden, so when you execute that, you just get your system python3.

So try:

python manage.py runserver

Or maybe you could even just do:

./manage.py runserver

If that doesn’t work, it might be because the manage.py file needs to have execute permissions:

chmod +x manage.py
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement