11

Is there a reliable way to check at runtime if some python code is "cythonized" or if it us running into a standard Python interpreter?

feetwet
  • 3,248
  • 7
  • 46
  • 84
user3274434
  • 151
  • 1
  • 10
  • 4
    I'm confused, you are forced to use a different syntax for cython code, yes? How could you not know? Why do you need to know this? – Brian Cain Mar 20 '15 at 15:21
  • 7
    @BrianCain: Cython can speed up pure Python code as well as Cython code. This is not an unreasonable thing to ask. – Kevin Mar 20 '15 at 16:07
  • 3
    Perhaps `import platform; platform.python_implementation()` is what you want. – Brian Cain Mar 20 '15 at 21:20
  • `cython` isn't an implementation in the same sense that `CPython` or `pypy` is. – hpaulj Mar 21 '15 at 22:46
  • @BrianCain I do not *need* to know this, I'm curious to know whether it is possible or not, that's all. – user3274434 Apr 02 '15 at 09:55

2 Answers2

16

Answering this because this question comes up as a top response on search engines and the answers aren't helpful.

From http://docs.cython.org/en/latest/src/tutorial/pure.html

import cython
if cython.compiled:
    print("Yep, I'm compiled.")
else:
    print("Just a lowly interpreted script.")
Community
  • 1
  • 1
MB.
  • 1,303
  • 12
  • 19
  • I might be missing something... but I think this answer is worse than the earlier posted by @hpaulj. Both answers will tell you if your imported module has been compiled or not by cython. However, hpaulj's doesn't require any modification of the modules source code to do so, so its a true _runtime_ solution IMO. – RTbecard Jan 03 '22 at 14:51
  • 3
    Depends on whether the question is with regards to code I own or code I am consuming (library). There are plenty of scenarios where I, as the writer of the code, want to know if this particular code is running in pure python or cython. Don't take my word for it: it's evidently important enough that the makers of cython *specifically* made an attribute to detect it. Furthermore, *inferring* whether code is compiled by looking either at `repr` or `__file__` is bound to get you in trouble: many cython projects have thin python wrappers and compile hot functions only (e.g try `pandas.__file__`) – MB. Jan 04 '22 at 02:15
5

When I import similar modules and look at their .__file__ attributes I get

In [205]: which.__file__
Out[205]: .../which.py'

In [206]: which_cy.__file__
Out[206]: '.../which_cy.cpython-34m.so'

The cythonized module is imported from a .so file, while the python source is .py (or .pyc). But there are other ways of compiling code which will also create .so files. So this doesn't distinguish among compilation routes.

And for individual functions:

In [225]: repr(which.nonzero)
Out[225]: '<function nonzero at 0xb4dd2d1c>'

In [226]: repr(which_cy.nonzero_cy)
Out[226]: '<built-in function nonzero_cy>'

cython does not 'run' code. It translates a file (e.g. *.pyx) into C code. That is then compiled, producing a loadable module (the .so) file. That is then imported and run by the Python interpreter. I don't know if there's a way of running the .so code as a stand along executable.

Or, are you really wondering whether the code in a given compiled module is using tight C code as opposed to calls to Python functions (mainly Python objects and their methods)? You can look at the C code for that. Both are using compiled code, but one has a lot of overhead from checking types, bounds, etc. The whole point to Cython tutorials is ways to shift away the Python generality to faster C definitions.

hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • I realize my question was not very clear but thanks for your answer. Actually I was just curious to see if it was possible to tell appart a .so module produced with Cython or whatever other way to provide a compiled python/C module and a standard .py import. Your answer with the `.__file__` attribute is fine and I wonder why I did not came to that conclusion by myself ! So thanks a lot ! – user3274434 Apr 02 '15 at 09:49