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: [email protected]
License: BSD
Location: c:\users\chan\desktop\testpy\lib\site-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) .\Scripts\python.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:\Users\Chan\Desktop\TESTPY> python3 manage.py runserver
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\threading.py", line 1016, in _bootstrap_inner
self.run()
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\threading.py", line 953, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Chan\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\Users\Chan\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\core\management\commands\runserver.py", line 125, in inner_run
autoreload.raise_last_exception()
File "C:\Users\Chan\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\utils\autoreload.py", line 87, in raise_last_exception
raise _exception[1]
File "C:\Users\Chan\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\core\management\__init__.py", line 398, in execute
autoreload.check_errors(django.setup)()
File "C:\Users\Chan\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\Users\Chan\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\Chan\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\apps\registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "C:\Users\Chan\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\apps\config.py", line 193, in create
import_module(entry)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\importlib\__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:\Users\Chan\Desktop\TESTPY> 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
CodePudding user response:
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
CodePudding user response:
You have two different python installations
from pip3 show
, there is this line:
Location: c:\users\chan\desktop\testpy\lib\site-packages
but your stack trace is listing files that are in
File "C:\Users\Chan\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\