我正在遵循本指南,以便可以为 python 3 安装 MySQL 连接器。
https://stackoverflow.com/questions/43501080/how-to-connect-to-mysql-db-from-python-3-on-ubuntu-16
不幸的是,当我输入:
(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/
你能给些建议么。
您需要 python 开发库来构建 python 模块:
应该为你处理好。
但是在 Ubuntu 中添加它的推荐方法是使用 Ubuntu 存储库。因此,要为 python3 安装 mysql 绑定,请运行
这将安装从 Python 3 连接到 mysql 所需的绑定,并确保它将与系统的其他包一起升级。建议这样做,除非您特别需要更新版本,或者该软件包在 Ubuntu 存储库中不可用。
MySQLdb 不适用于 Python 3。如果您可以从 MySQLdb 切换,那么我建议您使用MySQL 连接器 Python。因为它适用于 Python 2 和 3,并且它是MySQL 的官方 Oracle 驱动程序,用于使用 Python。
如果 pip 失败,您可以从https://dev.mysql.com/downloads/connector/python/下载安装程序
即使用 DEB 包安装 MySQL Connector参考本指南下载和安装
该模块遵循 Python 数据库 API 规范 v2.0 (PEP 249)。
然后使用
connect()
mysql连接器python的方法连接到MySQL。将所需的参数传递给connect()方法。即主机、用户名、密码和数据库名称