简体   繁体   中英

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 ./ . Then I installed django and wheel using pip, so pip freeze gives

django==1.8.3
wheel==0.24.0

Then I tried installing mysqlclient with pip install mysqlclient , but it ended with

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).

Do you have any ideas, what am I doing wrong?

Use pymysql and it should solve your problem.Use pip3 install pymysql . After installing , in file directory project/project/__init__.py write this

import pymysql 
pymysql.install_as_MySQLdb()

Note:- project is the name of your django project.

You can try installing python3-dev and libmysqlclient-dev as the documentation says. 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 .

Quoting one of the contributors to this library:

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

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:

Download according to your system and python version installed.

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. First, uninstall mysqlclient and pymysql globally (if installed):

pip3 uninstall mysqlclient
pip3 uninstall pymysql

Now, go to you project and remove venv

sudo rm -r venv

If using pycharm, it will popup for setting up an interpreter. Select python3.6 as your interpreter and create a virtual environment. If not using pycharm,

python3 -m virtualenv venv
source venv/bin/activate

Now, install 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.

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

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)

  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. Use your package manager to install them system-wide and don't forget to mention specific python version you are using for your project. 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 by

    apt-get install python3.7-dev

This is my first answer over stackoverflow. I hope it will help someone.

Note the following steps:

  • Microsoft Visual C++ 14.0 is required. Download it in HERE

  • Downloads mysqlclient in HERE

  • Here's a list of compatible mysqlclient and 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‑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‑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‑cp36‑cp36m‑win_amd64.whl --> python 3.6 (64 bit) mysqlclient‑1.4.6‑cp36‑cp36m‑win32.whl --> python 3.6 (32 bit)

  • Downloads mysql based on your python version

  • Install 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:

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

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

Now simply install the MySQL client using this command:

$ pip install mysqlclient

If you're installing the same at the system level then add sudo in the beginning of the command.

Note: Whenever you create a python virtual environment, always update the pip and setuptools before proceeding with the requirements installation:

$ pip install pip setuptools -U

If You are using Linux:

As mentioned in the pypi guide of installing mysqlclient,

You may need to install the Python 3 and MySQL development headers and libraries like so

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

Note: ( Replace python3 with your python version ) eg

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

I had a problem installing mysqlclient python 3.9 32 bit. I solved it thanks to the advice from the official documentation. Install Section for windows

After installed MariaDB Connector/C 32 bit version.

pip install mysqlclient

Ran without errors

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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