pip install MySQL python出错

2024-05-13 21:27:40 发布

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

我已经试了几天来解决这个问题,看看其他的问题,但我找不到一个问题与我得到的相同的错误。

我一直在尝试遵循本教程https://realpython.com/learn/start-django/并达到以下目的:

数据库设置 首先,安装MySQL python,它是python的数据库连接器:

$ pip install MySQL-python

到目前为止,一切正常,但当我尝试安装MySQL python时,出现以下错误:

(env) Camerons-MacBook-Pro:django15_project camrail$ pip install MySQL-python
Collecting MySQL-python
  Using cached MySQL-python-1.2.5.zip
    Complete output from command python setup.py egg_info:
    sh: mysql_config: command not found
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/private/var/folders/3v/czm4shrx0yg5jts39qktf6t00000gn/T/pip-build-2rNsQQ/MySQL-python/setup.py", line 17, in <module>
        metadata, options = get_config()
      File "/private/var/folders/3v/czm4shrx0yg5jts39qktf6t00000gn/T/pip-build-2rNsQQ/MySQL-python/setup_posix.py", line 43, in get_config
        libs = mysql_config("libs_r")
      File "/private/var/folders/3v/czm4shrx0yg5jts39qktf6t00000gn/T/pip-build-2rNsQQ/MySQL-python/setup_posix.py", line 25, in mysql_config
        raise EnvironmentError("%s not found" % (mysql_config.path,))
    EnvironmentError: mysql_config not found
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/3v/czm4shrx0yg5jts39qktf6t00000gn/T/pip-build-2rNsQQ/MySQL-python/
(env) Camerons-MacBook-Pro:django15_project camrail$ 

我发现这个答案https://opensourcehacker.com/2011/03/02/installing-mysql-python-connector-on-osx/说MySQL实用程序不可用,所以我按照他们的建议修复它,但没有效果。我还尝试安装开发人员工具xcode-select --install,正如一些人所建议的那样。我试过重新安装ez_setup

不太清楚从这里走到哪里,任何帮助都非常感谢。


Tags: installpipinpybuildconfigvarsetup
3条回答

在我看来,你的系统中缺少mysql_config,或者安装程序找不到它。请确保mysql_config确实已安装。

例如

  • 在Debian/Ubuntu上,您必须安装包: sudo apt get install libmysqlclient dev

  • 运行Mac OSX Mountain Lion,我只是在终端中运行这个来修复: 导出路径=$PATH:/usr/local/mysql/bin

这是我找到的最快的方法。

使用以下命令检查mysql配置文件:

mysql_config

现在您应该得到这样的输出:

mysql_config 
Usage: /usr/bin/mysql_config [OPTIONS]
Compiler: GNU 6.3.0
Options:
        --cflags         [-I/usr/include/mysql ]
        --cxxflags       [-I/usr/include/mysql ]
        --include        [-I/usr/include/mysql]
        --libs           [-L/usr/lib/x86_64-linux-gnu -lmysqlclient -lpthread -lz -lm -lrt -latomic -ldl]
        --libs_r         [-L/usr/lib/x86_64-linux-gnu -lmysqlclient -lpthread -lz -lm -lrt -latomic -ldl]
        --plugindir      [/usr/lib/mysql/plugin]
        --socket         [/var/run/mysqld/mysqld.sock]
        --port           [0]
        --version        [5.7.18]
        --libmysqld-libs [-L/usr/lib/x86_64-linux-gnu -lmysqld -lpthread -lz -lm -lrt -latomic -lcrypt -ldl -laio -llz4 -lnuma]
        --variable=VAR   VAR is one of:
                pkgincludedir [/usr/include/mysql]
                pkglibdir     [/usr/lib/x86_64-linux-gnu]
                plugindir     [/usr/lib/mysql/plugin]
Ideally it should contain libmysqld-libs option. If it is not there, as it was not there in my case, you can assume that your mysql is broken.

在这种情况下,可以将此配置直接写入配置文件

我在pip安装mysql包时也遇到了同样的错误,但是在安装libmysqlclient dev之后

  • sudo apt安装libmysqlclient dev
  • pip安装mysql

现在它对我起作用了。。

已验证:

import mysql

  • 未找到任何错误,表示安装成功并正常工作 适当地

相关问题 更多 >