如何为 Python 安装 Flask-mysqldb? [英] How to install Flask-mysqldb for Python?

查看:76
本文介绍了如何为 Python 安装 Flask-mysqldb?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 和 Python-Flask 的新手,遇到了错误.我正在使用 Ubuntu 18.04.3 LTS 和 Python 2.7.15+ 来构建一个 python-flask Web 应用程序.

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.

安装flask-mysqldb时遇到错误.

When installing flask-mysqldb I run into an error.

$ pip install flask-mysqldb

完整输出:

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

我已阅读其他几个问题和解决方案(https://stackoverflow.com/a/51228348/12403455)建议使用:

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

然后遇到一个错误:

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.

推荐答案

您不能像在其他答案中找到的那样使用 mysqlclient‑1.4.5‑cp27‑cp27m‑win_amd64.whl,因为那是 Windows 的轮子,而您使用的是 Linux.

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.

项目mysqlclient,它是flask-mysqldb,不为Linux分发轮子,所以你必须直接从源代码构建(mysqlclient-1.4.5.tar.gz).

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

然后再次尝试pip install --user flask-mysqldb.

这篇关于如何为 Python 安装 Flask-mysqldb?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