What is PyMySQL and how does it differ from MySQLd

2019-01-10 11:21发布

I just solved some problems in my Django 1.3 app by using PyMySQL instead of MySQLdb. I followed this tutorial on how to make the switch: http://web-eng-help.blogspot.com/2010/09/install-mysql-5-for-python-26-and.html

Now I want to know what PyMySQL actually is and how it is different from MySQLdb.

I am using it on localhost and will then upload it to some hosting.

Is it fine to use PyMySQL on localhost and on hosting whatever they provide? Since I have changed "MySQLdb" in base.py and introspection.py to "PyMySQL", will I need to upload it to the server after changing these files? Or as it is Django's files, since Django will be uploaded there already, does it not matter much?

4条回答
劳资没心,怎么记你
2楼-- · 2019-01-10 11:41

PyMySQL and MySQLdb provide the same functionality - they are both database connectors. The difference is in the implementation where MySQLdb is a C extension and PyMySQL is pure Python.

There are a few reasons to try PyMySQL:

  • it might be easier to get running on some systems
  • it works with PyPy
  • it can be "greened" and works with gevent

The proper way to use it with Django is to import it and tell it to impersonate MySQLdb in your top-level file, usually manage.py. Put the following code at the very top of your manage.py (or whatever file you call when starting your server):

try:
    import pymysql
    pymysql.install_as_MySQLdb()
except ImportError:
    pass
查看更多
Ridiculous、
3楼-- · 2019-01-10 11:43

Your first point:

According to pymysql wiki page:

MySQLdb, is a C extension module that has a reputation of being difficult to compile, especially if you're on a Mac. Additionally, end-users need to wait for new binaries to be compiled for each new release of Python, and MySQLdb will never run on Jython, IronPython, or PyPy (without something like cpyext or IronClad). We also maintain 100% compatibility between Python 2 and Python 3, so all advancements made on the 2.x trunk will be immediately available on Python 3.

Your second point:

If django is working fine on your localhost, then it should be fine on your development.

查看更多
冷血范
4楼-- · 2019-01-10 11:48

As per my experience I had difficulty in installing "MySQL-python" - (MySQLdb). It made me search for more alternatives so I found pymysql, and it also got installed easily and worked in first go like a charm. So I would suggest using pymysql only instead of MySQLdb.

查看更多
【Aperson】
5楼-- · 2019-01-10 12:00

PyMySQL and MySQLdb are both database connectors for Python, libraries to enable Python programs to talk to a MySQL server.

You would normally never upload core Django files when deploying an app. If Django is working fine on your deployment server, you definitely don't need to change anything there. The DB driver is a step or two below the ORM even, and certainly none of the code you have written depends on which of these is in use.

查看更多
登录 后发表回答