1

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.

Jeong Kim
  • 481
  • 3
  • 9
  • 21
  • Are you in the correct virtualenv? – A. J. Parr May 16 '19 at 00:02
  • I am fairly sure that I am in the correct virtualenv. – Jeong Kim May 16 '19 at 21:35
  • Did you use `sudo` when you created the virtualenv? Sounds like you have based on other comments, and if you want to install `rest_framework` then you'll need to either use sudo when you install it, or preferably delete the virtualenv and recreate without using the root user. – A. J. Parr May 17 '19 at 01:12

8 Answers8

5

Everything I've tried failed. I simply created another virtualenv and reinstalled everything. Now, it is working like a charm.

Jeong Kim
  • 481
  • 3
  • 9
  • 21
2

I was getting error even after installing rest_framework. Then finally, this solution worked for me.

  1. Open project in PyCharm IDE
  2. Create serializers.py
  3. Enter this line - from rest_framework import serializers
  4. Hover over the squiggly line and PyCharm would suggest you to install rest_framework
1

It seems to me that you're trying to run this with a virtual environment called new_app activated while Django REST Framework is installed globally.

You have to ways for solving this:

  1. Run pip install djangorestframework (and any other you want or need) with the new_app environment being active. Note there is no sudo here

  2. Run your development server with no virtual env active.

Of course, number one is the preferred way. Good luck!

Raydel Miranda
  • 13,825
  • 3
  • 38
  • 60
  • I tried to run 'pip install djangorestframework'. It gives me an error that I don't have a permission. Do I have to change the user permission? – Jeong Kim May 16 '19 at 21:37
1

You can run pip freeze to confirm what libraries you have installed in your virtualenv.
Sometimes the library was not installed properly and it will be missing in that list.

Danilo Akamine
  • 705
  • 7
  • 13
0

Copy a rest_framework to your project main dir or install it

Johnny
  • 687
  • 1
  • 6
  • 22
0

I had this problem. And I had run python -m pip install --upgrade pip for me was works.

0

This helped me

pip3 install djangorestframework
pip install djangorestframework
pip install djangorestframework-jwt
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
max
  • 1
0

I checked pip freeze and my requirements.txt. Some of my modules are not listing in pip freeze. So reinstalled all the requirements.

pip install -r requirements.txt

The above method only works if you have a requirements.txt, unless you need to install the missing modules explicitly.

shamnad sherief
  • 552
  • 5
  • 17