解决python2项目MySQLDB依赖问题

507 阅读1分钟



因为新项目要引用MySQLdb,而且是python2,之前自己写机器学习的项目都是python3,搞了一个早上,卡在import MySQLdb上了

运行

pip2 install mysqlclient

告诉我

dflags=''
      File "<string>", line 1, in <module>
      File "/private/var/folders/l3/hc2gmq15269bw3j7258p2tgc0000gn/T/pip-install-9KeT9n/mysqlclient/setup.py", line 16, in <module>
        metadata, options = get_config()
      File "setup_posix.py", line 51, in get_config
        libs = mysql_config("libs")
      File "setup_posix.py", line 29, 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/l3/hc2gmq15269bw3j7258p2tgc0000gn/T/pip-install-9KeT9n/mysqlclient/

试了好多办法

终于在

stackoverflow.com/questions/5…

找到了完美解决方案



I just resolved this exact issue when running Ansible's mysql_user module. The answer here helped tip me off to a solution. I also needed MySQL-python from pip, which also broke in this process, so I've added the extra steps.

Steps to resolve:

  1. brew unlink mysql # only if installed, causes the next step to fail
  2. brew install mysql-connector-c
  3. locate mysql_config file with which (mysql_config)
  4. edit the mysql_config file, under # Create options change this:

    libs="$libs -l "

    to this:

    libs="$libs -lmysqlclient -lssl -lcrypto"

    if using vim, :wq! to save the read-only file

  5. Now the install should run successfully

    pip install mysqlclient

  6. Adding this separately, as it's similar but not directly related to the initial question

    pip install MySQL-python

  7. Fix mysql brew formula, if it was unlinked in the first step.

    brew unlink mysql-connector-c

    brew link mysql

感觉有点坑啊