I have installed mysql in my virtual environment using this command and it has installed successfully..
sudo apt-get install libmysqlclient-dev
sudo pip install MySQL-python
Now in my settings.py I have done this..
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': os.path.join(BASE_DIR, 'db_hotelnepal'),
}
}
But when I run server it throws error saying ""Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb""...
Whats the prob and how can I fix ?
MySQL-python is not compatible with python 3, and it gives this error. You didn't mention the python version you are using, but I have only seen this when using python 3 and MySQL, so I will make that assumption. I use MySQL-for-Python-3 which can be found here: MySQL-for-Python-3. It is compatible with python 2.7-3.3, haven't had a project with MySQL and python 3.4 to try it yet.
I install by running pip install https://github.com/davispuh/MySQL-for-Python-3/archive/1.0.tar.gz
Related
I am working on the setup of my first unmanaged vps(ubuntu 20.4) and following the document to connect my django site to mysql server "https://www.digitalocean.com/community/tutorials/how-to-create-a-django-app-and-connect-it-to-a-database"
able to setup
python/django/virtualenv
mysql server - tested from mysql workbench on my local machine-Ok
apache2 working ok. able to see the welcome screen with sqlite3.
now moving to mysql. i tried both the way('ENGINE': 'django.db.backends.mysql' and option file '/etc/mysql/my.cnf' to connect to mysql server from django project.
None of the alternatives working. activated virtualenv, installed mysqldb using
a) sudo apt install libmysqlclient-dev default-libmysqlclient-dev
b) pip install wheel
c) pip install mysqlclient
while trying to migrate following error encountered. tried with all the option available on stakoverflow, i found none of them working.
Error:
"
(envFusion) worker#server:~/fusion/fusionerp$ python manage.py makemigrations
Traceback (most recent call last):
File "/home/worker/fusion/envFusion/lib/python3.8/site-packages/django/db/backends/mysql/base.py", line 15, in
import MySQLdb as Database
ModuleNotFoundError: No module named 'MySQLdb'
The above exception was the direct cause of the following exception:
.
.
File "/home/worker/fusion/envFusion/lib/python3.8/site-packages/django/db/backends/mysql/base.py", line 17, in
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient? "
with ubuntu 20.04 deafult python version is 3.8 and connecting to mysql with 3.8 is a bit challenge so you need to downgrade python to 3.6 or downgrade to ubuntu 18 if you are starting a fresh.
Found a tutorial to downgrade using alternatives and it worked. But had my other dependencies where i need python >= 3.7. So i had no other choice but refresh my ubuntu 20.04 on vps and configured with postgres. it is working way better than i expected with apache2/restframework/redis.
i suppose that i have a solution to this problem, so when we use a Ubuntu OS, i think that we need to run the next commands to work that package:
sudo apt-get install default-libmysqlclient-dev -y
sudo apt-get install gcc -y
pip3 install mysqlclient
Even if you already tried the first one, it might work. I have a Dockerfile in my GitHub of a Django app, if you want to test it later to see if it works on your machine, it took me a week to find the problem and deploy this app to Oracle Cloud because of this error.
Dockerfile
When connecting my Django project to a database hosted on a remote VM, when I run python3 manage.py run server I get the error
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?
when I try python3 -m pip install mysqlclient to resolve the dependency, I get the error
ERROR: Could not find a version that satisfies the requirement mysqlclient (from versions: 1.3.0, 1.3.1, 1.3.2, 1.3.3, 1.3.4, 1.3.5, 1.3.6, 1.3.7, 1.3.8, 1.3.9, 1.3.10, 1.3.11rc1, 1.3.11, 1.3.12, 1.3.13, 1.3.14, 1.4.0rc1, 1.4.0rc2, 1.4.0rc3, 1.4.0, 1.4.1, 1.4.2, 1.4.2.post1, 1.4.3, 1.4.4, 1.4.5, 1.4.6, 2.0.0, 2.0.1, 2.0.2, 2.0.3)
ERROR: No matching distribution found for mysqlclient
I'd like to understand what is causing these issues and how to resolve it.
Thanks in advance!
I got it to work! For anyone facing the same issues run brew install mysql then try python3 manage.py runserver
For what it's worth, you might try installing the mysql-connector-c first. It seems heavy-handed to have to install MySQL to use a driver.
From:
How to install Python MySQLdb module using pip?
Mac OS
brew install mysql-connector-c
if that fails, try
brew install mysql
Am Trying to connect to my mysql db from django app.
I get the below error during migration:
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb
module. Did you install mysqlclient?
I've already installed mysqlclient as below:
Requirement already satisfied: mysqlclient in
/usr/local/lib/python3.7/site-packages (1.4.2.post1)
I've also tried with pymysql and adding below code to ini.py file:
import pymysql
pymysql.install_as_MySQLdb()
Gives me some other errors.
What could be wrong?
Python 3.7 , mysql 5.7 and Django 2.2 are my setup versions.
I had the same issue. Running the below command fixed it for me.
pip install --force-reinstall --ignore-installed --no-binary :all: mysqlclient
I had the same issue. The thing that worked for me is the following:
https://stackoverflow.com/a/54521244/12497648,
except when I did brew install mysql-client
I got the message Warning: mysql-client 5.7.23_1 is already installed and up-to-date
To reinstall 5.7.23_1, run "brew reinstall mysql-client" so I ran brew reinstall mysql-client after which I continued with the instructions from the link above (export PATH... etc.)
(also don't forget to do the pip wheel mysqlclient / pip install mysqlclient)
If the error includes a Reason: image not found error, then it can be solved with symlinks like this:
Library not loaded: #rpath/libmysqlclient.21.dylib Reason: image not found Django migrate error using mysqlclient DB driver and MySQL 8 with macOS
Besides the above, I'm using virtualenv and virtualenvwrapper. MariaDB server 10.1.17 was installed via dnf install
I'm working through the first Django tutorial. My runserver command:
(djTut3)$ python manage.py runserver
ran OK, with a warning about migration.
In part 2, here: https://docs.djangoproject.com/en/1.10/intro/tutorial02/
I ran:
(djTut3)$ python manage.py migrate
as instructed, and got the following errors:
ImportError: No module named 'MySQLdb'
During handling of the above exception, another exception occurred:
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'
I did a lot of searching, and tried the below (among other things), mostly from SO:
===
$ sudo dnf install MySQL-python
<No error>
$ python3 manage.py migrate
<Same errors as before>
$ sudo dnf install mariadb-devel
Skipping packages with conflicts:
(add '--best --allowerasing' to command line to force their upgrade)
$ sudo dnf install mariadb-devel --best --allowerasing
<No error>
$ python3 manage.py migrate
<Same errors as before>
$ pip3 install mysql-python
ImportError: No module named 'ConfigParser'
$ pip3 install mysql-connector
<No error>
$ pip3 install mysql-client
_mysql.c:40:20: fatal error: Python.h: No such file or directory
#include "Python.h"
compilation terminated.
error: command 'gcc' failed with exit status 1
$sudo dnf install python-devel mysql-devel
<already installed>
===
Now I still can't get
(djTut3)$ python manage.py migrate
to work, and
(djTut3)$ python manage.py runserver
gives the same error as the migrate command.
My settings.py includes:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'djTut3',
'USER': os.environ['DB_USERNAME'],
'PASSWORD': os.environ['DB_PASSWORD'],
'HOST': '127.0.0.1',
'PORT': '',
}
}
Can anyone offer some help with this?
Thanks very much in advance for any response.
This is why you should install Python packages via pip, not your OS package manager. MySQLdb is not available for Python 3, as you can see from the error when you tried via pip3.
The reason you had issues installing mysql-client is presumably because you need the python-devel package, or whatever the Fedora equivalent is; that would be a dnf install.
However since you did succeed with mysql-connector, you can use that; you need to configure Django to use it as shown in that library's documentation, by replacing 'django.db.backends.mysql' with 'mysql.connector.django'.
I am new to Django and I try to follow the official tutorial. since I want to connect to mysql (installed on my computer, and i checked mysql module does exit in python command line), I set the ENGINE in setting.py to be django.db.backends.mysql . and then I tried to run
python manage.py syncdb
then I got error message like this:
Error loading MySQLdb module
and I cannot run
pip install mysql-python
the error msg is:
Unable to find vcvarsall.bat
so what is this error? and honestly I am not sure about the difference between mysql-python and mysql-connector-python. Since i tried with "pip install mysql-connector-python" and it tells me that requirement already satisfied...
You need to download the windows binary installer for the MySQL drivers for Python. Installing from source will not work since you do not have the development headers in Windows.
You need to install the mysql python connector
sudo apt-get install python-mysqldb