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?
2 Answers
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.")
-
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.– RTbecardJan 3, 2022 at 14:51
-
3Depends 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 trypandas.__file__
)– MB.Jan 4, 2022 at 2:15
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.
-
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 ! Apr 2, 2015 at 9:49
import platform; platform.python_implementation()
is what you want.cython
isn't an implementation in the same sense thatCPython
orpypy
is.