socialpoint-labs/unity-yaml-parser

Circular import error

FerzanGameflex opened this issue ยท 7 comments

Hi,

I'm getting the following error:

ImportError: cannot import name 'UnityDocument' from partially initialized module 'unityparser' (most likely due to a circular import) (/Users/ferzan/Cortex/lib/python3.9/site-packages/unityparser/init.py)`

pip install -U unityparser

Code is just:

from unityparser import UnityDocument

I cannot reproduce the error with those simple steps with the interactive terminal, even using python3.9.

Can you give a full example ?

Or try with a clean python environment with just this package installed ?

Package works great when I use interactive terminal but if I try to call it with

python myscript.py

It gives the circular import error.

myscript is simply this:

from unityparser import UnityDocument

doc = UnityDocument.load_yaml("yam.asset")

Weird thing is, after calling the script from python and getting the circular import error, interactive terminal also starts giving the circular import error when I call:

from unityparser import UnityDocument

I'm using MacOS with Python 3.9.5

I still could not reproduce the error(I have the exact same environment as yours).

Could provide the sample Yaml file you use and copy the full error stack trace ?

Sure, here's the full stack trace and related files.

(Cortex) ferzan@Ferzans-MacBook-Pro Desktop % python yaml.py
Traceback (most recent call last):
File "/Users/ferzan/Desktop/yaml.py", line 1, in
from unityparser import UnityDocument
File "/Users/ferzan/Cortex/lib/python3.9/site-packages/unityparser/init.py", line 3, in
from .utils import UnityDocument
File "/Users/ferzan/Cortex/lib/python3.9/site-packages/unityparser/utils.py", line 3, in
from .dumper import UnityDumper
File "/Users/ferzan/Cortex/lib/python3.9/site-packages/unityparser/dumper.py", line 1, in
from yaml.serializer import Serializer
File "/Users/ferzan/Desktop/yaml.py", line 1, in
from unityparser import UnityDocument
ImportError: cannot import name 'UnityDocument' from partially initialized module 'unityparser' (most likely due to a circular import) (/Users/ferzan/Cortex/lib/python3.9/site-packages/unityparser/init.py)

yam.asset.zip
requirements.txt
yaml.py.zip

Thanks ! I can reproduce the issue now, super strage behavior ๐Ÿ‘€

So the problem I believe is that your script file is named yaml.py and is messing up with the yaml(PyYAML) module name causing a wrong dependency cycle.

You should rename your python script file(and make sure to remove all pycache_ folders).

So the problem I believe is that your script file is named yaml.py and is messing up with the yaml(PyYAML) module name causing a wrong dependency cycle.

You should rename your python script file(and make sure to remove all pycache_ folders).

I wish there was a facepalm emoji here :) Thanks for looking into this. Closing the issue.