how to configure django with mysql using pymysql (python 3.5) - mysql

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

Related

ModuleNotFoundError: No module named '_version' when importing mysql

import mysql.connector
conn = mysql.connector.connect(host="127.0.0.1", user="root", passwd="root", database="Master")
print(conn)
conn.close()
Python needs a MySQL driver to access the MySQL database.
install mysql-connector :
python -m pip install mysql-connector
Start by creating a connection to the database
Use the username and password from your MySQL database:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
passwd="yourpassword"
)
print(mydb)
I had the myaql connector in place but git the following error:
mysql/connector/__init__.py", line 34, in <module>
import _version
ModuleNotFoundError: No module named '_version'
Neither pipreqs --force nor python -m pip install mysql-connector-python could resolve the problem. However, the solution by Arpit Jain solve the issue.

MySQL (MariaDB) backend for Django under Python3 on Debian

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.

MySQL gem only accessable from ruby command line, not apache localhost

Heads up, ruby n00b testing the waters -
OS - OSX 10.7
test.rb in /Library/WebServer/CGI-Executables:
#!/usr/bin/ruby
require 'mysql'
con = Mysql.new('localhost', 'root', 'root', 'rubytest')
rs = con.query('select * from people')
rs.each_hash { |h| puts h['name']}
con.close
When running from the command line ruby test.rb, I get the fields within just fine. When hitting test.rb from localhost (http://localhost/cgi-bin/test.rb), I get a 500 and a nag in the error log about mysql being missing-
Apache error log:
[Fri Aug 31 23:47:22 2012] [error] [client 127.0.0.1] /Library/WebServer/CGI-Executables/test.rb:3:in `require': no such file to load -- mysql (LoadError)
[Fri Aug 31 23:47:22 2012] [error] [client 127.0.0.1] \tfrom /Library/WebServer/CGI-Executables/test.rb:3
[Fri Aug 31 23:47:22 2012] [error] [client 127.0.0.1] Premature end of script headers: test.rb
My export paths in ~/.bashrc:
export DYLD_LIBRARY_PATH="/usr/local/mysql/lib"
export PATH="$PATH:/usr/local/mysql/bin"
export PS1="$ "
If this is any help, running gem env | grep 'RUBY EXECUTABLE'
RUBY EXECUTABLE: /Users/robertcox/.rvm/rubies/ruby-1.9.2-p318/bin/ruby
Any help appreciated
Maybe you need update gem install mysql2

Rerunning the Django Project

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/

Problem running syncdb in django

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.