What is PyMySQL and how does it differ from MySQLdb? Can it affect Django deployment?

54,413

Solution 1

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.

Solution 2

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

Solution 3

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.

Solution 4

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.

Share:
54,413

Related videos on Youtube

Hafiz
Author by

Hafiz

I have 8 years of experience Developing web based applications. Have experience in managing short teams as well as working in teams. Most of my technical experience is in but not limited to PHP, MySQL, Python and JavaScript(mostly client side but also have used node.js) but I am language agnostic and always open to learn more. I have worked on both frontend and backend in different projects with different responsibilities. I have created teams as well as managed teams. Book: I have written "Building RESTful Webservices in PHP7" with Packt pub. https://www.packtpub.com/application-development/building-restful-web-services-php-7 Skillset: I am listing my skillset here so that you can look at it if required: Backend: Mostly used PHP and Python on server side. PHP Frameworks Used: Laravel, Lumen, Kohana, Code Igniter Python Frameworks used: Django, Scrapy Good understanding and experience in creating and consuming REST API Used Codeception for REST API Testing Storage/DB: Used MySQL in most of projects while PgSQL and elasticsearch both are used in one project only. Basic understanding of MongoDB and have developed a basic todo app in Laravel with MongoDB. Also supervised a team which used Neo4j, so have high level knowledge of that as well. LAMP Server Configuration: Can and have configured LAMP server on Amazon EC2 as well as DigitalOcean. Also configured mod_wsgi for Django with Apache2. Frontend: Good at HTML5 and CSS Basic knowledge of Phonegap, bootsrap, HTML5 Canvas, SVG, Photoshop and Web Sockets Datavisualization using D3 that renders SVG. version control system: SVN, Git Mobile: Basic understanding and working knowledge of Android SDK including some of its UI components, SQLite and AsyncTask as I used them in my BS project. Operating Systems: Ubuntu and Windows Other than above skill-set I am good learner and always ready to learn anything required to accomplish work. Other interests: I am always interested in improving things.Other than what I have currently done and know. I am also interested in working in Node JS, functional paradigm, WebGL, noSQL and search engines. Detail: I have done many projects which are different in nature than each other. Some of them are listed in my resume with my experience and other detail. However, please note that my bigger plus is not my skill-set but my quick learning abilities.

Updated on July 09, 2022

Comments

  • Hafiz
    Hafiz almost 2 years

    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?

    • skjoshi
      skjoshi over 9 years
      pymysql is pure python port of mysqldb (mysql-python) package. So, pymysql can be installed on any system without needing a C compiler. Installing mysqldb may need a compiler and in windows can produce error(error: Unable to find vcvarsall.bat) if you do not have one.
    • Mark Amery
      Mark Amery over 8 years
      This question would've been more useful if it had been less broad. You're asking several things: the difference between the two libraries, how to deploy PyMySQL to some kind of shared hosting environment (which you haven't told us anything about, and which we therefore can't possibly help you with), and, more broadly, what issues one needs to be aware of when using PyMySQL with Django. Those are three distinct questions and would've been better asked as such.
    • Hafiz
      Hafiz over 8 years
      Question was about whether should I use PyMySQL or not and I had two concerns that you are calling multiple questions.
  • Hafiz
    Hafiz over 12 years
    yah but my point is that while I have changed MySQLdb to pymysql in those two files told above that are of django, so will it create problem? Or django is already there on webhostings ? Or pymysql is there?
  • Daniel Eriksson
    Daniel Eriksson over 11 years
    I should add that the official version of PyMySQL is not (currently) compatible with Django 1.4 using the method above. There is a fork that seems to work at github.com/CMGS/PyMySQL if you want to try it.
  • Alveoli
    Alveoli almost 10 years
    I would add one more reason: to support Python 3
  • chishaku
    chishaku over 9 years
    What does "greened" mean?
  • Andrew Gorcester
    Andrew Gorcester over 9 years
    @chisaku it can be "made compatible with green thread-based concurrency", for instance gevent, eventlet, etc. For green threads to work, the network bits of the driver need to be modified to release control of the thread while waiting for i/o.
  • João dos Reis
    João dos Reis over 8 years
    Why the try/except? Surely you want an obvious error if you've forgotten to install pymysql?
  • ChrisGuest
    ChrisGuest over 7 years
    I believe the idea is to load PyMySQL to mimic MySQLdb if it is present, but fall back to MySQLdb otherwise.
  • gbtimmon
    gbtimmon over 5 years
    +1 reason to use PyMySQL it is MIT license which is better if you intend to distribute commercial code then MySQLdbs GPL license.