6

I'm trying Pypy compiler to see if I can speed up my code. Nevertheless, I'm having troubles with MySQLdb module, which Pypy is unable to find.

I have read that MySQLdb 1.2.4 should work fine with Pypy, so I upgraded the module, and I tested that it is the right version with CPython compiler:

import MySQLdb
MySQLdb.__version__
>> '1.2.4'

But when using Pypy, I'm getting:

Python 2.7.2 (1.9+dfsg-1, Jun 19 2012, 23:23:45)
[PyPy 1.9.0 with GCC 4.7.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
And now for something completely different: ``-FIRST they ignore you, then they
laugh at you, then they fight you, then you win.-''
>>>> import MySQLdb
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ImportError: No module named MySQLdb

Any help? I'm running over Ubuntu 13.04 and using Pypy which came into the Canonical repositories.

Emil Vikström
  • 90,431
  • 16
  • 141
  • 175
Roman Rdgz
  • 12,836
  • 41
  • 131
  • 207

1 Answers1

13

MySQLdb is mostly written in C which pypy can't use directly. You'd need to patch and recompile it.

The easier solution would be to use a pure python mysql connector library, like pymysql or mysql-connector-python

pymysql can even be used as dropin replacement for MySQLdb, all you need to do add:

import pymysql
pymysql.install_as_MySQLdb()

or put this in a module MySQLdb.py, after that code which imports MySQLdb should work normally.

mata
  • 67,110
  • 10
  • 163
  • 162
  • Is pymysql much slower than MySQLdb? Without pypy I'm getting twice as much processing time – Roman Rdgz Jun 26 '13 at 15:40
  • it should be way better on pypy then on cpython, but i haven't done any benchmarks. usually the bottleneck is the database performance anyway. – mata Jun 26 '13 at 16:35
  • 1
    OK, I've done som very simple test with a bunch of inserts, queries and deletes on a test table. cpython2.7.4+MySQLdb: `50 loops, best of 3: 80 msec per loop`, pypy1.9.0+pymysql: `50 loops, best of 3: 89.7 msec per loop` - so for this testcase pypy+pymysql was about 12% slower - but the result may not really be representative for mor complex usecases. – mata Jun 26 '13 at 17:04
  • Yeah without pypy pymysql is very slow. I am using oursql and tried to convert to pymysql for pypy compatibility but now processing is way too high. – radtek Jan 13 '18 at 19:40