【发布时间】:2019-05-15 21:49:56
【问题描述】:
我是 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'
我在 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',
]
我试过这些命令。
sudo pip3 install djangorestframework
sudo pip install djangorestframework
sudo pip install django-rest-swagger
sudo apt-get install python3-setuptools
我还尝试在sudo su 之后安装 djangorestframework。
(我也尝试了其他建议。但我不记得了。)
另外,这是我在 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'
>>>
这是一个 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
如何解决我的问题?
任何帮助将不胜感激。我在 * 上看到其他帖子似乎与我的相似,但似乎没有解决我的问题。
(编辑 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.
(编辑 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
我的 virtualenv 上似乎没有安装 rest_framework。但是,djangorestframework==3.9.4 是全局安装的。
【问题讨论】:
-
你在正确的 virtualenv 中吗?
-
我很确定我在正确的 virtualenv 中。
-
您在创建 virtualenv 时是否使用了
sudo?听起来您基于其他 cmets,如果您想安装rest_framework,那么您需要在安装时使用 sudo,或者最好在不使用 root 用户的情况下删除 virtualenv 并重新创建。
标签: django reactjs django-rest-framework