我正在尝试使用 python 导入 MySQL 模块,更具体地说是 Flask,尽管我收到了一个错误。我在我的应用程序中使用虚拟环境。这是错误:
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
我可以在它说的错误中看到Library not loaded: /Library/Python/2.7/site-packages/_mysql.so
。由于我使用的是虚拟环境,因此路径不正确。应该是/lib/python2.7/site-packages/_mysql.so
。
有没有办法改变这个?谢谢。
编辑:
我发现 OSX 上有一个终端命令可以更改库位置:
sudo install_name_tool -change libmysqlclient.18.dylib /lib/python2.7/site-packages/MySQLdb/
虽然在按回车后我得到了这个:
Usage: /Library/Developer/CommandLineTools/usr/bin/install_name_tool [-change old new] ... [-rpath old new] ... [-add_rpath new] ... [-delete_rpath old] ... [-id name] input
我似乎没有输入错误的命令,这是什么问题?