简体   繁体   中英

Circular Dependency caused without import?

Code is here .

I have created a file, results_pb2.py . I then had utils.py import this file.

However, when I try to run utils_test.py , I get a circular dependency. For some reason, results_pb2.py is calling another Python program, tcav.py that depends on utils.py through a chain of dependencies. 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 . I would appreciate if someone could explain what is causing this unexpected import.

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

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. Moving results.proto outside of the tcav module and into a new, separate tcav_results module fixed the issue.

This doesn't seem ideal. But, it gets around the issue of a circular dependency caused by serializing with protoc .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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