ModuleNotFoundError:没有名为“rest_framework”的模块。我该如何修复这个错误?

Jeo*_*Kim 1 django django-rest-framework reactjs

我是 Django 新手。我正在使用 Ubuntu、Apache 和 virtualenv。我在 DigitalOcean 上运行 Ubuntu。我正在尝试同时使用 Django 和 React。所以我下载了djangorestframework。但是当我运行服务器时,我遇到了这个问题。

(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'
Run Code Online (Sandbox Code Playgroud)

我在settings.py 文件中正确包含了rest_framework。

INSTALLED_APPS = [
    'rest_framework',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'commercial',
]
Run Code Online (Sandbox Code Playgroud)

我尝试了这些命令。

sudo pip3 install djangorestframework
sudo pip install djangorestframework
sudo pip install django-rest-swagger
sudo apt-get install python3-setuptools
Run Code Online (Sandbox Code Playgroud)

我也尝试在之后安装 djangorestframework sudo su

(我也尝试过其他建议。但我不记得那些了。)

另外,这是我在 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'
>>> 
Run Code Online (Sandbox Code Playgroud)

这是一个 dist-packages 以防您需要。

(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
Run Code Online (Sandbox Code Playgroud)

我该如何解决我的问题?

任何帮助将不胜感激。我在 StackOverFlow 上看到其他帖子似乎与我的类似,但似乎没有什么可以解决我的问题。

(编辑1)

当环境被激活时,我尝试运行pip install djangorestframework,并收到此错误消息。

(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.
Run Code Online (Sandbox Code Playgroud)

(编辑2)

我跑pip freeze,然后pip3 freeze。我收到了这个结果。

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
Run Code Online (Sandbox Code Playgroud)

我的virtualenv 上似乎没有安装rest_framework。但是,djangorestframework==3.9.4是全局安装的。

Jeo*_*Kim 5

我尝试过的一切都失败了。我只是创建了另一个 virtualenv 并重新安装了所有内容。现在,它就像魅力一样发挥作用。