为什么会出现此错误?我正在运行3.8.2的DRF。我想知道docker是否与此有关。也许解释器正在尝试使用/查找我在计算机上安装的其他安装。任何建议/帮助将是巨大的。谢谢
此行失败
from rest_framework.decorators import action
我要替换list_route方法以对此行进行操作
@action(methods=['POST'], permission_classes=[IsAuthenticated, ])
我正在运行在pycharm中创建的virtualenv,并且运行pip Frozen给我:
$ pip freeze
...
Django==1.10.8
django-extensions==2.1.0
django-rest-swagger==2.2.0
django-widget-tweaks==1.4.1
djangorestframework==3.8.2
...
backend_1 | Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7fa4163708c8>
backend_1 | Traceback (most recent call last):
backend_1 | File "/usr/local/lib/python3.6/site-packages/django/utils/autoreload.py", line 226, in wrapper
backend_1 | fn(*args, **kwargs)
backend_1 | File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run
backend_1 | self.check(display_num_errors=True)
backend_1 | File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 374, in check
backend_1 | include_deployment_checks=include_deployment_checks,
backend_1 | File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 361, in _run_checks
backend_1 | return checks.run_checks(**kwargs)
backend_1 | File "/usr/local/lib/python3.6/site-packages/django/core/checks/registry.py", line 81, in run_checks
backend_1 | new_errors = check(app_configs=app_configs)
backend_1 | File "/usr/local/lib/python3.6/site-packages/django/core/checks/urls.py", line 14, in check_url_config
backend_1 | return check_resolver(resolver)
backend_1 | File "/usr/local/lib/python3.6/site-packages/django/core/checks/urls.py", line 24, in check_resolver
backend_1 | for pattern in resolver.url_patterns:
backend_1 | File "/usr/local/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__
backend_1 | res = instance.__dict__[self.name] = self.func(instance)
backend_1 | File "/usr/local/lib/python3.6/site-packages/django/urls/resolvers.py", line 313, in url_patterns
backend_1 | patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
backend_1 | File "/usr/local/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__
backend_1 | res = instance.__dict__[self.name] = self.func(instance)
backend_1 | File "/usr/local/lib/python3.6/site-packages/django/urls/resolvers.py", line 306, in urlconf_module
backend_1 | return import_module(self.urlconf_name)
backend_1 | File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in import_module
backend_1 | return _bootstrap._gcd_import(name[level:], package, level)
backend_1 | File "<frozen importlib._bootstrap>", line 978, in _gcd_import
backend_1 | File "<frozen importlib._bootstrap>", line 961, in _find_and_load
backend_1 | File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
backend_1 | File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
backend_1 | File "<frozen importlib._bootstrap_external>", line 678, in exec_module
backend_1 | File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
backend_1 | File "/project_backend/project_backend/urls.py", line 20, in <module>
backend_1 | from apps.app_name.views import XYZViewSet
backend_1 | File "/project_backend/apps/app_name/views.py", line 4, in <module>
backend_1 | from rest_framework.decorators import action
backend_1 | ImportError: cannot import name 'action'
答案 0 :(得分:3)
我的虚拟环境和docker容器未使用相同的django rest框架版本。我不得不通过docker exec -it <container_id> bash
进入docker容器并运行pip install djangorestframework --upgrade
,这给了我想要的3.8.2版本。
Docker安装了3.4.7,因为我以前在需求文件中有djangorestframework<3.5
,并且容器以该状态存储。我可以删除该容器,然后重新运行docker-compose up
以使用3.8.2的较新版本重新创建该容器。我当然选择只升级pip。