I am trying to go through the django docs tutorial and having a problem syncing mysql. On the command python manage.py syncdb I get the following error (note I'm running in windows 7):
...
File "C:\python27\lib\site-packages\django\db\backends\mysql\base.py", line 14, in <module>
raise Improperlyconfigured("Error loading Mysqldb module: %s" % e)
django.core.excepions.Improperlyconfigured: Error loading Mysqldb module: No module named mySQLdb
I have initialized the db in setting.py as:
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mysite', #the name I gave in Mysql with 'CREATE DATABASE mysite;'
'USER': 'root',
'PASSWORD': 'mypassword', # as set in MysqlInstanceConfig
'HOST': '',
'PORT': '',
So how do I get syncdb to run correctly? What does the missing module error mean and how do I correct it?
You need to install the python mysql library.
Here is a django-related guide to do this on windows
You have to install the mySQLdb module.
You most probably haven't installed the Python module that handles the mysql connections. You can install it through the Cheeseshop with pip or easy_install and it's called MySQL-python.
Related
Some trouble after Big Sur update
I use Django 2.1.15 and Python 3.8
according to posts and documentation I installed the connector
brew install mysql-connector-c
and
pip install mysql-python
after I install mysql
brew install mysql
and the client
pip install mysqlclient
try to run django and i get this ERROR:
django.db.utils.OperationalError: (2013, "Lost connection to MySQL server at 'reading initial communication packet', system error: 54")
This is my setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'BATABASE',
'USER': 'sa',
'PASSWORD': 'password',
'HOST': '192.168.1.10',
'PORT': '1433',
}
}
You can try this https://github.com/imagineai/create-django-app
npm install -g imagine && imagine create -f django -n myapp
It will auto-detect your Python version and install appropriate dependencies. You can also select the database you want in the im file.
I am attempting to configure Django running under Python3 on a Debian box to use MariaDB as its backend. If I alter my mysite/settpings.py as per the tutorial i.e.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'foo',
'USER': 'tim',
'PASSWORD': 'swordfish',
'HOST': 'localhost'
}
}
I get a lot of grief, culminating in
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/mysql/base.py", line 30, in <module>
'Did you install mysqlclient or MySQL-python?' % e
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'.
Did you install mysqlclient or MySQL-python?
Now, obviously, MySQLdb doesn't work on Python3 and although I have installed the mysql connector package doesn't seem to work.
If I try using the connector recommended by MySQL
i.e.
DATABASES = {
'default': {
'ENGINE': 'mysql.connector.django',
'NAME': 'foo',
'USER': 'tim',
'PASSWORD': 'swordfish',
'HOST': 'localhost'
then I get a load of grief culminating in
django.core.exceptions.ImproperlyConfigured: 'mysql.connector.django' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
'mysql', 'oracle', 'postgresql', 'sqlite3'
Error was: cannot import name 'BaseDatabaseFeatures'
Any suggestions?
UPDATE
Further to Matt Seymour's suggestion if I attempt a pip (actually pip3 as my system Python is still 2.x) install of mysqlclient I get the following...
tim#merlin:~/mysite$ sudo pip3 install mysqlclient
Collecting mysqlclient
Using cached mysqlclient-1.3.10.tar.gz
Complete output from command python setup.py egg_info:
/bin/sh: 1: mysql_config: not found
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-build-9uub69zo/mysqlclient/setup.py", line 17, in <module>
metadata, options = get_config()
File "/tmp/pip-build-9uub69zo/mysqlclient/setup_posix.py", line 44, in get_config
libs = mysql_config("libs_r")
File "/tmp/pip-build-9uub69zo/mysqlclient/setup_posix.py", line 26, in mysql_config
raise EnvironmentError("%s not found" % (mysql_config.path,))
OSError: mysql_config not found
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-9uub69zo/mysqlclient
Could the fact that I am using MariaDB rather than MySQL be significant here?
OK, after some messing around I have an answer of sorts, although not as satisfactory as I would like.
The MySQLdb library isn't supported under Python 3.x, and so you should use the mysqlclient fork of it instead. However, because I am using the MariaDB fork of MySQL, some of Debian's dependencies required to install mysqlclient are unmet. I don't want to revert to MySQL as I am using MariaDB for other purposes.
So, as all I needed was a fairly robust, multi-user back-end RDBMS, I've installed PostgreSQL for use with Django which seems to work nicely enough.
having much trouble getting mysql(5.6) to connect with django (1.11) using python 3.5.2 in ubuntu(16.04) venv. im trying to use pymysql because it works great with flask applications, just not django yet. mysqlclient failed install and has other dependencies (ive read django docs yes) so would prefer avoiding that module. this really should work since it works flawless in flask and others seem to have it working. heres what Ive tried and found:
# first steps
pip install pymysql
mysql -u root -p
CREATE DATABASE djang0 CHARACTER SET utf8 COLLATE utf8_bin;
# manage.py
try:
import pymysql
pymysql.install_as_MySQLdb()
except ImportError:
pass
# settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'djang0',
'USER': 'root',
'PASSWORD': 'mypasswd',
'HOST': '',
'PORT': '3306',
}
}
# current models.py Post class for blog
class Post(models.Model):
title = models.CharField(max_length=140)
body = models.TextField()
date = models.DateTimeField()
image = models.ImageField(upload_to='uploads', blank=True)
# error - full here https://dpaste.de/vtEH
[Thu Apr 27 18:58:00.010964 2017] [wsgi:error] [pid 22811:tid 3049028416] [remote 192.168.0.3:52267] django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'.
[Thu Apr 27 18:58:00.010967 2017] [wsgi:error] [pid 22811:tid 3049028416] [remote 192.168.0.3:52267] Did you install mysqlclient or MySQL-python?
had to use dependency, pip install mysqlclient. boo
I try to connect MySQL from django application serverd in AWS but it raises an interesting error: Error was: No module named mysql.base
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'try',
'USER': 'root',
'PASSWORD': 'pwd',
'HOST': '',
'PORT': '3306',
}
}
MySQLdb is already installed. import MySQLdb runs in python command line.
I can connect MySQL. I have installed it by sudo apt-get install mysql-server mysql-common mysql-client libmysqlclient15-dev
I am really stuck with this error. Could you help to figure this out?
Thanks
It seems to me that you haven't installed mysql for python.
http://pypi.python.org/pypi/MySQL-python/1.2.3
or
sudo apt-get install python-mysqldb
Seems like a problem with your Python PATH. Make sure the python-mysql db driver is in the PATH.
Intitally when i setup i didn't have any error when i typed python manage.py runserver. However when i installed mysql and changed admins and databases in my settings.py, i can't seem to run the server again.
Setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': '', # Or path to database file if using sqlite3.
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
When i run python manage.py run server:
Traceback (most recent call last):
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
YOu have to make sure to active your env as you noticed. As you can see the django package ONLY exists in your env. If you are not using it then you can not access any part of the django package (django.core.management) THere are many man tutorials explaining how virtualenv functions.
http://www.arthurkoziel.com/2008/10/22/working-virtualenv/
http://iamzed.com/2009/05/07/a-primer-on-virtualenv/