I have typical Cython code (.pyx) with an exception that it needs to access the variable from Python module:
helloworld.pyx:
from hellotest import label
def say_hello():
print('Hello world!' + label)
And hellotest.py
from helloworld import say_hello
label = 'some label'
say_hello()
When I try to compile it with usual python setup.py build_ext --inplace, I get an error: Traceback (most recent call last): File "hellotest.py", line 1, in from helloworld import say_hello File "helloworld.pyx", line 2, in init helloworld (helloworld.c:967) from hellotest import label File "/home/dusan/projects/temp/hellotest.py", line 1, in from helloworld import say_hello ImportError: cannot import name say_hello
However, when I just remove label dependency (the very first line in helloworld.pyx and label in print statemet), code compiles and runs fine.
Can someone please tell me, how to access other module from within pyx code? Thanks!
The problem was that you tried to import hellotest into helloworld and to import helloworld into hellotest. This is called a circular import, which means that the dependency-graph, where the nodes are the files to import and the vertices are the import statements, so, this graph has a circle, therefore it will never be a forest. This means that you need to read hellotest to be able to read helloworld, but you need to read helloworld to read hellotest. If you imagine something which tries to analyze the dependencies, then you will quickly realize that the circle creates an infinite loop. Try to not form circles with your import statements in the future and you will be safe against this kind of problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With