How to install Flask-mysqldb for Python?

14,138

Solution 1

You can not use mysqlclient‑1.4.5‑cp27‑cp27m‑win_amd64.whl as you found in other answer, because that is a wheel for Windows and you are on Linux.

The project mysqlclient, which is a dependency of flask-mysqldb, does not distribute wheels for Linux, so you have to build from source code directly (mysqlclient-1.4.5.tar.gz).

Your install likely fails due to missing build dependencies.

As the project landing page has mentioned, install the build dependencies first:

sudo apt-get install python-dev default-libmysqlclient-dev libssl-dev

Then try again to pip install --user flask-mysqldb.

Solution 2

I followed instructions from this answer mysql_config not found when installing mysqldb python interface

Just in case I ran

sudo apt-get install mysql-server

later

sudo apt-get install libmysqlclient-dev

and finally

pip3 install flask-mysqldb

and it worked, with Ubuntu 18.04

Share:
14,138
Rose
Author by

Rose

Updated on June 13, 2022

Comments

  • Rose
    Rose almost 2 years

    I am new to Python and Python-Flask and have ran into an error. I am using Ubuntu 18.04.3 LTS and Python 2.7.15+ to build a python-flask web application.

    When installing flask-mysqldb I run into an error.

    $ pip install flask-mysqldb
    

    The full output:

    DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
    Processing ./.cache/pip/wheels/fd/c9/ef/e0e99dfcf898bc040267e36c4a414ff0b22239968a25161012/Flask_MySQLdb-0.2.0-cp27-none-any.whl
    Collecting mysqlclient
      Using cached https://files.pythonhosted.org/packages/f8/9b/5db9a03e2088a87c26e3e4d4c7f7e8f4c2dbae610f9521cdfac15755a795/mysqlclient-1.4.5.tar.gz
    Requirement already satisfied: Flask>=0.10 in ./.local/lib/python2.7/site-packages (from flask-mysqldb) (1.1.1)
    Requirement already satisfied: click>=5.1 in ./.local/lib/python2.7/site-packages (from Flask>=0.10->flask-mysqldb) (7.0)
    Requirement already satisfied: Werkzeug>=0.15 in ./.local/lib/python2.7/site-packages (from Flask>=0.10->flask-mysqldb) (0.16.0)
    Requirement already satisfied: itsdangerous>=0.24 in ./.local/lib/python2.7/site-packages (from Flask>=0.10->flask-mysqldb) (1.1.0)
    Requirement already satisfied: Jinja2>=2.10.1 in ./.local/lib/python2.7/site-packages (from Flask>=0.10->flask-mysqldb) (2.10.3)
    Requirement already satisfied: MarkupSafe>=0.23 in ./.local/lib/python2.7/site-packages (from Jinja2>=2.10.1->Flask>=0.10->flask-mysqldb) (1.1.1)
    Building wheels for collected packages: mysqlclient
      Building wheel for mysqlclient (setup.py) ... error
      ERROR: Command errored out with exit status 1:
       command: /usr/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-Gno8ny/mysqlclient/setup.py'"'"'; __file__='"'"'/tmp/pip-install-Gno8ny/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/pip-wheel-OrgtfM --python-tag cp27
           cwd: /tmp/pip-install-Gno8ny/mysqlclient/
      Complete output (40 lines):
      running bdist_wheel
      running build
      running build_py
      creating build
      creating build/lib.linux-x86_64-2.7
      creating build/lib.linux-x86_64-2.7/MySQLdb
      copying MySQLdb/__init__.py -> build/lib.linux-x86_64-2.7/MySQLdb
      copying MySQLdb/_exceptions.py -> build/lib.linux-x86_64-2.7/MySQLdb
      copying MySQLdb/compat.py -> build/lib.linux-x86_64-2.7/MySQLdb
      copying MySQLdb/connections.py -> build/lib.linux-x86_64-2.7/MySQLdb
      copying MySQLdb/converters.py -> build/lib.linux-x86_64-2.7/MySQLdb
      copying MySQLdb/cursors.py -> build/lib.linux-x86_64-2.7/MySQLdb
      copying MySQLdb/release.py -> build/lib.linux-x86_64-2.7/MySQLdb
      copying MySQLdb/times.py -> build/lib.linux-x86_64-2.7/MySQLdb
      creating build/lib.linux-x86_64-2.7/MySQLdb/constants
      copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
      copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
      copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
      copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
      copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
      copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
      running build_ext
      building 'MySQLdb._mysql' extension
      creating build/temp.linux-x86_64-2.7
      creating build/temp.linux-x86_64-2.7/MySQLdb
      x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -Wdate-time -D_FORTIFY_SOURCE=2 -g -fdebug-prefix-map=/build/python2.7-PPrPZj/python2.7-2.7.15=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Dversion_info=(1,4,5,'final',0) -D__version__=1.4.5 -I/usr/include/mysql -I/usr/include/python2.7 -c MySQLdb/_mysql.c -o build/temp.linux-x86_64-2.7/MySQLdb/_mysql.o
      MySQLdb/_mysql.c: In function ‘_mysql_field_to_python’:
      MySQLdb/_mysql.c:1142:33: warning: passing argument 1 of ‘PyInt_FromString’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
               return PyInt_FromString(rowitem, NULL, 10);
                                       ^~~~~~~
      In file included from /usr/include/python2.7/Python.h:89:0,
                       from MySQLdb/_mysql.c:38:
      /usr/include/python2.7/intobject.h:37:24: note: expected ‘char *’ but argument is of type ‘const char *’
       PyAPI_FUNC(PyObject *) PyInt_FromString(char*, char**, int);
                              ^~~~~~~~~~~~~~~~
      x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -Wdate-time -D_FORTIFY_SOURCE=2 -g -fdebug-prefix-map=/build/python2.7-PPrPZj/python2.7-2.7.15=. -fstack-protector-strong -Wformat -Werror=format-security -Wl,-Bsymbolic-functions -Wl,-z,relro -Wdate-time -D_FORTIFY_SOURCE=2 -g -fdebug-prefix-map=/build/python2.7-PPrPZj/python2.7-2.7.15=. -fstack-protector-strong -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/MySQLdb/_mysql.o -L/usr/lib/x86_64-linux-gnu -lmysqlclient -lpthread -lz -lm -lrt -latomic -lssl -lcrypto -ldl -o build/lib.linux-x86_64-2.7/MySQLdb/_mysql.so
      /usr/bin/ld: cannot find -lssl
      /usr/bin/ld: cannot find -lcrypto
      collect2: error: ld returned 1 exit status
      error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
      ----------------------------------------
      ERROR: Failed building wheel for mysqlclient
      Running setup.py clean for mysqlclient
    Failed to build mysqlclient
    Installing collected packages: mysqlclient, flask-mysqldb
        Running setup.py install for mysqlclient ... error
        ERROR: Command errored out with exit status 1:
         command: /usr/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-Gno8ny/mysqlclient/setup.py'"'"'; __file__='"'"'/tmp/pip-install-Gno8ny/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-record-uh9IqO/install-record.txt --single-version-externally-managed --compile
             cwd: /tmp/pip-install-Gno8ny/mysqlclient/
        Complete output (40 lines):
        running install
        running build
        running build_py
        creating build
        creating build/lib.linux-x86_64-2.7
        creating build/lib.linux-x86_64-2.7/MySQLdb
        copying MySQLdb/__init__.py -> build/lib.linux-x86_64-2.7/MySQLdb
        copying MySQLdb/_exceptions.py -> build/lib.linux-x86_64-2.7/MySQLdb
        copying MySQLdb/compat.py -> build/lib.linux-x86_64-2.7/MySQLdb
        copying MySQLdb/connections.py -> build/lib.linux-x86_64-2.7/MySQLdb
        copying MySQLdb/converters.py -> build/lib.linux-x86_64-2.7/MySQLdb
        copying MySQLdb/cursors.py -> build/lib.linux-x86_64-2.7/MySQLdb
        copying MySQLdb/release.py -> build/lib.linux-x86_64-2.7/MySQLdb
        copying MySQLdb/times.py -> build/lib.linux-x86_64-2.7/MySQLdb
        creating build/lib.linux-x86_64-2.7/MySQLdb/constants
        copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
        copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
        copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
        copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
        copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
        copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
        running build_ext
        building 'MySQLdb._mysql' extension
        creating build/temp.linux-x86_64-2.7
        creating build/temp.linux-x86_64-2.7/MySQLdb
        x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -Wdate-time -D_FORTIFY_SOURCE=2 -g -fdebug-prefix-map=/build/python2.7-PPrPZj/python2.7-2.7.15=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Dversion_info=(1,4,5,'final',0) -D__version__=1.4.5 -I/usr/include/mysql -I/usr/include/python2.7 -c MySQLdb/_mysql.c -o build/temp.linux-x86_64-2.7/MySQLdb/_mysql.o
        MySQLdb/_mysql.c: In function ‘_mysql_field_to_python’:
        MySQLdb/_mysql.c:1142:33: warning: passing argument 1 of ‘PyInt_FromString’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
                 return PyInt_FromString(rowitem, NULL, 10);
                                         ^~~~~~~
        In file included from /usr/include/python2.7/Python.h:89:0,
                         from MySQLdb/_mysql.c:38:
        /usr/include/python2.7/intobject.h:37:24: note: expected ‘char *’ but argument is of type ‘const char *’
         PyAPI_FUNC(PyObject *) PyInt_FromString(char*, char**, int);
                                ^~~~~~~~~~~~~~~~
        x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -Wdate-time -D_FORTIFY_SOURCE=2 -g -fdebug-prefix-map=/build/python2.7-PPrPZj/python2.7-2.7.15=. -fstack-protector-strong -Wformat -Werror=format-security -Wl,-Bsymbolic-functions -Wl,-z,relro -Wdate-time -D_FORTIFY_SOURCE=2 -g -fdebug-prefix-map=/build/python2.7-PPrPZj/python2.7-2.7.15=. -fstack-protector-strong -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/MySQLdb/_mysql.o -L/usr/lib/x86_64-linux-gnu -lmysqlclient -lpthread -lz -lm -lrt -latomic -lssl -lcrypto -ldl -o build/lib.linux-x86_64-2.7/MySQLdb/_mysql.so
        /usr/bin/ld: cannot find -lssl
        /usr/bin/ld: cannot find -lcrypto
        collect2: error: ld returned 1 exit status
        error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
        ----------------------------------------
    ERROR: Command errored out with exit status 1: /usr/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-Gno8ny/mysqlclient/setup.py'"'"'; __file__='"'"'/tmp/pip-install-Gno8ny/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-record-uh9IqO/install-record.txt --single-version-externally-managed --compile Check the logs for full command output.`
    

    I have read several other questions and solutions (https://stackoverflow.com/a/51228348/12403455) which suggested using:

    mysqlclient‑1.4.5‑cp27‑cp27m‑win_amd64.whl

    So I installed this by using:

     $ pip install mysqlclient-1.4.5-cp27m-win_amd64.whl
    

    And ran into an error:

    Requirement 'mysqlclient‑1.4.5‑cp27‑cp27m‑win_amd64.whl' looks like a filename, but the file does not exist
    mysqlclient‑1.4.5‑cp27‑cp27m‑win_amd64.whl is not a valid wheel filename.
    

    I am unsure how to resolve this issue or how to proceed. Any help would be appreciated.