zoukankan      html  css  js  c++  java
  • macOS安装Python MySQLdb

    macOS安装Python MySQLdb

    0. 参考

    Mac OS X - EnvironmentError: mysql_config not found
    Error installing mysql-python: library not found for -lssl

    1. 背景

    import MySQLdb
    

    出错:

    ImportError: No module named MySQLdb
    

    1. 解决步骤

    1.1 安装mysql

    brew install mysql
    

    把以下export加到~/.bash_profile

    export PATH="$PATH:/usr/local/bin/mysql"
    

    1.2 安装pip

    sudo easy_install pip
    

    1.3 安装MySQL-Python

    sudo pip install MySQL-Python
    

    2. 番外

    如果依照上述方法,安装过程还出现如下错误,那可能是因为brew改了MySQL的版本导致的。

    _mysql.c:29:10: fatal error: 'my_config.h' file not found
    

    可以通过安装旧版本MySQL的方法处理,如下:

    brew uninstall mysql # 卸载新版本mysql
    brew install mysql@5.7 # 安装旧版本mysql
    echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.bash_profile
    source ~/.bash_profile
    sudo pip install MySQL-Python
    

    3. 番外macOS Catalina 10.15.3

    更新到最新版本之后,发现上述方法已经失效,网上说的安装mysql-connector-c也不可以,经过尝试之后,直接安装mysql和mysqlclient就可以了,而非之前的mysql-python。安装mysqlclient会有以下报错,需要加上编译选项:

        ld: library not found for -lssl
        clang: error: linker command failed with exit code 1 (use -v to see invocation)
        error: command 'cc' failed with exit status 1
    
    

    具体如下:

    brew install mysql
    LDFLAGS=-L/usr/local/opt/openssl/lib pip install mysqlclient
    

    4. 番外macOS big sur

    python3也是可以用MySQLdb的,没必要改用pymysql。
    但发现新版本3的方法执行又不行了,会报ld: library not found for -lzstd,但是zstd是已经安装的
    解决思路是看一下zstd的安装路径在哪里,pip install的时候指定一下。

    具体如下:

    brew install mysql
    brew list zstd(找到lib的路径)
    LDFLAGS=-L/opt/homebrew/Cellar/zstd/1.5.0/lib pip install mysqlclient
    
  • 相关阅读:
    Grunt中批量无损压缩图片插件--Grunt-contrib-imagemin
    移动端前端题
    前端面试题,不解释...
    HTMLFormElement获取表单里面所有的值然后以json形式返回
    关于 CommonJS AMD CMD UMD 规范的差异总结
    mysql库安装
    安装mysql到ubuntu
    一个致命的操作
    vim编辑器常规配置
    samba的安装与配置
  • 原文地址:https://www.cnblogs.com/fatshen/p/8990988.html
Copyright © 2011-2022 走看看