简体   繁体   English

导入Python的MySQL包时出错

[英]Error importing MySQL package for Python

I'm trying to import a MySQL module with python, more specifically Flask, though I receive an error. 我正在尝试使用python导入MySQL模块,更具体地说是Flask,尽管我收到了一个错误。 I'm using a virtual environment with my application. 我正在使用我的应用程序的虚拟环境。 Here is the error: 这是错误:

    Traceback (most recent call last):
  File "../myapp/application.py", line 9, in <module>
    from flask.ext.mysql import MySQL
  File "/Users/pavsidhu/Documents/Web-Development/app/env/lib/python2.7/site-packages/flask/exthook.py", line 81, in load_module
    reraise(exc_type, exc_value, tb.tb_next)
  File "/Users/pavsidhu/Documents/Web-Development/app/env/lib/python2.7/site-packages/flaskext/mysql.py", line 3, in <module>
    import MySQLdb
  File "/Users/pavsidhu/Documents/Web-Development/app/env/lib/python2.7/site-packages/MySQLdb/__init__.py", line 19, in <module>
    import _mysql
ImportError: dlopen(/Users/pavsidhu/Documents/Web-Development/app/env/lib/python2.7/site-packages/_mysql.so, 2): Library not loaded: /Library/Python/2.7/site-packages/_mysql.so
  Referenced from: /Users/pavsidhu/Documents/Web-Development/app/env/lib/python2.7/site-packages/_mysql.so
  Reason: image not found

I can see in the error it says Library not loaded: /Library/Python/2.7/site-packages/_mysql.so . 我可以在错误中看到它Library not loaded: /Library/Python/2.7/site-packages/_mysql.so As I'm using a virtual environment that path is incorrect. 因为我正在使用路径不正确的虚拟环境。 It should be /lib/python2.7/site-packages/_mysql.so . 它应该是/lib/python2.7/site-packages/_mysql.so

Is there a way to change this? 有没有办法改变这个? Thanks. 谢谢。

EDIT: 编辑:

I found there was a terminal command on OSX to change the library location: 我发现OSX上有一个终端命令来更改库位置:

 sudo install_name_tool -change libmysqlclient.18.dylib /lib/python2.7/site-packages/MySQLdb/

though after hitting enter I get this: 虽然在击中后我得到了这个:

Usage: /Library/Developer/CommandLineTools/usr/bin/install_name_tool [-change old new] ... [-rpath old new] ... [-add_rpath new] ... [-delete_rpath old] ... [-id name] input

I don't appear to be entering the command wrong, what is the issue? 我似乎没有输入命令错误,问题是什么?

As a rule, try avoiding system python install like a plague. 作为一项规则,尝试避免像瘟疫一样的系统python安装。 Yes, even basing your virtualenvs on it. 是的,甚至基于你的virtualenvs。 It will too often generate hard to understand problems. 它往往会产生难以理解的问题。

I recommend getting Homebrew , then installing python and mysql for the headers: 我建议获取Homebrew ,然后为头文件安装python和mysql:

brew install python
brew install mysql

And then basing your virtualenv on python from brew: 然后在brew上基于python创建你的virtualenv:

virtualenv venv --python /usr/local/bin/python

I know, it's a little more hoops to jump through, but it will make your development process that much easier. 我知道,跳过这一点需要更多的努力,但它会让您的开发过程变得更加容易。 Not to mention brew is a great aid to a developer in its own right. 更不用说brew对于开发者来说是一个很好的帮助。

I just checked if it worked, it took me two minutes to get a working MySQLdb. 我只是检查它是否有效,我花了两分钟才得到一个有效的MySQLdb。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM