简体   繁体   English

无法在 python3 virtualenv 中安装 mysqlclient

[英]Unable to install mysqlclient in python3 virtualenv

I want to run django with MySQL and Python 3. I initialized virtual environment with virtualenv --no-site-packages -p python3 ./ .我想用 MySQL 和 Python 3 运行 django。我用virtualenv --no-site-packages -p python3 ./初始化了虚拟环境。 Then I installed django and wheel using pip, so pip freeze gives然后我使用 pip 安装了 django 和 wheel,所以 pip freeze 给出了

django==1.8.3
wheel==0.24.0

Then I tried installing mysqlclient with pip install mysqlclient , but it ended with然后我尝试使用pip install 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

I have installed libmysqlclient-dev , python3-dev , zlib1g-dev (packages that were suggested in other questions here in stackoverflow).我已经安装了libmysqlclient-devpython3-devzlib1g-dev (在stackoverflow的其他问题中建议的包)。

Do you have any ideas, what am I doing wrong?你有什么想法,我做错了什么?

Use pymysql and it should solve your problem.Use pip3 install pymysql .使用 pymysql 它应该可以解决您的问题。使用pip3 install pymysql After installing , in file directory project/project/__init__.py write this安装后,在文件目录project/project/__init__.py写这个

import pymysql 
pymysql.install_as_MySQLdb()

Note:- project is the name of your django project.注意:- project 是 django 项目的名称。

You can try installing python3-dev and libmysqlclient-dev as the documentation says.您可以按照文档中的说明尝试安装python3-devlibmysqlclient-dev I had the same problem until I noticed about I needed those packages.我遇到了同样的问题,直到我注意到我需要这些包。

If you're using python 3.5 or 3.6 you should be having the problem described in this issue .如果您使用的是 python 3.53.6您应该会遇到此问题中描述的问题

Quoting one of the contributors to this library:引用该库的一位贡献者的话:

Building MySQL-python on Windows is hard.在 Windows 上构建 MySQL-python 很难。 You should build from source instead of pip install since you need to edit setup.cfg file.您应该从源代码而不是 pip install 构建,因为您需要编辑 setup.cfg 文件。

Thankfully there is an Unofficial Windows Binaries for Python Extension Packages by Christoph Gohlke (Laboratory for Fluorescence Dynamics, University of California, Irvine) and there you can download wheels ( .whl ) for this library:值得庆幸的是,Christoph Gohlke(加利福尼亚大学欧文分校荧光动力学实验室)为 Python 扩展包提供了一个非官方的 Windows 二进制文件,您可以在那里下载该库的轮子 ( .whl ):

Download according to your system and python version installed.根据你的系统和安装的python版本下载。

Then you just need to do (in the directory of the downloaded file):然后你只需要做(在下载文件的目录中):

pip install mysqlclient-....whl

This generally happens if you have multiple versions of python installed in your system.如果您的系统中安装了多个版本的 python,通常会发生这种情况。 First, uninstall mysqlclient and pymysql globally (if installed):首先,全局卸载mysqlclient和pymysql(如果已安装):

pip3 uninstall mysqlclient
pip3 uninstall pymysql

Now, go to you project and remove venv现在,转到您的项目并删除 venv

sudo rm -r venv

If using pycharm, it will popup for setting up an interpreter.如果使用 pycharm,它会弹出设置解释器。 Select python3.6 as your interpreter and create a virtual environment.选择 python3.6 作为解释器并创建一个虚拟环境。 If not using pycharm,如果不使用pycharm,

python3 -m virtualenv venv
source venv/bin/activate

Now, install mysqlclient,现在,安装 mysqlclient,

pip3 install mysqlclient

This should work!这应该有效!

Download the pre-compiled whl file for mysqlclient according to your OS from Christophe Gohlke's Library of pre-compiled python packages for windows from the below location.根据您的操作系统,从 Christophe Gohlke 的 Windows 预编译 python 包库从以下位置下载 mysqlclient 的预编译 whl 文件。

http://www.lfd.uci.edu/~gohlke/pythonlibs/#vlfd http://www.lfd.uci.edu/~gohlke/pythonlibs/#vlfd

Now got to directory where you downloaded that file and open command prompt there.现在到达下载该文件的目录并在那里打开命令提示符。

Now run below command现在运行下面的命令

pip install xxx.whl pip 安装 xxx.whl

Best Luck !!好运 !!

The problem occurs whilst compiling binaries and there are two possible solutions编译二进制文件时出现问题,有两种可能的解决方案

  1. Use pre-compiled binaries from third part websites like lfd (only for windows)使用来自第三方网站(如lfd )的预编译二进制文件(仅适用于 Windows)

  2. Install the header files through python dev package which are necessary for compilation as if you want to build extensions, you need to install the corresponding dev package.通过python dev包安装编译所必需的头文件,就像你要构建扩展一样,你需要安装相应的dev包。 Use your package manager to install them system-wide and don't forget to mention specific python version you are using for your project.使用您的包管理器在系统范围内安装它们,不要忘记提及您用于项目的特定 python 版本。 As of Ubuntu , python 3.6 is default version but my virtual environment was set for python3.7 and i had to specifically install development package byUbuntu 开始,python 3.6 是默认版本,但我的虚拟环境设置为 python3.7,我必须专门安装开发包

    apt-get install python3.7-dev

