Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL package for python 3.7

I am in the need of saving data into a MySQL database, my problem is I can't find the package...

Solution explored:

  • Oracle website: https://dev.mysql.com/downloads/connector/python/

  • Trying to install MySQLdb from Pycharm it said non existing.

So if anyone has an alternative to it for python 3.7, or knows how to get the connector for version 3.7 I would be happy.

like image 760
NotanNimda Avatar asked Jul 22 '18 18:07

NotanNimda


2 Answers

There are two ways to install MySQL connector. The second way is preferred.

1. MySQL Installer

This is Oracle's product installer. The problem is that it is outdated. It is only aware of Python version 3.6, and nothing newer. To install for a newer version of Python, use option 2.

2. Python package manager, pip

The Python package manager comes with Python, called pip. It downloads the package from the PyPI repository and installs it in an automatic location based on what version of Python (or what virtual copy) you use to install it. The package that you want is mysql-connector-python. In fact, the official documentation says this is the recommended method for installing the MySQL Connector.

For example, on Windows, open up the Command Prompt (cmd.exe) by searching in the Start Menu. Navigate to the directory where pip is installed. Or make sure that pip's directory is included in the $PATH variable (which you can edit by searching for "PATH" in the Start Menu).

The command prompt will show you this:

PS C:\Users\Ryan> 

except instead of my username, it'll show your username, or the path you navigated to after you found pip, like "C:\blah\blah". Then use this command...

Input:

pip install mysql-connector-python

It downloads and installs it.

Results:

PS C:\Users\Ryan> pip install mysql-connector-python
Collecting mysql-connector-python
Downloading https://files.pythonhosted.org/packages/2d/65/3fc902c0f7635912800c6b935313b99b9d4426419ef7ba04f76231b24923/mysql_connector_python-8.0.12-py2.py3-none-any.whl (300kB)
    100% |████████████████████████████████| 307kB 1.1MB/s
Collecting protobuf>=3.0.0 (from mysql-connector-python)
Downloading https://files.pythonhosted.org/packages/77/78/a7f1ce761e2c738e209857175cd4f90a8562d1bde32868a8cd5290d58926/protobuf-3.6.1-py2.py3-none-any.whl (390kB)
    100% |████████████████████████████████| 399kB 1.8MB/s
Requirement already satisfied: setuptools in c:\users\ryan\appdata\local\programs\python\python37\lib\site-packages (from protobuf>=3.0.0->mysql-connector-python) (40.4.3)
Collecting six>=1.9 (from protobuf>=3.0.0->mysql-connector-python)
Using cached https://files.pythonhosted.org/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a/six-1.11.0-py2.py3-none-any.whl
Installing collected packages: six, protobuf, mysql-connector-python
Successfully installed mysql-connector-python-8.0.12 protobuf-3.6.1 six-1.11.0

Afterward, you can make sure it is installed by displaying all packages that have been installed by you (not including standard libraries, which come with Python):

Input:

PS C:\Users\Ryan> pip list

Results:

Package                Version
---------------------- -------
mysql-connector-python 8.0.12
pip                    18.0
protobuf               3.6.1
setuptools             40.4.3
six                    1.11.0

To check if you installed it to the right version of Python, use pip -V. To make sure you installed it to the right copy (virtual or original), look at the file path where the package was installed:

PS C:\Users\Ryan> pip -V

Results:

pip 18.0 from c:\users\ryan\appdata\local\programs\python\python37\lib\site-packages\pip (python 3.7)

For example, in my system, it shows python37 in the folder path, so it installed it to the original Python 3.7.0 installation I have, instead of an older version or virtual environment (conda or virtualenv, etc).

To check the version using the Python executable, instead of pip:

PS C:\Users\Ryan> py -V
Python 3.7.0

If you need to install it to an older or newer Python version than the default installation, insert the version number as an option (aka. "switch") in the command, using -. For example, to select version 3.6:

py -3.6 -m pip install mysql-connector-python

The - works on both Windows and Unix-like OSs.

like image 149
Ryan Avatar answered Sep 28 '22 04:09

Ryan


mysqlclient supports python3.7 officially, you can find it here :

https://pypi.python.org/pypi/mysqlclient

1)you can download , PyMySQL 0.9.2
2)extract & copy the folder pymysql into the python Lib folder
3)and for connection you can do like this(make a file for example freeman.py):

#!/usr/bin/env python

import pymysql

conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='', db='freemanDB')

cur = conn.cursor()
cur.execute("SELECT * FROM users")

print(cur.description)
print()

for row in cur:
    print(row)

cur.close()
conn.close()
like image 41
Freeman Avatar answered Sep 28 '22 03:09

Freeman