Python 导入错误:无法导入名称';CustomerIdNameSerializer';从';contacts.api.serializers';

Python 导入错误:无法导入名称';CustomerIdNameSerializer';从';contacts.api.serializers';,python,django,pytest,Python,Django,Pytest,我一直在尝试为pytest设计一些单元测试。当我执行“python manage.py runserver”时,服务器运行正常。但是,当我执行“pytest”时,它会显示以下错误: ============================ test session starts ============================= platform darwin -- Python 3.7.9, pytest-6.2.2, py-1.10.0, pluggy-0.13.1 django:

我一直在尝试为pytest设计一些单元测试。当我执行“python manage.py runserver”时,服务器运行正常。但是,当我执行“pytest”时,它会显示以下错误:

============================ test session starts =============================
platform darwin -- Python 3.7.9, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
django: settings: tradeforce.settings.dev (from ini)
rootdir: /Users/woohyunan/projects/work/tradir/backend-app, configfile: pytest.ini
plugins: fixture-order-0.1.3, drf-1.1.2, common-subject-1.0.5, lambda-1.2.4, django-4.1.0, assert-utils-0.2.1
collected 0 items
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/_pytest/python.py", line 578, in _importtestmodule
INTERNALERROR>     mod = import_path(self.fspath, mode=importmode)
INTERNALERROR>   File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/_pytest/pathlib.py", line 531, in import_path
INTERNALERROR>     importlib.import_module(module_name)
INTERNALERROR>   File "/Users/woohyunan/.pyenv/versions/3.7.9/lib/python3.7/importlib/__init__.py", line 127, in import_module
INTERNALERROR>     return _bootstrap._gcd_import(name[level:], package, level)
INTERNALERROR>   File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
INTERNALERROR>   File "<frozen importlib._bootstrap>", line 983, in _find_and_load
INTERNALERROR>   File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
INTERNALERROR>   File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
INTERNALERROR>   File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/_pytest/assertion/rewrite.py", line 170, in exec_module
INTERNALERROR>     exec(co, module.__dict__)
INTERNALERROR>   File "/Users/woohyunan/projects/work/tradir/backend-app/contacts/tests/integration_tests.py", line 4, in <module>
INTERNALERROR>     from contacts.api.views import CustomerCreateAPIView
INTERNALERROR>   File "/Users/woohyunan/projects/work/tradir/backend-app/contacts/api/views.py", line 15, in <module>
INTERNALERROR>     from contacts.api.serializers import (
INTERNALERROR>   File "/Users/woohyunan/projects/work/tradir/backend-app/contacts/api/serializers.py", line 7, in <module>
INTERNALERROR>     import user.api.serializers
INTERNALERROR>   File "/Users/woohyunan/projects/work/tradir/backend-app/user/api/serializers.py", line 20, in <module>
INTERNALERROR>     from contacts.api.serializers import (
INTERNALERROR> ImportError: cannot import name 'CustomerIdNameSerializer' from 'contacts.api.serializers' (/Users/woohyunan/projects/work/tradir/backend-app/contacts/api/serializers.py)
INTERNALERROR>
INTERNALERROR> The above exception was the direct cause of the following exception:
INTERNALERROR>
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/_pytest/main.py", line 269, in wrap_session
INTERNALERROR>     session.exitstatus = doit(config, session) or 0
INTERNALERROR>   File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/_pytest/main.py", line 322, in _main
INTERNALERROR>     config.hook.pytest_collection(session=session)
INTERNALERROR>   File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/pluggy/hooks.py", line 286, in __call__
INTERNALERROR>     return self._hookexec(self, self.get_hookimpls(), kwargs)
INTERNALERROR>   File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/pluggy/manager.py", line 93, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR>   File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/pluggy/manager.py", line 87, in <lambda>
INTERNALERROR>     firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
INTERNALERROR>   File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/pluggy/callers.py", line 208, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/pluggy/callers.py", line 80, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/pluggy/callers.py", line 187, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/_pytest/main.py", line 333, in pytest_collection
INTERNALERROR>     session.perform_collect()
INTERNALERROR>   File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/_pytest/main.py", line 634, in perform_collect
INTERNALERROR>     self.items.extend(self.genitems(node))
INTERNALERROR>   File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/_pytest/main.py", line 808, in genitems
INTERNALERROR>     rep = collect_one_node(node)
INTERNALERROR>   File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/_pytest/runner.py", line 457, in collect_one_node
INTERNALERROR>     ihook.pytest_collectstart(collector=collector)
INTERNALERROR>   File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/pluggy/hooks.py", line 286, in __call__
INTERNALERROR>     return self._hookexec(self, self.get_hookimpls(), kwargs)
INTERNALERROR>   File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/pluggy/manager.py", line 93, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR>   File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/pluggy/manager.py", line 87, in <lambda>
INTERNALERROR>     firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
INTERNALERROR>   File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/pluggy/callers.py", line 208, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/pluggy/callers.py", line 80, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/pluggy/callers.py", line 187, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/pytest_lambda/plugin.py", line 11, in pytest_collectstart
INTERNALERROR>     process_lambda_fixtures(collector.module)
INTERNALERROR>   File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/_pytest/python.py", line 272, in module
INTERNALERROR>     return node.obj if node is not None else None
INTERNALERROR>   File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/_pytest/python.py", line 291, in obj
INTERNALERROR>     self._obj = obj = self._getobj()
INTERNALERROR>   File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/_pytest/python.py", line 500, in _getobj
INTERNALERROR>     return self._importtestmodule()
INTERNALERROR>   File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/_pytest/python.py", line 608, in _importtestmodule
INTERNALERROR>     ) from e
INTERNALERROR> _pytest.nodes.Collector.CollectError: ImportError while importing test module '/Users/woohyunan/projects/work/tradir/backend-app/contacts/tests/integration_tests.py'.
INTERNALERROR> Hint: make sure your test modules/packages have valid Python names.
INTERNALERROR> Traceback:
INTERNALERROR> ../../../../.pyenv/versions/3.7.9/lib/python3.7/importlib/__init__.py:127: in import_module
INTERNALERROR>     return _bootstrap._gcd_import(name[level:], package, level)
INTERNALERROR> contacts/tests/integration_tests.py:4: in <module>
INTERNALERROR>     from contacts.api.views import CustomerCreateAPIView
INTERNALERROR> contacts/api/views.py:15: in <module>
INTERNALERROR>     from contacts.api.serializers import (
INTERNALERROR> contacts/api/serializers.py:7: in <module>
INTERNALERROR>     import user.api.serializers
INTERNALERROR> user/api/serializers.py:20: in <module>
INTERNALERROR>     from contacts.api.serializers import (
INTERNALERROR> E   ImportError: cannot import name 'CustomerIdNameSerializer' from 'contacts.api.serializers' (/Users/woohyunan/projects/work/tradir/backend-app/contacts/api/serializers.py)
有问题的序列化程序位于: contacts/api/serializers.py

.
.
.
class CustomerIdNameSerializer(serializers.ModelSerializer):
    class Meta:
        model = Customer
        fields = ('id', 'name', )
.
.
.

如果这不是由循环导入引起的,那么在执行测试时如何发生此错误?

来自app\u name import api

# to use serializer
serializer=api.serializeFileName

你有没有尝试过在这个帖子中发布的解决方案?是的。尽管触摸contacts/api/\uuuu init\uuuuuuuuuuuuupy,但未解决此问题。可能其他软件包也受影响。仍然没有修复它。。
# to use serializer
serializer=api.serializeFileName