没有导入导致的循环依赖?

Circular Dependency caused without import?

代码是 here.

我已经创建了一个文件,results_pb2.py. I then had utils.py导入这个文件。

但是,当我尝试 运行 utils_test.py, I get a circular dependency. For some reason, results_pb2.py is calling another Python program, tcav.py 时,它通过依赖链依赖于 utils.py。这是下面的完整链/堆栈跟踪:

File "/usr/local/home/karanshukla/tcav/tcav/utils_test.py", line 19, in <module>
  from tcav.utils import flatten, process_what_to_run_expand, process_what_to_run_concepts, process_what_to_run_randoms
File "tcav/utils.py", line 20, in <module>
  from tcav.results_pb2 import Result, Results
File "tcav/tcav.py", line 22, in <module>
  from tcav.cav import CAV
File "tcav/cav.py", line 27, in <module>
  from tcav import utils
ImportError: cannot import name utils

奇怪的是 我没有看到 tcav.py 被 results_pb2.py 导入。如果有人能解释导致这种意外导入的原因,我将不胜感激。

作为参考,以下是 results_pb2.py 中的导入:

import sys
from google.protobuf import descriptor as _descriptor
from google.protobuf import message as _message
from google.protobuf import reflection as _reflection
from google.protobuf import symbol_database as _symbol_database

要重现错误,请安装 protoc tool (for generating results_pb2.py), pull the codebase from here 和 运行 以下命令:

pip uninstall tcav
rm tcav/results_pb2.pyc tcav/results_pb2.py
protoc tcav/results.proto --python_out=.
python setup.py bdist_wheel --python-tag py2
pip install dist/tcav-0.2.1-py2-none-any.whl
python -m tcav.utils_test

这很可能是由于 protoc 序列化将整个 tcav 模块作为依赖项造成的。将 results.proto 移出 tcav 模块并移入一个新的单独的 tcav_results 模块解决了这个问题。

这似乎不太理想。但是,它解决了由 protoc.

序列化引起的循环依赖问题