EricFillion / happy-transformer

Happy Transformer makes it easy to fine-tune and perform inference with NLP Transformer models.
http://happytransformer.com
Apache License 2.0
517 stars 66 forks source link

circular import issue when importing with `transformers` and `happytransformer` #298

Closed maifeeulasad closed 2 years ago

maifeeulasad commented 2 years ago

I'm trying to create a basic project with

Here is the entry file(code.py):

from ping import onichan
from pong import araara

if __name__ == '__main__':
    pass    

A module called ping.py:

from happytransformer import HappyTextToText, TTSettings

def onichan():
    print('onichan')

Another module called pong.py:

from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

def araara():
    print('araara')

And I have a file named __init__.py to initialize the module.

But this throws some error saying, circular import

Directory structure:

│---code.py
│---ping.py
│---pong.py
│---__init__.py

Complete Trace:

C:\Users\ShibaInu\Desktop\err>python code.py
2022-08-28 18:51:13.374326: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2022-08-28 18:51:13.374507: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
Traceback (most recent call last):
  File "C:\Python310\lib\site-packages\transformers\utils\import_utils.py", line 1002, in _get_module
    return importlib.import_module("." + module_name, self.__name__)
  File "C:\Python310\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "C:\Python310\lib\site-packages\transformers\pipelines\__init__.py", line 37, in <module>
    from .audio_classification import AudioClassificationPipeline
  File "C:\Python310\lib\site-packages\transformers\pipelines\audio_classification.py", line 20, in <module>
    from .base import PIPELINE_INIT_ARGS, Pipeline
  File "C:\Python310\lib\site-packages\transformers\pipelines\base.py", line 34, in <module>
    from ..modelcard import ModelCard
  File "C:\Python310\lib\site-packages\transformers\modelcard.py", line 44, in <module>
    from .training_args import ParallelMode
  File "C:\Python310\lib\site-packages\transformers\training_args.py", line 26, in <module>
    from .trainer_utils import (
  File "C:\Python310\lib\site-packages\transformers\trainer_utils.py", line 47, in <module>
    import tensorflow as tf
  File "C:\Python310\lib\site-packages\tensorflow\__init__.py", line 37, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "C:\Python310\lib\site-packages\tensorflow\python\__init__.py", line 42, in <module>
    from tensorflow.python import data
  File "C:\Python310\lib\site-packages\tensorflow\python\data\__init__.py", line 21, in <module>
    from tensorflow.python.data import experimental
  File "C:\Python310\lib\site-packages\tensorflow\python\data\experimental\__init__.py", line 95, in <module>
    from tensorflow.python.data.experimental import service
  File "C:\Python310\lib\site-packages\tensorflow\python\data\experimental\service\__init__.py", line 387, in <module>
    from tensorflow.python.data.experimental.ops.data_service_ops import distribute
  File "C:\Python310\lib\site-packages\tensorflow\python\data\experimental\ops\data_service_ops.py", line 22, in <module>
    from tensorflow.python.data.experimental.ops import compression_ops
  File "C:\Python310\lib\site-packages\tensorflow\python\data\experimental\ops\compression_ops.py", line 16, in <module>
    from tensorflow.python.data.util import structure
  File "C:\Python310\lib\site-packages\tensorflow\python\data\util\structure.py", line 22, in <module>
    from tensorflow.python.data.util import nest
  File "C:\Python310\lib\site-packages\tensorflow\python\data\util\nest.py", line 36, in <module>
    from tensorflow.python.framework import sparse_tensor as _sparse_tensor
  File "C:\Python310\lib\site-packages\tensorflow\python\framework\sparse_tensor.py", line 24, in <module>
    from tensorflow.python.framework import constant_op
  File "C:\Python310\lib\site-packages\tensorflow\python\framework\constant_op.py", line 25, in <module>
    from tensorflow.python.eager import execute
  File "C:\Python310\lib\site-packages\tensorflow\python\eager\execute.py", line 24, in <module>
    from tensorflow.python.framework import ops
  File "C:\Python310\lib\site-packages\tensorflow\python\framework\ops.py", line 23, in <module>
    from absl import app
  File "C:\Python310\lib\site-packages\absl\app.py", line 31, in <module>
    import pdb
  File "C:\Python310\lib\pdb.py", line 77, in <module>
    import code
  File "C:\Users\ShibaInu\Desktop\err\code.py", line 1, in <module>
    from ping import onichan
ImportError: cannot import name 'onichan' from partially initialized module 'ping' (most likely due to a circular import) (C:\Users\ShibaInu\Desktop\err\ping.py)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\ShibaInu\Desktop\err\code.py", line 1, in <module>
    from ping import onichan
  File "C:\Users\ShibaInu\Desktop\err\ping.py", line 1, in <module>
    from happytransformer import HappyTextToText, TTSettings
  File "C:\Python310\lib\site-packages\happytransformer\__init__.py", line 1, in <module>
    from happytransformer.happy_question_answering import HappyQuestionAnswering
  File "C:\Python310\lib\site-packages\happytransformer\happy_question_answering.py", line 7, in <module>
    from transformers import QuestionAnsweringPipeline, AutoModelForQuestionAnswering
  File "<frozen importlib._bootstrap>", line 1075, in _handle_fromlist
  File "C:\Python310\lib\site-packages\transformers\utils\import_utils.py", line 992, in __getattr__
    module = self._get_module(self._class_to_module[name])
  File "C:\Python310\lib\site-packages\transformers\utils\import_utils.py", line 1004, in _get_module
    raise RuntimeError(
RuntimeError: Failed to import transformers.pipelines because of the following error (look up to see its traceback):
cannot import name 'onichan' from partially initialized module 'ping' (most likely due to a circular import) (C:\Users\ShibaInu\Desktop\err\ping.py)
maifeeulasad commented 2 years ago

ref: https://github.com/huggingface/transformers/issues/18790

ted537 commented 2 years ago

The problem is you have code.py, which is imported by something in the standard library. Unrelated to happy transformer.

maifeeulasad commented 2 years ago

It got resolved after restarting the machine.

But code.py was the entry point.