This is my first answer over stackoverflow.这是我对 stackoverflow 的第一个回答。 I hope it will help someone.我希望它会帮助某人。

Note the following steps:请注意以下步骤:

  • Microsoft Visual C++ 14.0 is required.需要 Microsoft Visual C++ 14.0。 Download it in HERE此处下载

  • Downloads mysqlclient in HERE此处下载 mysqlclient

  • Here's a list of compatible mysqlclient and python:这是兼容的 mysqlclient 和 python 列表:

    mysqlclient‑1.4.6‑cp39‑cp39‑win_amd64.whl --> python 3.9 (64 bit) mysqlclient‑1.4.6‑cp39‑cp39‑win32.whl --> python 3.9 (32 bit) mysqlclient‑1.4.6‑cp39‑cp39‑win_amd64.whl --> python 3.9(64 位) mysqlclient‑1.4.6‑cp39‑cp39‑win32.whl --> python 3.9(32 位)

    mysqlclient‑1.4.6‑cp38‑cp38‑win_amd64.whl --> python 3.8 (64 bit) mysqlclient‑1.4.6‑cp38‑cp38‑win32.whl --> python 3.8 (32 bit) mysqlclient‑1.4.6‑cp38‑cp38‑win_amd64.whl --> python 3.8 (64 位) mysqlclient‑1.4.6‑cp38‑cp38‑win32.whl --> python 3.8 (32 位)

    mysqlclient‑1.4.6‑cp37‑cp37m‑win_amd64.whl --> python 3.7 (64 bit) mysqlclient‑1.4.6‑cp37‑cp37m‑win32.whl --> python 3.7 (32 bit) mysqlclient‑1.4.6‑cp37‑cp37m‑win_amd64.whl --> python 3.7 (64 位) mysqlclient‑1.4.6‑cp37‑cp37m‑win32.whl --> python 3.7 (32 位)

    mysqlclient‑1.4.6‑cp36‑cp36m‑win_amd64.whl --> python 3.6 (64 bit) mysqlclient‑1.4.6‑cp36‑cp36m‑win32.whl --> python 3.6 (32 bit) mysqlclient‑1.4.6‑cp36‑cp36m‑win_amd64.whl --> python 3.6 (64 位) mysqlclient‑1.4.6‑cp36‑cp36m‑win32.whl --> python 3.6 (32 位)

  • Downloads mysql based on your python version根据您的python版本下载mysql

  • Install mysql --> pip3 install mysqlclient‑1.4.6.xxxxx.whl安装mysql --> pip3 install mysqlclient‑1.4.6.xxxxx.whl

  • Done完毕

You can easily install the mysqlclient in python3.* virtual environment or at system level just follow these steps:您可以轻松地在 python3.* 虚拟环境或系统级别安装mysqlclient ,只需按照以下步骤操作:

First, install:首先,安装:

$ sudo apt-get install gcc libssl-dev

$ sudo apt-get install python3.6-dev libmysqlclient-dev

Don't forget to update the python version as per your need, and for python2.7 use不要忘记根据您的需要更新 python 版本,对于 python2.7 使用

$ sudo apt-get install python-dev libmysqlclient-dev

Now simply install the MySQL client using this command:现在只需使用以下命令安装 MySQL 客户端:

$ pip install mysqlclient

If you're installing the same at the system level then add sudo in the beginning of the command.如果您在系统级别安装相同的,则在命令的开头添加sudo

Note: Whenever you create a python virtual environment, always update the pip and setuptools before proceeding with the requirements installation:注意:无论何时创建 python 虚拟环境,在继续进行需求安装之前,请务必更新 pip 和 setuptools:

$ pip install pip setuptools -U

If You are using Linux:如果您使用的是 Linux:

As mentioned in the pypi guide of installing mysqlclient,如安装mysqlclient的pypi指南中所述,

You may need to install the Python 3 and MySQL development headers and libraries like so您可能需要像这样安装 Python 3 和 MySQL 开发头文件和库

sudo apt-get install python3-dev default-libmysqlclient-dev build-essential

Note: ( Replace python3 with your python version ) eg注意:(用你的python版本替换python3)例如

sudo apt-get install python3.8-dev default-libmysqlclient-dev build-essential

I had a problem installing mysqlclient python 3.9 32 bit.我在安装 mysqlclient python 3.9 32 位时遇到问题。 I solved it thanks to the advice from the official documentation.由于官方文档的建议,我解决了它。 Install Section for windows Windows安装部分

After installed MariaDB Connector/C 32 bit version.安装 MariaDB Connector/C 32 位版本后。

pip install mysqlclient

Ran without errors运行没有错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM