Detecting pypy vs cpython runtime

Hi everyone. I have a cellular automata framework in C++ and I use cython and cpython to run it. I found out that if I port it to pure python and run it with pypy, it's close to the same performance as the C++ version. (about 2x as slow, compared to 20x as slow when using pure python + cpython). When I throw in other overheads with pure python libraries, using the pure python and pypy is much faster than cpython with the C++ library, all things equal. What I'd like to do is detect if pypy or cpython is doing the importing of my module, and switch over to the pure python interface if pypy is found. As it stands I have to do it manually in my module's __init__.py. *Is there any way to detect if my module is being imported by pypy vs cpython?* Either via sys, or maybe some latent variable that is present, or something else. sys.argv[0] only has the script name (obviously), not the interpreter call. Keep up the outstanding work. Pypy is great! Thanks! Blaine

On Tue, Nov 15, 2011 at 12:56 PM, Blaine <frikker@gmail.com> wrote:
Hi everyone. I have a cellular automata framework in C++ and I use cython and cpython to run it. I found out that if I port it to pure python and run it with pypy, it's close to the same performance as the C++ version. (about 2x as slow, compared to 20x as slow when using pure python + cpython). When I throw in other overheads with pure python libraries, using the pure python and pypy is much faster than cpython with the C++ library, all things equal.
What I'd like to do is detect if pypy or cpython is doing the importing of my module, and switch over to the pure python interface if pypy is found. As it stands I have to do it manually in my module's __init__.py.
*Is there any way to detect if my module is being imported by pypy vs cpython?* Either via sys, or maybe some latent variable that is present, or something else. sys.argv[0] only has the script name (obviously), not the interpreter call.
Keep up the outstanding work. Pypy is great!
Thanks! Blaine
_______________________________________________ pypy-dev mailing list pypy-dev@python.org http://mail.python.org/mailman/listinfo/pypy-dev
The canonical way is probably `import platform; platform.python_implementation()` which will return either "PyPy" or "CPython". Alex -- "I disapprove of what you say, but I will defend to the death your right to say it." -- Evelyn Beatrice Hall (summarizing Voltaire) "The people's good is the highest law." -- Cicero

