. Advertisement .
..3..
. Advertisement .
..4..
No module named mysql is a frequent error that there are many chances you may bump into when running a .py script in the command line.
So, what is its arising root and how to solve such an irritating wrongdoing?
Read on for no more puzzling problems not yet resolved!
Why Does Your Code Happen To Have The “No Module Named Mysql” Error?
This issue likely comes into existence when you forgot to deploy the mysql-connector-python module in advance of installing or importing it in the wrong environment. As a result, the Python compiler fails to locate a package named mysql in the libraries, leading to the appearance of the below error.
>>> import mysql.connector
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named ‘mysql’
The other reasons that can trigger this problem are also you setting up the package in another Python version instead of the one you’re currently using, or the software being installed worldwide rather than in your virtual environment.
Aside from that, you should be careful to avoid the situations as follows: Creating a module called “mysql.py” to replace the official module, your IDE using the wrong Python, or declaring “mysql” as a shadow variable for the imported value.
How To Fix The “No Module Named Mysql” Error?
Solution 1: Install mysql-connector-python module
The first and foremost way to solve such an error is merely to institute the model using the ‘pip install mysql-connector-python’ command.
After the installation is complete, try running the script again using the python command. We bet you will have racked up something this time!
For example:
import mysql.connector
# Connect to server
cnx = mysql.connector.connect(
host="127.0.0.1",
port=3306,
user="alice",
password="dogsandcats123")
# Get a cursor
cur = cnx.cursor()
# Execute a query
cur.execute("SELECT CURDATE()")
# Fetch one result
row = cur.fetchone()
print("Current date is: {0}".format(row[0]))
# Close connection
cnx.close()
Solution 2: Check whether you’ve installed ‘mysql-connector-python’ the module in your virtual environment rather than the global one.
What if I cannot find anywhere else the virtual environment needed? Then creating one on your own must be the only answer that can satisfy your demand.
Running the code:
# use correct version of Python when creating VENV
python3 -m venv venv
# activate on Unix or MacOS
source venv/bin/activate
# activate on Windows (cmd.exe)
venv\Scripts\activate.bat
# activate on Windows (PowerShell)
venv\Scripts\Activate.ps1
# install mysql-connector-python in virtual environment
pip install mysql-connector-python
The Bottom Line
Above are ones of the quick tickets you can seek help from when encountering the error “no module named mysql”. Hopefully, this guidance, as well as others regarding fixing Python bugs (such as DLL load failed while importing and so on) can be of great use to use somehow. Good luck to you then and also, don’t forget to stay tuned for our next updates!
Leave a comment