简体   繁体   中英

Calling Python function from Cython code, inside a Google Colab notebook

I am working in a Google Colab notebook. There is one particular, computationally intensive piece of code that I'm doing using Cython in the same notebook. Within this piece of code, I want to call a function (defined in another cell in the same notebook, in Python).

Now, that function is heavily integrated with the rest of my pure Python code and rewriting and redefining things for Cython would not be possible.

My question is: How do I call that function written in Python, from another cell that is getting compiled in Cython?

Link I have already looked at: Call python file with python imports from C using cython

Normally, you would put the whole functionality into a module and import it in the %%cython -cell.

Another less clean (but in case of a notebook probably acceptable) way would be to import from __main__ , eg:

[1]: def foo():
         print("I'm main foo")

and then:

[2]: %%cython
     def usefoo():
         from __main__ import foo
         foo()

and now:

[3]: usefoo()
I'm main foo

Another variant would be to import foo from __main__ slightly differently:

[2]: %%cython
     from __main__ import foo
     def usefoo2():
         foo()

There are two main differences:

  1. if foo isn't (yet) defined in __main__ , second %%cython -cell will fail. First version will fail if foo is not or no longer defined during the call of the function usefoo .
  2. if foo is changed in __main__ , the first version will use the current version while the second version will always use the version from the moment %%cython -cell built (which might not be the same time the %%cython -cell is run due to caching). This can be quite confusing.

In the long run, this way is quite confusing and puzzling, so after short try-out phase I would change to a more sustainable approach using dedicated modules.

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