简体   繁体   English

循环依赖导致未导入?

[英]Circular Dependency caused without import?

Code is here . 代码在这里

I have created a file, results_pb2.py . 我创建了一个文件results_pb2.py I then had utils.py import this file. 然后,我将utils.py导入此文件。

However, when I try to run utils_test.py , I get a circular dependency. 但是,当我尝试运行utils_test.py时 ,我得到了循环依赖。 For some reason, results_pb2.py is calling another Python program, tcav.py that depends on utils.py through a chain of dependencies. 由于某种原因,results_pb2.py正在调用另一个Python程序tcav.py ,该程序通过一系列依赖项依赖于utils.py。 Here is the full chain / stack trace below: 这是下面的完整链/堆栈跟踪:

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

The bizarre thing is that I don't see tcav.py being imported by results_pb2.py . 奇怪的是, 我看不到results_pb2.py导入了tcav.py。 I would appreciate if someone could explain what is causing this unexpected import. 如果有人可以解释导致此意外导入的原因,我将不胜感激。

For reference, below are the imports in 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

To reproduce the bug, install the protoc tool (for generating results_pb2.py), pull the codebase from here , and run the following commands: 要重现该错误,请安装protoc工具(用于生成results_pb2.py),从此处拉出代码库,然后运行以下命令:

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

This is most likely caused by the protoc serialization taking the entire tcav module as a dependency. 这很可能是由于protoc序列化将整个tcav模块作为依赖项而引起的。 Moving results.proto outside of the tcav module and into a new, separate tcav_results module fixed the issue. results.proto移到tcav模块之外,并移到新的单独的tcav_results模块中解决了该问题。

This doesn't seem ideal. 这似乎并不理想。 But, it gets around the issue of a circular dependency caused by serializing with protoc . 但是,它解决了由protoc序列化导致的循环依赖的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM