Defintion of global variables in Cython

Hi,
I have a main python file (main.py) which declares a variable W as follows:
W =
main.py imports a cython file 'my_lib.pyx" consisting of multiple cdef or cpdef functions that use or modify this W. Note that W may also be used or modified in main.py.

The question is how do we declare W in cython functions of ‘my_lib.pyx’ so that they regard W as a global function. It seems that declaring W as global either within these functions or outside is not honored and the tool complains that W is not known.

I meant “How do we declare W in cython functions of ‘my_lib.pyx’ so that these function regard W as a global variable”?

I don’t know about Cython specifically, but if it’s like Python, then “global” means global to the module. The file that you run directly (not through importing it) is given the name __main__. If you want to access it from another module, you would write:

import __main__

Then, like any other module you import, you’d be able to refer to names in that file with (for example):

__main__.W

I don’t know if there’s any way that’s specific to Cython for doing this.