首页 > 解决方案 > 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":

'api' app directory

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!

标签: pythondjangodjango-rest-framework

解决方案


推荐阅读