ModuleNotFoundError:没有名为'rest_framework'的模块我已经安装了djangorestframework

时间:2018-01-03 09:05:39

标签: python django

我收到一个错误,ModuleNotFoundError:当我运行命令python manage.py runserver时没有名为'rest_framework'的模块。 Traceback说

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x108193ae8>
Traceback (most recent call last):
  File "/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 113, in inner_run
    autoreload.raise_last_exception()
  File "/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages/django/apps/config.py", line 116, in create
    mod = import_module(mod_path)
  File "/Users/xxx/anaconda/envs/env/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 948, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'rest_framework'

我已经运行了命令pip3 install djangorestframework,当我再次运行此命令时,Requirement已经满足:/usr/local/lib/python3.6/site-packages中的djangorestframework在terminal.Python版本中显示为3.6.2我的代码出了什么问题?我应该如何解决这个问题?我使用的是Anaconda虚拟环境。

7 个答案:

答案 0 :(得分:6)

好像/usr/local/lib/python3.6/site-packages已将软件包安装到/Users/xxx/anaconda/envs/env/lib/python3.6/site-packages,而不是您的环境python -m pip

如果您使用python manage.py runserver,那么该软件包将安装在您运行python -m pip install djangorestframework 时使用的相同版本的python中。这是建议的命令in the Python docs

{{1}}

答案 1 :(得分:3)

您是否在设置中添加了rest_framework?

INSTALLED_APPS = [
'rest_framework',
'django.contrib.contenttypes',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

可能是这个post会帮助你。看看。

答案 2 :(得分:1)

(取决于您的python版本)

pip/pip3 install djangorestframework

这解决了我。

答案 3 :(得分:0)

通过告诉django从lib中收集它来将你想要在django项目中使用的模块添加到你的设置文件中

INSTALLED_APPS = [

'django.contrib.contenttypes',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',                # now django knows you will use it in your project.
]

但是,让我告诉你一个我遵循的最佳做法,以保持它整洁。

我创建了另一个名为THIRD_PARTY_LIBS的列表,我将连接到原始INSTALLED_APPS这样的内容

THIRD_PARTY_APPS = ['rest_framework'] # this list shall contain many more of those useful apps and stuff.

INSTALLED_APPS += THIRD_PARTY_APPS  # Boom.. the things goes skraa.. pop.. pop.. 

谢谢!

答案 4 :(得分:0)

首先需要检查项目解释器: 如果您使用的是Pycharm:请转到Pycharm->首选项/设置->项目解释器,然后检查其他已安装的库中是否存在该文件?

如果不是,则需要手动添加。为此,可以使用+按钮在其中添加新软件包,搜索“ djangorestframework ”并下载。重新启动服务器,您一切顺利

答案 5 :(得分:0)

此命令python -m pip install djangorestframework表示 djangorestframework已安装

DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: djangorestframework in /Users/rana.singh/Library/Python/2.7/lib/python/site-packages (3.9.4)

然后我尝试了此python3 -m pip install djangorestframework,它开始安装

Collecting djangorestframework
  Downloading djangorestframework-3.11.0-py3-none-any.whl (911 kB)
     |████████████████████████████████| 911 kB 1.6 MB/s 
Requirement already satisfied: django>=1.11 in /usr/local/lib/python3.7/site-packages/Django-3.1-py3.7.egg (from djangorestframework) (3.1)
Requirement already satisfied: asgiref>=3.2 in /usr/local/lib/python3.7/site-packages/asgiref-3.2.7-py3.7.egg (from django>=1.11->djangorestframework) (3.2.7)
Requirement already satisfied: pytz in /usr/local/lib/python3.7/site-packages/pytz-2020.1-py3.7.egg (from django>=1.11->djangorestframework) (2020.1)
Requirement already satisfied: sqlparse>=0.2.2 in /usr/local/lib/python3.7/site-packages/sqlparse-0.3.1-py3.7.egg (from django>=1.11->djangorestframework) (0.3.1)
Installing collected packages: djangorestframework
Successfully installed djangorestframework-3.11.0

此后,我能够跑步 python3 manage.py makemigrations

答案 6 :(得分:0)

如果您使用 pipenv shell,请确保使用 pipenv 重新安装“丢失的包” 例如pipenv install djangorestframework 这解决了我的问题