Vraag Hoe verbinding te maken met MySQL db van python 3 op 16.04


Ik volg deze handleiding zodat ik de MySQL-connector voor python 3 kan installeren.

https://stackoverflow.com/questions/43501080/how-to-connect-to-mysql-db-from-python-3-on-ubuntu-16

Helaas toen ik typte:

(env) pip install mysqlclient

Collecting mysqlclient
  Downloading mysqlclient-1.3.12.tar.gz (89kB)
    100% |████████████████████████████████| 92kB 1.4MB/s 
Building wheels for collected packages: mysqlclient
  Running setup.py bdist_wheel for mysqlclient ... error
  Complete output from command /home/hugh/SAT/env/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-sbgaxask/mysqlclient/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /tmp/tmpqgsp3qwwpip-wheel- --python-tag cp35:
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.linux-x86_64-3.5
  copying _mysql_exceptions.py -> build/lib.linux-x86_64-3.5
  creating build/lib.linux-x86_64-3.5/MySQLdb
  copying MySQLdb/__init__.py -> build/lib.linux-x86_64-3.5/MySQLdb
  copying MySQLdb/compat.py -> build/lib.linux-x86_64-3.5/MySQLdb
  copying MySQLdb/connections.py -> build/lib.linux-x86_64-3.5/MySQLdb
  copying MySQLdb/converters.py -> build/lib.linux-x86_64-3.5/MySQLdb
  copying MySQLdb/cursors.py -> build/lib.linux-x86_64-3.5/MySQLdb
  copying MySQLdb/release.py -> build/lib.linux-x86_64-3.5/MySQLdb
  copying MySQLdb/times.py -> build/lib.linux-x86_64-3.5/MySQLdb
  creating build/lib.linux-x86_64-3.5/MySQLdb/constants
  copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-3.5/MySQLdb/constants
  copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-3.5/MySQLdb/constants
  copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-3.5/MySQLdb/constants
  copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-3.5/MySQLdb/constants
  copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-3.5/MySQLdb/constants
  copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-3.5/MySQLdb/constants
  copying MySQLdb/constants/REFRESH.py -> build/lib.linux-x86_64-3.5/MySQLdb/constants
  running build_ext
  building '_mysql' extension
  creating build/temp.linux-x86_64-3.5
  x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -Dversion_info=(1,3,12,'final',0) -D__version__=1.3.12 -I/usr/include/mysql -I/usr/include/python3.5m -I/home/hugh/SAT/env/include/python3.5m -c _mysql.c -o build/temp.linux-x86_64-3.5/_mysql.o
  _mysql.c:32:20: fatal error: Python.h: No such file or directory
  compilation terminated.
  error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

  ----------------------------------------
  Failed building wheel for mysqlclient
  Running setup.py clean for mysqlclient
Failed to build mysqlclient
Installing collected packages: mysqlclient
  Running setup.py install for mysqlclient ... error
    Complete output from command /home/hugh/SAT/env/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-sbgaxask/mysqlclient/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-d4xggqn_-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/hugh/SAT/env/include/site/python3.5/mysqlclient:
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-3.5
    copying _mysql_exceptions.py -> build/lib.linux-x86_64-3.5
    creating build/lib.linux-x86_64-3.5/MySQLdb
    copying MySQLdb/__init__.py -> build/lib.linux-x86_64-3.5/MySQLdb
    copying MySQLdb/compat.py -> build/lib.linux-x86_64-3.5/MySQLdb
    copying MySQLdb/connections.py -> build/lib.linux-x86_64-3.5/MySQLdb
    copying MySQLdb/converters.py -> build/lib.linux-x86_64-3.5/MySQLdb
    copying MySQLdb/cursors.py -> build/lib.linux-x86_64-3.5/MySQLdb
    copying MySQLdb/release.py -> build/lib.linux-x86_64-3.5/MySQLdb
    copying MySQLdb/times.py -> build/lib.linux-x86_64-3.5/MySQLdb
    creating build/lib.linux-x86_64-3.5/MySQLdb/constants
    copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-3.5/MySQLdb/constants
    copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-3.5/MySQLdb/constants
    copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-3.5/MySQLdb/constants
    copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-3.5/MySQLdb/constants
    copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-3.5/MySQLdb/constants
    copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-3.5/MySQLdb/constants
    copying MySQLdb/constants/REFRESH.py -> build/lib.linux-x86_64-3.5/MySQLdb/constants
    running build_ext
    building '_mysql' extension
    creating build/temp.linux-x86_64-3.5
    x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -Dversion_info=(1,3,12,'final',0) -D__version__=1.3.12 -I/usr/include/mysql -I/usr/include/python3.5m -I/home/hugh/SAT/env/include/python3.5m -c _mysql.c -o build/temp.linux-x86_64-3.5/_mysql.o
    _mysql.c:32:20: fatal error: Python.h: No such file or directory
    compilation terminated.
    error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

    ----------------------------------------
Command "/home/tom/SAT/env/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-sbgaxask/mysqlclient/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-d4xggqn_-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/hugh/SAT/env/include/site/python3.5/mysqlclient" failed with error code 1 in /tmp/pip-build-sbgaxask/mysqlclient/

Kunt u alstublieft adviseren.


1
2017-12-27 16:25


oorsprong




antwoorden:


Je hebt python-ontwikkelbibliotheken nodig om python-modules te bouwen:

sudo apt install python3-dev libpython3-dev

zou dat voor je moeten regelen.

Maar de aanbevolen manier om dit toe te voegen in Ubuntu zou zijn om de Ubuntu-repositories te gebruiken. Dus, om mysql bindingen voor python3 te installeren, voer uit

sudo apt install python3-mysqldb

Dit zal de bindingen installeren die nodig zijn om verbinding te maken met mysql van Python 3, en zorgt ervoor dat het geüpgraded wordt samen met andere pakketten van je systeem. Dit wordt aanbevolen, tenzij u specifiek een nieuwere versie nodig heeft, of het pakket niet beschikbaar is in de Ubuntu-repositories.


3
2017-12-27 16:32





MySQLdb werkt niet met Python 3. Als het mogelijk is dat je overschakelt van MySQLdb, raad ik je aan om te gebruiken MySQL-connector Python. Omdat het werkt met zowel Python 2 als 3 en dat is het ook officiële Oracle-stuurprogramma van MySQL voor het werken met Python.

pip install mysql-connector-python

als pip mislukt, kun je het installatieprogramma downloaden van https://dev.mysql.com/downloads/connector/python/

d.w.z. het installeren van MySQL Connector met behulp van DEB-pakket Raadpleeg deze handleiding voor downloaden en installeren 

Deze module voldoet aan Python Database API Specification v2.0 (PEP 249).

Gebruik dan connect() methode van mysql-connectorpython om verbinding te maken met MySQL.pass het vereiste argument om de methode connect () te verbinden. d.w.z. Host, gebruikersnaam, wachtwoord en databasenaam


1
2017-08-04 09:22