python - ImportError: cannot import name 'ConfigSerializer' from partially initialized module 'api.serializers'
问题描述
I have been trying to understand which part of the app has a circular import but I just can't get the logical flow in this. If I may show you the directory tree of this app called, "api":
When I run a unit test, I get this error:
ERROR: api.serializers (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: api.serializers
Traceback (most recent call last):
File "/usr/local/Cellar/python@3.8/3.8.6/Frameworks/Python.framework/Versions/3.8/lib/python3.8/unittest/loader.py", line 470, in _find_test_path
package = self._get_module_from_name(name)
File "/usr/local/Cellar/python@3.8/3.8.6/Frameworks/Python.framework/Versions/3.8/lib/python3.8/unittest/loader.py", line 377, in _get_module_from_name
__import__(name)
File "/Users/nick/work/shiftwork_backend/api/serializers/__init__.py", line 1, in <module>
from .default import (random_string_generator, WorkTypeSerializer, PatternSerializer,
File "/Users/nick/work/shiftwork_backend/api/serializers/default.py", line 10, in <module>
from api import views
File "/Users/nick/work/shiftwork_backend/api/views/__init__.py", line 1, in <module>
from .base import (ActionBasedPermission, get_server_status, validate_token,
File "/Users/nick/work/shiftwork_backend/api/views/base.py", line 10, in <module>
from api.serializers import ConfigSerializer
ImportError: cannot import name 'ConfigSerializer' from partially initialized module 'api.serializers' (most likely due to a circular import) (/Users/nick/work/shiftwork_backend/api/serializers/__init__.py)
So I looked at the views/base.py and saw this:
import requests
from django.http import JsonResponse
from django.shortcuts import get_object_or_404
from rest_framework.permissions import AllowAny
from api.constants import Constants
from api.models import Config, UserCompany, UserPattern
from api.serializers import ConfigSerializer
and views/init.py looks like below:
from .base import (ActionBasedPermission, get_server_status, validate_token,
get_user_company_by_local_id, get_user_pattern_by_local_id, logger
)
and I checked serializers/default.py:
from api import views
from api.models import (LANGUAGE_CODE_CHOICES, COUNTRY_CODE_CHOICES, SHARE_ON_OFF_TOGGLE,
STATUS_CHOICES, Category, Company, User, Pattern, ChangeWorkSchedule,
UserChangeWorkSchedule, UserWorkCondition, UserColorPreset, WorkType, Holiday,
VacationType, Vacation, VacationData, Salary, Config, OTType, OverTimePeriod,
OTData, OTAutoExceptDate, Memo, Alarm, LockedPattern, UserCompany,
UserWorkSchedule, UserPattern, UserWorkType, WorkTypePeriod, MemoTodo,
CompanyMapping, Share, ShareHistory, Icon, IconVersion, Location, Address, Weather
)
and finally, checked serializers/init.py again:
from .default import (random_string_generator, WorkTypeSerializer, PatternSerializer,
ChangeWorkScheduleSerializer, CategorySerializer, CompanyMiniSerializer,
CompanySerializer, UserCompanySerializer, UserWorkTypeSerializer,
WorkTypePeriodSerializer, UserSerializer, HolidaySerializer,
SalaryPatternSerializer, VacationTypeSerializer, VacationSerializer,
VacationDataSerializer, CompanyMappingSerializer, ConfigSerializer,
OTTypeSerializer, OTPeriodSerializer, OTDataSerializer,
OTAutoExceptDateSerializer, MemoSerializer, MemoTodoSerializer,
AlarmSerializer, LockedPatternSerializer, UserPatternSerializer,
UserWorkScheduleSerializer, UserChangeWorkScheduleSerializer,
UserWorkConditionSerializer, UserColorPresetSerializer,
CustomTokenObtainPairSerializer, CustomTokenObtainPairView,
IconSerializerView, NewsSerializerView
)
from .backup import *
And I still do not understand how I can even begin to fix this.
Thanks a lot!
解决方案
推荐阅读
- javascript - 需要构造函数、方法、访问器或属性。src/app/app.component.ts(129,1):错误 TS1128:需要声明或声明
- python - Pipenv 卡住“⠋ Locking...”
- javascript - Luxon:如何忽略特定日期的默认时区
- java - 如何在我的 TextArea 中显示控制台中显示的文本?
- r - H2O 弹性网络 lambda 搜索不会选择最小化验证偏差的 lambda
- swift - 无法将类型“XXX.Protocol”的值转换为预期的参数类型“XXX.Type”
- python - 使用 pyfmi 创建 2 元组作为 fmu 模型的 model.simulate() 的输入时出错
- c++ - 为什么我不能像 someClassObject++5 那样在后缀 operator++ 中使用虚拟参数?
- html - 有没有办法播放在 html5 中同步的多个音频文件?
- automated-tests - 脚本内的Testcafe记者