加载 MySQLdb 模块和“pip install MySQLdb”时出错
Posted
技术标签:
【中文标题】加载 MySQLdb 模块和“pip install MySQLdb”时出错【英文标题】:Error Loading MySQLdb Module and "pip install MySQLdb" 【发布时间】:2016-03-05 22:55:04 【问题描述】:我正在尝试启动一个 Django 项目。
我在尝试运行 manage.py 时收到此错误:
(venv)dyn-160-39-161-214:proj Bren$ python manage.py
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
utility.execute()
File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 328, in execute
django.setup()
File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/django/apps/config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/django/contrib/auth/models.py", line 41, in <module>
class Permission(models.Model):
File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/django/db/models/base.py", line 139, in __new__
new_class.add_to_class('_meta', Options(meta, **kwargs))
File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/django/db/models/base.py", line 324, in add_to_class
value.contribute_to_class(cls, name)
File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/django/db/models/options.py", line 250, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/django/db/__init__.py", line 36, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/django/db/utils.py", line 241, in __getitem__
backend = load_backend(db['ENGINE'])
File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/django/db/utils.py", line 112, in load_backend
return import_module('%s.base' % backend_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 27, in <module>
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
我想知道我应该尝试安装什么模块。
当我使用pip install MySQLdb
时出现此错误:
Collecting MySQLdb
Could not find a version that satisfies the requirement MySQLdb (from versions: )
No matching distribution found for MySQLdb
当我在 SO 和其他地方查找此内容时,似乎那里有很多不同的 mysql 模块。
这也是我为 DB 设置的样子(我没有写这个项目,而且对 django 和 python 很陌生):
DATABASES =
'default':
'ENGINE': 'django.db.backends.mysql',
...
EdBaker 建议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 20, in <module>
File "/private/var/folders/m3/11zknyw55zxbw6zqh58rwq580000gn/T/pip-build-DC9tSL/MySQL-python/setup.py", line 17, in <module>
metadata, options = get_config()
File "setup_posix.py", line 43, in get_config
libs = mysql_config("libs_r")
File "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/m3/11zknyw55zxbw6zqh58rwq580000gn/T/pip-build-DC9tSL/MySQL-python
尝试在 shell 中运行 import MySQLdb:
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named MySQLdb
正在尝试升级 pip:
pip install pip --upgrade
显示 pip 是最新的
Requirement already up-to-date: pip in /Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages
【问题讨论】:
试试这个答案:***.com/questions/25865270/… @EdBaker 没有工作将更新错误。 你能不能尝试启动一个 python shell 并运行 add "import MySQLdb" .. 这样我们就可以判断它是 python 错误还是 django 错误 先尝试更新 pip:“pip install pip --upgrade”,然后尝试“pip install MySQL-python” @Bren 这个答案完全适用于 Mac:***.com/a/33928783/2419215 【参考方案1】:显然安装pip install MySQL-python
是要走的路。问题在于 mysql_config 的位置。看看这里,这可能会对您有所帮助,具体取决于您的操作系统:mysql_config not found when installing mysqldb python interface
编辑:
如果安装脚本失败并显示Command "python setup.py egg_info" failed with error code 1 in ...
,请使用
sudo apt install libmysqlclient-dev
应该解决这个问题。感谢@thirupathi-thangavel。
【讨论】:
命令“python setup.py egg_info”在/tmp/pip-build-FPhc9H/MySQL-python/中失败,错误代码为1sudo apt install libmysqlclient-dev
修复了上述问题。
错误来了:命令“python setup.py egg_info”在/private/var/folders/zs/fq6s3c217pxd3bw8hf9dv0h40000gr/T/pip-install-mdkf0wy8/MySQL-python/中失败,错误代码为1跨度>
【参考方案2】:
brew安装mysql
安装完成后会显示路径,在下面的命令中使用:
sudo PATH=/usr/local/Cellar/mysql/bin/:$PATH pip install mysql-python
【讨论】:
这对我有用。只需更改 breQ -> brew install mysql【参考方案3】:如果上述方法不起作用,请尝试
mysql客户端
pip install mysqlclient
我没有使用 python 版本(3.9) 找到上述库,我安装了 mysqlclient,它运行良好。
【讨论】:
【参考方案4】:我在这里关注了我的问题404 error when using brew to install mysql
然后按照 fodma1 的建议去这里 ***.com/a/33928783/2419215
现在它大部分都可以工作了(还有另一个错误,但那是另一个帖子)!
感谢 EdBaker 和 fodma1!
【讨论】:
如果@Edbaker 或@fodma1!想要制作他们的 cmets 之一,我会接受它作为答案。以上是关于加载 MySQLdb 模块和“pip install MySQLdb”时出错的主要内容,如果未能解决你的问题,请参考以下文章
django.core.exceptions.ImproperlyConfigured:加载 MySQLdb 模块时出错:没有名为“MySQLdb”的模块
Django AWS Elastic Beanstalk 错误配置不当:加载 MySQLdb 模块时出错:没有名为 MySQLdb 的模块
谷歌应用引擎+python(django)部署错误:加载MySQLdb模块时出错
加载 MySQLdb 模块时出错。你安装了mysqlclient吗?在 MacOS 上