python 没有名为rest_framework的模块

vs3odd8k  于 2022-10-30  发布在  Python
关注(0)|答案(3)|浏览(296)

我看到很多人在这里遇到这个问题,我总是看到他们说其他使用pip 3或只是pip来安装它。
但在我的情况下,我已经卸载了它,并安装了多次id都使用pip或pip 3,他们似乎都不工作。
我已经将“rest_framework”添加到我的设置中,但似乎仍然不起作用。请帮助我

EDIT
Here is my settings

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'agenda.apps.AgendaConfig',
    'rest_framework',

]

REST_FRAMEWORK = {
    # Use Django's standard `django.contrib.auth` permissions,
    # or allow read-only access for unauthenticated users.
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
    ]
}

正在收集Djangorestframework下载Djangorestframework-3. 7. 3-py 2. py 3-none-any.whl(1. 5 MB)100%|我是一个很好的朋友。|1.5MB 506 kB/s安装收集的软件包:已成功安装djangorestframework-3. 7. 3
还是不行
编辑2

> Traceback (most recent call last):   File
> "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py",
> line 226, in wrapper
>     fn(*args,**kwargs)   File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py",
> line 121, in inner_run
>     self.check(display_num_errors=True)   File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py",
> line 374, in check
>     include_deployment_checks=include_deployment_checks,   File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py",
> line 361, in _run_checks
>     return checks.run_checks(**kwargs)   File "/usr/local/lib/python2.7/dist-packages/django/core/checks/registry.py",
> line 81, in run_checks
>     new_errors = check(app_configs=app_configs)   File "/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py",
> line 14, in check_url_config
>     return check_resolver(resolver)   File "/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py",
> line 24, in check_resolver
>     for pattern in resolver.url_patterns:   File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py",
> line 35, in __get__
>     res = instance.__dict__[self.name] = self.func(instance)   File "/usr/local/lib/python2.7/dist-packages/django/urls/resolvers.py",
> line 313, in url_patterns
>     patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)   File
> "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py",
> line 35, in __get__
>     res = instance.__dict__[self.name] = self.func(instance)   File "/usr/local/lib/python2.7/dist-packages/django/urls/resolvers.py",
> line 306, in urlconf_module
>     return import_module(self.urlconf_name)   File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
>     __import__(name)   File "/home/cyro/PycharmProjects/MyClinic/MyClinic/urls.py", line 20, in
> <module>
>     url(r'^agenda/', include('agenda.urls')),   File "/usr/local/lib/python2.7/dist-packages/django/conf/urls/__init__.py",
> line 50, in include
>     urlconf_module = import_module(urlconf_module)   File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
>     __import__(name)   File "/home/cyro/PycharmProjects/MyClinic/agenda/urls.py", line 3, in
> <module>
>     from . import views   File "/home/cyro/PycharmProjects/MyClinic/agenda/views.py", line 8, in
> <module>
>     import rest_framework ImportError: No module named rest_framework
vfh0ocws

vfh0ocws1#

尝试使用sudo pip install djangorestframework,我已经看到了其他的SO问题,它似乎解决了这个问题。

rdrgkggo

rdrgkggo2#

在www.example.com文件中的已安装应用程序中的agenda.apps.AgendaConfig之前添加rest_frameworksettings.py。agenda应用程序似乎在初始化之前使用了一些rest_framework库。

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

    'rest_framework',
    'agenda.apps.AgendaConfig',

]

此答案假定您已成功安装rest_framework并激活了virtualenv

bcs8qyzn

bcs8qyzn3#

我知道这是老问题。但可能有人会发现这个解决方案有帮助。当你使用

pip install djangorestframework

它会把这个包安装在你计算机的本地用户python文件夹中,而django无法读取这个包,所以我们得到
未找到模块错误:没有名为“rest_framework”的模块
相反,我们必须在我们的开发环境中安装这个包。
因此,请改用以下命令

python -m pip install djangorestframework

希望这对你有帮助。

相关问题