[pypy-dev] Detecting pypy vs cpython runtime

Blaine frikker at gmail.com
Tue Nov 15 19:16:25 CET 2011


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 at gmail.com> wrote:

>
>
> On Tue, Nov 15, 2011 at 1:08 PM, Maciej Fijalkowski <fijall at gmail.com>wrote:
>
>> On Tue, Nov 15, 2011 at 8:03 PM, Alex Gaynor <alex.gaynor at gmail.com>
>> wrote:
>> >
>> >
>> > On Tue, Nov 15, 2011 at 12:56 PM, Blaine <frikker at 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 at 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 at 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pypy-dev/attachments/20111115/dcb607e7/attachment-0001.html>


More information about the pypy-dev mailing list