django中的“没有模块名称mysqldb”错误[重复]
Posted
技术标签:
【中文标题】django中的“没有模块名称mysqldb”错误[重复]【英文标题】:"No module name mysqldb" error in django [duplicate] 【发布时间】:2015-05-16 22:56:05 【问题描述】:我已经在 ubantu 14.04 上使用 mysql 数据库引擎安装了 django 1.7(python 2.7.6),但在运行命令-
python manage.py 迁移
我遇到了错误
(env1)shiv@shiv-Calpella:~/Project/Poll_app/mysite$ python manage.py migrate
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
django.setup()
File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/contrib/auth/models.py", line 40, in <module>
class Permission(models.Model):
File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/db/models/base.py", line 122, in __new__
new_class.add_to_class('_meta', Options(meta, **kwargs))
File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/db/models/base.py", line 297, in add_to_class
value.contribute_to_class(cls, name)
File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/db/models/options.py", line 166, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/db/__init__.py", line 40, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/db/utils.py", line 242, in __getitem__
backend = load_backend(db['ENGINE'])
File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/db/utils.py", line 108, in load_backend
return import_module('%s.base' % backend_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 18, in <module>
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
我试过通过命令安装mysqldb
pip 安装 MySQL-python
然后我得到以下错误
(env1)shiv@shiv-Calpella:~/Project/Poll_app/mysite$ pip install MySQL-pythonCollecting MySQL-python
Using cached MySQL-python-1.2.5.zip
sh: 1: mysql_config: not found
Traceback (most recent call last):
File "<string>", line 20, in <module>
File "/tmp/pip-build-3FRYWs/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
Complete output from command python setup.py egg_info:
sh: 1: mysql_config: not found
Traceback (most recent call last):
File "<string>", line 20, in <module>
File "/tmp/pip-build-3FRYWs/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 /tmp/pip-build-3FRYWs/MySQL-python
我的setting.py文件是
DATABASES =
'default':
'ENGINE': 'django.db.backends.mysql',
'NAME': 'poll_db',
'USER': 'root',
'PASSWORD': 'root',
【问题讨论】:
您使用的是什么操作系统?Command "python setup.py egg_info" failed with error code 1
并不是真正的错误信息。请在此行上方查看实际错误并将其添加到您的问题中。
***.com/questions/7459766/installing-mysql-python
我已经更新了错误
@ShivDangi 谢谢。所以实际的错误是EnvironmentError: mysql_config not found
。这意味着在您的路径中找不到mysql_config
,因此MySQL-python
的安装失败。我作为重复链接的问题应该可以帮助您解决这个问题(@WojciechPolak 的回答也应该如此)。
【参考方案1】:
在上面的例子中,答案可能在正确的日志文件pip.log
或/tmp/pip-build-rHe_Sb/MySQL-python
的某个地方。
1) 使用 PIP 安装 MySQL 模块时,请确保您的系统中已安装例如 mysql-devel
(yum) 或 libmysqlclient-dev
(apt-get) 软件包。
2) 顺便说一句,我会推荐 mysqlclient
($ pip install mysqlclient
) 而不是 MySQL-python
,因为它同时支持 Python 2 和 3.x。
【讨论】:
【参考方案2】:您有 2 个选项,如下所述:
分发包是-:
sudo apt-get install python-mysqldb
在这种情况下,您不能使用 virtualenv no-site-packages(默认选项),但必须使用:
virtualenv --system-site-packages myenv
使用干净的 virtualenv 并构建您自己的 python-mysql 包。
首先创建virtualenv:
虚拟环境我的虚拟环境
来源 myvirtualenv/bin/activate
然后安装构建依赖:
sudo apt-get build-dep python-mysqldb
现在你可以安装python-mysql了
pip 安装 mysql-python
注意 Ubuntu 包是 python-mysqldb,python pypi 包是 python-mysql (没有 db)
【讨论】:
以上是关于django中的“没有模块名称mysqldb”错误[重复]的主要内容,如果未能解决你的问题,请参考以下文章
如何解决 django 中的 NoReverseMatch 错误 [重复]
如何修复 Reactjs、Django、Django Rest Frame 工作项目中的“net::ERR_CONNECTION_REFUSED 和错误:网络错误”错误