Please Help. How Can I import mysql-connector into Django Project

Hi I am following a Django tutorial by John Elder…Django Project - Code a CRM App Tutorial. At timestamp 0:09:39 John Elder says import mysql connector. On my machine there is a yellow squiggly warning line under the line “import mysql-connector”. I have installed both mysql-connector and mysql-connector-python. Still I get the yellow warning line. What should I do to get my code to start working?
Question 2: Instead of using mysql for the tutorial as John Elder suggests would it be okay to use the default SQLite3 database instead of MySQL? He had mentioned earlier that it does not matter which database I use because Django abstracts the database.

Question 1: Review that portion of the tutorial carefully. Make sure you do all the steps exactly as described, and pay attention to what is showing on the screen. (This is generally good advice when trying to follow any video tutorials.)
I can tell you that this:

is invalid Python syntax. Either that’s not what is in the tutorial, or it’s a bad tutorial.

Question 2: Maybe? It depend upon whether or not he uses anything that would end up being MySQL-specific.

Thank you. Let me check the tutorial again

I think I have followed the tutorial as best as I can. I have downloaded and installed MySQL on my computer. I have pip installed mysql-connector and for good measure installed mysql-connector-python. While installing MySQL this what I get in the terminal

(venv) C:\Users\hp\Documents\Learn Python\Django\John Elder\dcrm>pip install mysql
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: mysql in c:\users\hp\appdata\local\packages\pythonsoftwarefoundation.python.3.12_qbz5n2kfra8p0\localcache\local-packages\python312\site-packages (0.0.3)
Requirement already satisfied: mysqlclient in c:\users\hp\appdata\local\packages\pythonsoftwarefoundation.python.3.12_qbz5n2kfra8p0\localcache\local-packages\python312\site-packages (from mysql) (2.2.4)

Images are not allowed but I have checked again and I think at timestamp 0:9:23 John Elder gives the command “import mysql-connector”. Just before that I am asked to create a file mydb.py which I did from the terminal with

type null>mydb.py
``
then I hit enter. In my file tree in VSCode I see the file mydb.py. But there is a warning yellow squiggly line under the line "import mysql.connector"

In the video he clearly types (and says out loud):

import mysql.connector

I am thinking maybe I have not pip installed the necessary dependencies/requirements. So I just did a pip list and this is what I get. Just to confirm what is installed.

Django                 5.0.6
isort                  5.13.2
mccabe                 0.7.0
mysql                  0.0.3
mysql-connector        2.2.9
mysql-connector-python 9.0.0
mysqlclient            2.2.4
pip                    24.2
platformdirs           4.2.2
pylint                 3.2.6
pylint-django          2.5.5
pylint-plugin-utils    0.8.2
sqlparse               0.5.0
tomlkit                0.13.0
tzdata                 2024.1

This is only a warning by your editor, it’s not necessarily accurate. It’s only something to worry about if your code fails to run properly.

Okay. So I continue with the tutorial. I run

python manage.py migrate

and I get a whole bunch of errors.
I must admit that 1) I created the ‘elderco’ database using my terminal. 2)When I run the command

python mydb.py

I get the following error in my terminal

mysql.connector.errors.DatabaseError: 1007 (HY000): Can't create database 'elderco'; database exists 

So I go to my settings.py file copy the database config and name the new database as ‘new_elderco’. I also comment out the lines of code that refer to the ‘elderco’ database.
When I now run

python mydb.py

it works and creates the ‘new_elderco’ database which I confirm by running in my terminal

show databases;
``
When I now run

python manage.py migrate

I get a whole bunch of error lines the last line of which reads:

django.db.utils.OperationalError: (1045, “Access denied for user ‘root’@‘localhost’ (using password: NO)”)

What can I do to fix this?

What is your DATABASES setting from settings.py?

# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases

DATABASES = {
    # 'default': {
    #     'ENGINE': 'django.db.backends.mysql',
    #     # 'ENGINE': 'django.db.backends.sqlite3',
    #     # 'NAME': BASE_DIR / 'db.sqlite3',
    #     'NAME': 'elderco',
    #     'USER': 'root',
    #     'password': '$zA$345_678$%',
    #     'HOST': 'localhost',
    #     'PORT': '3306',
    # },

    'default': {
        'ENGINE': 'django.db.backends.mysql',
        # 'ENGINE': 'django.db.backends.sqlite3',
        # 'NAME': BASE_DIR / 'db.sqlite3',
        'NAME': 'new_elderco',
        'USER': 'root',
        'password': '$zA$345_678$%',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}

The error message is about a password being missing.

When you look at your DATABASES settings, what is different about the password setting, compared to the others?

The fear of being ridiculed for not knowing or appearing ignorant made me skip math classes when I was young. Now I am 73 years old and that fear came up again when I saw your question about the difference in the password settings. That is why I have not replied since yesterday. Honestly I have checked the password settings and do not seem to find any difference. I guess it is alright not to know something. So I will ask for further explanation. Thank you for your patience. Some of us are slow learners. I am one of the slow learners. Could you please explain more about the password settings…thnx

I think that should be capitalized like this: 'PASSWORD': '$zA$345_678$%',

1 Like

@anefta , @phigyford, @KenWhitesell I really appreciate your patience and your help. Thank you so much. anefta I was looking at the lowercase ''password" and I did not or should I say was not aware of any difference. Thank you. I changed everything to uppercase and my code now works. Wow. There is so much to learn. Si I guess I will just continue plodding along and learning bit by bit. Thanks ever so much

1 Like

Keep going and ask for help freely! :slight_smile:

Sorry to make you feel ignorant @ZacAmata, that wasn’t my intention.

Sometimes I feel it’s good to nudge someone towards spotting an error for themselves, rather than simply telling them the answer. If they can’t see it, no problem, but it was worth a try, and I’ll explain.

But maybe that isn’t helpful after all. Good luck, and keep asking questions!

No no no. Its not you who makes me feel ignorant. Not at all. In fact I like it when you nudge me as you say. The fact is that I was looking at the code and my mind just did not get… whats the word… I was looking at the lowercase letters but was not seeing it. Thanks for helping me. Indeed you just came at the right time. I am working on a tutorial by Real Python. Build a Blog from scratch and I am seeing TemplateDoesNotExist. I am going to close this post and create another topic with the specifics. I would appreciate your help. Thanks Phil

1 Like