I am new to Django. I am using Ubuntu, Apache, and virtualenv. I am running Ubuntu on DigitalOcean. I am trying to use Django and React together. So I downloaded djangorestframework. But when I run the server, I encountered this problem.
(new_app) sot232@ubuntu:/var/www/html/django_app/commercial$ python3 manage.py runserver
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner
self.run()
File "/usr/lib/python3.7/threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "/home/sot232/new_app/lib/python3.7/site-packages/django/utils/autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "/home/sot232/new_app/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/home/sot232/new_app/lib/python3.7/site-packages/django/utils/autoreload.py", line 77, in raise_last_exception
raise _exception[0](_exception[1]).with_traceback(_exception[2])
File "/home/sot232/new_app/lib/python3.7/site-packages/django/utils/autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "/home/sot232/new_app/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/sot232/new_app/lib/python3.7/site-packages/django/apps/registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "/home/sot232/new_app/lib/python3.7/site-packages/django/apps/config.py", line 90, in create
module = import_module(entry)
File "/home/sot232/new_app/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'rest_framework'
I included rest_framework properly in the settings.py file.
INSTALLED_APPS = [
'rest_framework',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'commercial',
]
I tried these commands.
sudo pip3 install djangorestframework
sudo pip install djangorestframework
sudo pip install django-rest-swagger
sudo apt-get install python3-setuptools
I also tried to install djangorestframework after sudo su
.
(I tried other suggestions too. But I can't remember those.)
In addition, here's what I got on a python shell.
Python 3.7.3 (default, Apr 3 2019, 05:39:12)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import rest_framework
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'rest_framework'
>>>
Here is a dist-packages in case you need it.
(new_app) sot232@ubuntu:/usr/local/lib/python3.7/dist-packages$ ls
Django-2.2.1.dist-info pytz-2019.1.dist-info
__pycache__ rest_framework
corsheaders sqlparse
django sqlparse-0.3.0.dist-info
django_cors_headers-3.0.0.dist-info virtualenv-16.5.0.dist-info
djangorestframework-3.9.4.dist-info virtualenv.py
pytz virtualenv_support
How can I fix my problem?
Any help will be appreciated. I saw other posts on StackOverFlow that seem to be similar to mine, but nothing seemed to resolve my problem.
(Edit 1)
While the env is activated, I tried to run pip install djangorestframework
, and I got this error message.
(new_app) sot232@ubuntu:~/new_app/bin$ pip install djangorestframework
Collecting djangorestframework
Using cached https://files.pythonhosted.org/packages/1b/fe/fcebec2a98fbd1548da1c12ce8d7f634a02a9cce350833fa227a625ec624/djangorestframework-3.9.4-py2.py3-none-any.whl
Installing collected packages: djangorestframework
ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/home/sot232/new_app/lib/python3.7/site-packages/djangorestframework-3.9.4.dist-info'
Consider using the `--user` option or check the permissions.
(Edit 2)
I run pip freeze
and pip3 freeze
. I received this result.
sot232@ubuntu:~/new_app/bin$ source activate
(new_app) sot232@ubuntu:~/new_app/bin$ pip freeze
Django==2.2.1
mysqlclient==1.4.2.post1
pytz==2019.1
sqlparse==0.3.0
It seems like rest_framework is not installed on my virtualenv. However, djangorestframework==3.9.4
is installed globally.