On Tue, Nov 15, 2011 at 8:03 PM, Alex Gaynor <alex.gaynor@gmail.com> wrote:
On Tue, Nov 15, 2011 at 12:56 PM, Blaine <frikker@gmail.com> wrote:
Hi everyone. I have a cellular automata framework in C++ and I use cython and cpython to run it. I found out that if I port it to pure python and run it with pypy, it's close to the same performance as the C++ version. (about 2x as slow, compared to 20x as slow when using pure python + cpython). When I throw in other overheads with pure python libraries, using the pure python and pypy is much faster than cpython with the C++ library, all things equal. What I'd like to do is detect if pypy or cpython is doing the importing of my module, and switch over to the pure python interface if pypy is found. As it stands I have to do it manually in my module's __init__.py. Is there any way to detect if my module is being imported by pypy vs cpython? Either via sys, or maybe some latent variable that is present, or something else. sys.argv[0] only has the script name (obviously), not the interpreter call. Keep up the outstanding work. Pypy is great! Thanks! Blaine
_______________________________________________ pypy-dev mailing list pypy-dev@python.org http://mail.python.org/mailman/listinfo/pypy-dev
The canonical way is probably `import platform; platform.python_implementation()` which will return either "PyPy" or "CPython". Alex
If you need to support older pythons (which don't have platform.python_implementation), we use import sys is_pypy = '__pypy__' in sys.builtin_module_names or alternatively: try: import __pypy__ except ImportError: __pypy__ = None
-- "I disapprove of what you say, but I will defend to the death your right to say it." -- Evelyn Beatrice Hall (summarizing Voltaire) "The people's good is the highest law." -- Cicero
_______________________________________________ pypy-dev mailing list pypy-dev@python.org http://mail.python.org/mailman/listinfo/pypy-dev

On Tue, Nov 15, 2011 at 1:08 PM, Maciej Fijalkowski <fijall@gmail.com>wrote:
On Tue, Nov 15, 2011 at 8:03 PM, Alex Gaynor <alex.gaynor@gmail.com> wrote:
On Tue, Nov 15, 2011 at 12:56 PM, Blaine <frikker@gmail.com> wrote:
Hi everyone. I have a cellular automata framework in C++ and I use
and cpython to run it. I found out that if I port it to pure python and run it with pypy, it's close to the same performance as the C++ version. (about 2x as slow, compared to 20x as slow when using pure python + cpython). When I throw in other overheads with pure python libraries, using the pure
and pypy is much faster than cpython with the C++ library, all things equal. What I'd like to do is detect if pypy or cpython is doing the importing of my module, and switch over to the pure python interface if pypy is found. As it stands I have to do it manually in my module's __init__.py. Is there any way to detect if my module is being imported by pypy vs cpython? Either via sys, or maybe some latent variable that is present, or something else. sys.argv[0] only has the script name (obviously), not
cython python the
interpreter call. Keep up the outstanding work. Pypy is great! Thanks! Blaine
_______________________________________________ pypy-dev mailing list pypy-dev@python.org http://mail.python.org/mailman/listinfo/pypy-dev
The canonical way is probably `import platform; platform.python_implementation()` which will return either "PyPy" or "CPython". Alex
If you need to support older pythons (which don't have platform.python_implementation), we use
import sys is_pypy = '__pypy__' in sys.builtin_module_names
or alternatively:
try: import __pypy__ except ImportError: __pypy__ = None
-- "I disapprove of what you say, but I will defend to the death your right
to
say it." -- Evelyn Beatrice Hall (summarizing Voltaire) "The people's good is the highest law." -- Cicero
_______________________________________________ pypy-dev mailing list pypy-dev@python.org http://mail.python.org/mailman/listinfo/pypy-dev
And don't forget `hasattr(sys, "pypy_translation_info")` just for completeness, platform is the cleanest IMO though :) Alex -- "I disapprove of what you say, but I will defend to the death your right to say it." -- Evelyn Beatrice Hall (summarizing Voltaire) "The people's good is the highest law." -- Cicero

Awesome. Thanks everyone, exactly what I was looking for. Thank you! Blaine On Tue, Nov 15, 2011 at 1:10 PM, Alex Gaynor <alex.gaynor@gmail.com> wrote:
On Tue, Nov 15, 2011 at 1:08 PM, Maciej Fijalkowski <fijall@gmail.com>wrote:
On Tue, Nov 15, 2011 at 8:03 PM, Alex Gaynor <alex.gaynor@gmail.com> wrote:
On Tue, Nov 15, 2011 at 12:56 PM, Blaine <frikker@gmail.com> wrote:
Hi everyone. I have a cellular automata framework in C++ and I use
and cpython to run it. I found out that if I port it to pure python and run it with pypy, it's close to the same performance as the C++ version. (about 2x as slow, compared to 20x as slow when using pure python + cpython). When I throw in other overheads with pure python libraries, using the pure
and pypy is much faster than cpython with the C++ library, all things equal. What I'd like to do is detect if pypy or cpython is doing the importing of my module, and switch over to the pure python interface if pypy is found. As it stands I have to do it manually in my module's __init__.py. Is there any way to detect if my module is being imported by pypy vs cpython? Either via sys, or maybe some latent variable that is
something else. sys.argv[0] only has the script name (obviously), not
cython python present, or the
interpreter call. Keep up the outstanding work. Pypy is great! Thanks! Blaine
_______________________________________________ pypy-dev mailing list pypy-dev@python.org http://mail.python.org/mailman/listinfo/pypy-dev
The canonical way is probably `import platform; platform.python_implementation()` which will return either "PyPy" or "CPython". Alex
If you need to support older pythons (which don't have platform.python_implementation), we use
import sys is_pypy = '__pypy__' in sys.builtin_module_names
or alternatively:
try: import __pypy__ except ImportError: __pypy__ = None
-- "I disapprove of what you say, but I will defend to the death your
right to
say it." -- Evelyn Beatrice Hall (summarizing Voltaire) "The people's good is the highest law." -- Cicero
_______________________________________________ pypy-dev mailing list pypy-dev@python.org http://mail.python.org/mailman/listinfo/pypy-dev
And don't forget `hasattr(sys, "pypy_translation_info")` just for completeness, platform is the cleanest IMO though :)
Alex
-- "I disapprove of what you say, but I will defend to the death your right to say it." -- Evelyn Beatrice Hall (summarizing Voltaire) "The people's good is the highest law." -- Cicero
participants (3)
-
Alex Gaynor
-
Blaine
-
Maciej Fijalkowski