12👍
MySQLdb is only for Python 2.x. You can’t install in Python 3.x versions. Now from your question i can see that you are working with Django. In this case you have three alternatives, from Django mysql notes:
- mysqldb
- mysqlclient
- mysql-connect-python
This gives to you two alternatives, mysqlclient and mysql-connect-python, The first one requires compilation from extensions for the plugin and, in Windows, this implies VStudio Libraries and a know-how for compile native extensions.
mysql-connect-python is not compiled (and i don’t recommend this for production, maybe only for dev) so you are going to need to install this.
You can try:
pip3 install mysql-connect-python
or
pip3 install http://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-python-2.0.4.zip
if the first one fails.
45👍
MySQLdb is the interface to MySQL database. As mentioned by other posts, MySQLdb doesn’t support Python 3.x. I used PyMySQL as the replacement. You need to install it first:
pip install PyMySQL
The next step is to replace ‘MySQLdb’ with ‘pymysql’ in all the codes, which is intimidating. Luckily, PyMySQL can be loaded as MySQLdb dyanamically. In order to achieve it in Django, you need to add the following lines to __init__.py file under the dir of the project’s default app (If your have a project named ‘myproject’, add lines to myproject/myproject/init.py):
import pymysql
pymysql.install_as_MySQLdb()
This __init__.py would be executed when you run the Django project, and MySQLdb will be replaced. You problem is then solved.
- Django Admin Drop down selections
- Multiple lookup_fields for django rest framework
- Allow a task execution if it's not already scheduled using celery
- How to paginate "Change List" page in Django admin?
- Django form field grouping
14👍
You can use mysqlclient instead of MySQLdb. MySqLdb is not compatible with Python 3.
pip install mysqlclient
2👍
You can also try installing mysqlclient-python directly from source:
-
Download source by git clone or zipfile (URL – https://github.com/PyMySQL/mysqlclient-python.git).
-
Customize site.cfg (you can give the path for mysql_config)
-
python setup.py install
- Django i18n: how to not translate the admin site?
- Retrieving the 'many' end of a Generic Foreign Key relationship in Django
- Django REST Framework Swagger 2.0
- Reverse Queryset Order in Django
1👍
This one resolved my issue in Python 3.x
pip3 install PyMySQL
Instead of : SQLALCHEMY_DATABASE_URI = "'mysql://.....'"
Use : SQLALCHEMY_DATABASE_URI = "'mysql+pymysql://.....'"
- Django – inlineformset_factory with more than one ForeignKey
- "TemplateSyntaxError Invalid block tag: 'trans'" error in Django Templates
- Can I create model in Django without automatic ID?
- Django.core.exceptions.ImproperlyConfigured
- Indirect inline in Django admin
- SQL injection hacks and django
0👍
MySQLdb is not available for Python 3.x
I have CentOS server
This worked for me
python3 -m pip install –user https://github.com/davispuh/MySQL-for-Python-3/archive/1.0.tar.gz
Python 3.5.0 (default, Dec 12 2017, 17:00:35)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import MySQLdb
- Pagination and get parameters
- How can I automatically let syncdb add a column (no full migration needed)
- Add extra field to ModelForm
- Python/Django development, windows or linux?