am new to python and django
I am trying to build a small python django application on my local windows laptop.
i am not able to underlying tables required for my Django project as when i run "python manage.py syncdb" i get the below error
` Error :django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'. Did you install mysqlclient or MySQL-python? '
And when i try running "pip install mysqlclient" i get the below error
'error: Microsoft Visual C++ 10.0 is required. Get it with "Microsoft Windows SDK 7.1": www.microsoft.com/download/details.aspx?id=8279'
I am stuck in this step and not able to find any leads. can someone suggest any workaround
By default Django is configured to use Sqlite database. See settings.py file:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
You have it configured to use MySQL database:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DATABASE_NAME',
'USER': 'DB_USERNAME',
'PASSWORD': 'DB_PASSWORD',
'HOST': '127.0.0.1',
'PORT': '3306'
}
}
If it's a small test project, I would recommend you to switch back to Sqlite, but if you intend to run your project later in production using MySQL, then better use MySQL in development process.
Install:
MySQL or MariaDB locally on your computer
Windows SDK from www.microsoft.com/download/details.aspx?id=8279
run pip install mysqlclient - now it should succeed
Related
I want to set the mysql database using mysql-connector-python for setting up a web site made with Django using a database backend but I get this error:
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'
My credentials (name, user, password, host, port) works fine, so the problem isn't here.
DATABASES = {
'default': {
'ENGINE': 'mysql.connector.django',
'NAME': 'library',
'USER': 'username',
'PASSWORD': 'password',
'HOST': '127.0.0.1',
'PORT': '3306',
'OPTIONS': {
'autocommit': True,
'use_pure': True,
},
}
}
I'm using python 3.8.5 and mysql-connector-python 8.0.21 and I understand that mysql-connector-python, starting from version 8.0.13, has a bug, which makes it impossible to use with Django and to avoid the bug I added ‘use_pure’: True in database options. But it doesn't work.
I found this question but it doesn't say anything about using ‘use_pure’: True.
If the mysql-connector-python isn't compatible with python 3, what can I use instead? (I can't wait for the support release).
you do not have the option to use mysql connector, you have to install mysql-client from https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient and try installing all the wheels incase you get errors, while installing using pip install
I am currently building a web project in Django and working on getting the site ready for deployment. I initially deployed the site on Heroku using Sqlite3, with my database code in settings as follows:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
However, due to Heroku's ephemeral file system I realized I needed to switch to Postgres. After following a few different guides I arrived at the following changes to my settings. I first deleted the "DATABASES" as mentioned above and replaced it with the following:
import dj_database_url, psycopg2
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': *************,
'USER': **************,
'PASSWORD': ************************************,
'HOST': *********************,
'PORT': '5432',
}
}
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)
When I make these changes everything works perfectly on the local Django development server (127.0.0.1:8000), but once I push the changes through Git and to Heroku I try opening my site on Heroku and get "Application Error" and a suggestion to check my logs. Which report "ModuleNotFoundError: No module named dj_database_url" and similarly any outside library I try to upload to Heroku appears to have this issue. How can I fix this issue and move my site into production? Any help would be greatly appreciated.
You need to include them in your requirements.txt file or however the buildpack you're using expects the applications dependencies to be indicated.
I have this installed for mysql and django , still I receive an error
-Django 2.1.3
-mysql-connector-python 8.0.13
-pip 18.1
-pytz 2018.7
-setuptools 39.0.1
-virtualenv 16.0.0
-virtualenvwrapper-win 1.2.5
-wheel 0.32.3
How can I connect to mysql and django with mysql-connector. I still receive an error to install mysqlclient. Is there anything missing. please let me know
If you do not have MySQL installed. follow up here:
But first, check if these tutorials apply for your current OS version! The installation process can be different
example:
Ubuntu 14, 16 or 18.
Mac osx 10.12 Sierra, 10.13 High Sierra or 10.14 mojave
Windows:
https://chocolatey.org/packages?q=mysql
Ubuntu:
https://www.digitalocean.com/community/tutorials/how-to-use-mysql-or-mariadb-with-your-django-application-on-ubuntu-14-04
Mac osx:
https://gist.github.com/operatino/392614486ce4421063b9dece4dfe6c21
With MySQL up and running, log into the interactive session for MySQL
mysql -u root -p
create your database user
CREATE DATABASE myproject CHARACTER SET UTF8;
CREATE USER myprojectuser#localhost IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON myproject.* TO myprojectuser#localhost;
FLUSH PRIVILEGES;
now in myproject/myproject/settings.py apply these changes:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'myproject',
'USER': 'myprojectuser',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '',
}
}
Update your DATABASES setting to use the connector backend.
DATABASES = {
'default': {
'NAME': 'user_data',
'ENGINE': 'mysql.connector.django',
...
},
...
}
import MySQLDB as Database
File "c:\pythonprojects\env\lib\site-packages\mysql_python-1.2.2-py3.6-win32.egg\mysqldb__init__py", line 22
raise ImportError, "This is MySQL Version %s, But _mysql is version %r"
SyntaxError: invalid syntax
Make sure you have pymysql installed. Then in settings.py do this
try:
import pymysql
pymysql.install_as_MySQLdb()
except:
pass
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db-name',
'USER': 'username',
'PASSWORD': 'password',
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '3306',
}
}
i) First you have to install the mysql server in your machine, if you already have mysql server up and running then jump to next step.
ii) Create a user in mysql (you can use the root user, but I would suggest create a separate one) you can refer mysql documentation for creating a new user. Let, you have created a user with:- username = testuser , password = testpass#123
iii) Create a database in mysql , let named it testDB
iv) Now you have to install mysqlclient-python, to install it from PyPI you can do so by typing pip install mysqlclient (by default pip the python package manager points python2 series if you are setting up your django project on python3 then you have to use pip3) for more information on how to install mysqlclient-python on your particular OS you can refer this link: https://github.com/PyMySQL/mysqlclient-python
v) Now In settings.py under key DATABASES make these changes:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # for mysql engine
'NAME': 'testDB',
'USER': 'testuser',
'PASSWORD': 'testpass#123',
'HOST': 'localhost', # if you want to run your project on your localmachine
}
}
vi) Now change your working directory to your django project directory: " cd yourDjangoProject" and run these django commands
python manage.py makemigrations
python manage.py migrate
Now I hope that above django commands will run successfully and you would be able to use to your mysql database in your django project.
i am very new to python Django. i need to connect the MySQL database with python, i really don't have any idea about that ..
i am using python 1.7.
tell me how to connect MySQL database with python. what settings i need to fill in settings.py.
i have goggled it but i am not getting a proper answer please tell me from the beaning how to achieve it.
i have run this command now what are the next steps ?
sudo apt-get install python-mysqldb
i really don't have any idea about the further steps, please guide me
Well you just installed MySQL Database interface for python
Make sure to run pip freeze and see MySQL-python==1.2.5 is there or not .)
And include below snippet in your settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'yourdatabasename',
'USER': 'root',
'PASSWORD': 'root',
'HOST': 'localhost',
'PORT': '',
}
}