无法在python3 virtualen中安装mysqlclient

2024-04-23 20:04:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用MySQL和Python 3运行django。我用virtualenv --no-site-packages -p python3 ./初始化了虚拟环境。然后我用pip安装了django和wheel,所以pip freeze给出了

django==1.8.3
wheel==0.24.0

然后我试着用pip install mysqlclient安装mysqlclient,但它以

Downloading/unpacking mysqlclient
Downloading mysqlclient-1.3.6.tar.gz (78kB): 78kB downloaded
Running setup.py (path:/tmp/pip-build-jpdlrnc8/mysqlclient/setup.py) egg_info for package mysqlclient

Installing collected packages: mysqlclient
Running setup.py install for mysqlclient
building '_mysql' extension
i586-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -fPIC -Dversion_info=(1,3,6,'final',1) -D__version__=1.3.6 -I/usr/include/mysql -I/usr/include/python3.4m -I/home/ondra/zelvovani/include/python3.4m -c _mysql.c -o build/temp.linux-i686-3.4/_mysql.o -DBIG_JOINS=1 -fno-strict-aliasing -DTAOCRYPT_DISABLE_X86ASM -g -DNDEBUG
error: command 'i586-linux-gnu-gcc' failed with exit status 1
Complete output from command /home/ondra/zelvovani/bin/python3 -c "import setuptools, tokenize;__file__='/tmp/pip-build-jpdlrnc8/mysqlclient/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-fa_6nkh3-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/ondra/zelvovani/include/site/python3.4:
running install

running build

running build_py

creating build

creating build/lib.linux-i686-3.4

copying _mysql_exceptions.py -> build/lib.linux-i686-3.4

creating build/lib.linux-i686-3.4/MySQLdb

copying MySQLdb/__init__.py -> build/lib.linux-i686-3.4/MySQLdb

copying MySQLdb/compat.py -> build/lib.linux-i686-3.4/MySQLdb

copying MySQLdb/converters.py -> build/lib.linux-i686-3.4/MySQLdb

copying MySQLdb/connections.py -> build/lib.linux-i686-3.4/MySQLdb

copying MySQLdb/cursors.py -> build/lib.linux-i686-3.4/MySQLdb

copying MySQLdb/release.py -> build/lib.linux-i686-3.4/MySQLdb

copying MySQLdb/times.py -> build/lib.linux-i686-3.4/MySQLdb

creating build/lib.linux-i686-3.4/MySQLdb/constants

copying MySQLdb/constants/__init__.py -> build/lib.linux-i686-3.4/MySQLdb/constants

copying MySQLdb/constants/CR.py -> build/lib.linux-i686-3.4/MySQLdb/constants

copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-i686-3.4/MySQLdb/constants

copying MySQLdb/constants/ER.py -> build/lib.linux-i686-3.4/MySQLdb/constants

copying MySQLdb/constants/FLAG.py -> build/lib.linux-i686-3.4/MySQLdb/constants

copying MySQLdb/constants/REFRESH.py -> build/lib.linux-i686-3.4/MySQLdb/constants

copying MySQLdb/constants/CLIENT.py -> build/lib.linux-i686-3.4/MySQLdb/constants

running build_ext

building '_mysql' extension

creating build/temp.linux-i686-3.4

i586-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -fPIC -Dversion_info=(1,3,6,'final',1) -D__version__=1.3.6 -I/usr/include/mysql -I/usr/include/python3.4m -I/home/ondra/zelvovani/include/python3.4m -c _mysql.c -o build/temp.linux-i686-3.4/_mysql.o -DBIG_JOINS=1 -fno-strict-aliasing -DTAOCRYPT_DISABLE_X86ASM -g -DNDEBUG

error: command 'i586-linux-gnu-gcc' failed with exit status 1

----------------------------------------
Cleaning up...
Command /home/ondra/zelvovani/bin/python3 -c "import setuptools, tokenize;__file__='/tmp/pip-build-jpdlrnc8/mysqlclient/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-fa_6nkh3-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/ondra/zelvovani/include/site/python3.4 failed with error code 1 in /tmp/pip-build-jpdlrnc8/mysqlclient
Storing debug log for failure in /home/ondra/.pip/pip.log

我已经安装了libmysqlclient-devpython3-devzlib1g-dev(stackoverflow中其他问题中建议的包)。

你知道我做错什么了吗?


Tags: installpippybuildhomeincludelinuxlib
3条回答

使用pymysql应该能解决你的问题。使用pip3 install pymysql。安装后,在文件目录project/project/__init__.py中写入

import pymysql 
pymysql.install_as_MySQLdb()

注:project是您的django项目的名称。

如果您使用的是python3.53.6,那么您应该遇到this issue中描述的问题。

引用此库的贡献者之一:

Building MySQL-python on Windows is hard. You should build from source instead of pip install since you need to edit setup.cfg file.

谢天谢地,Christoph Gohlke(加州大学欧文分校荧光动力学实验室)为Python扩展包提供了一个非官方的Windows二进制文件,您可以在那里下载这个库的wheels(.whl):

根据您的系统和安装的python版本下载。

然后你只需要做(在下载文件的目录中):

pip install mysqlclient-....whl

您可以尝试按the documentation所述安装python3-devlibmysqlclient-dev。我也有同样的问题,直到我发现我需要那些包裹。

相关问题 更多 >