How to Install MySQL in Python 3



Python


In this article, we show how to install MySQL for Python 3.

So, Python 2 has widespread support for Python, being that it's been out longer. Python 3 has less support, being that it's newer, especially the newer-released versions of Python 3.

The easiest way to probably install and use MySQL in Python is to open up your command prompt. Once the command prompt is opened, then specify the full path to the Scripts folder of the Python package. Once you have this full path specified to the Scripts folder, then type in, pip install pymysql.

This will look like the image shown below.

Install pymysql in Python




Once you have seen that pymysql has been successfully installed in the command prompt, then you can be assured that you have MySQL. Even though pymysql is not MySQL, it can be converted to MySQL. This will be shown below.

So, the following code will convert pymysql to MySQL.



So, again, once the pymysql module has been installed, you can import pymysql.

The line, pymysql.install_as_MySQLdb(), installs pymysql as a MySQL database.

You can then import MySQLdb and use it as if you had directly installed the MySQL database.

This is most likely the simplest way you can work directly with MySQL databases in Python.

If you are using Python 2, there is a lot of support. You can just directly install the MySQLdb and you won't have to do anything else other than import MySQLdb. However, with Python 3, especially the newer versions, there is much less support, and installing pymysql and then using pymysql to install the MySQLdb is much easier.

So, this is the simplest way to install MySQL in Python 3. There are other ways but this is one of the few ways that will work for all versions of Python 3.


Related Resources

How to Connect to a MySQL Database in Python



HTML Comment Box is loading comments...