加载 MySQLdb 模块时出错,"pip install MySQLdb"
Error Loading MySQLdb Module and "pip install MySQLdb"
我正在尝试启动 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 模块。
这也是我的数据库设置(我没有编写这个项目并且对 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 中导入 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
显示的点是最新的
Requirement already up-to-date: pip in /Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages
我在这里关注了我的问题 404 error when using brew to install mysql
然后按照 fodma1 的建议转到此处 Whosebug。com/a/33928783/2419215
现在大部分都可以使用(有另一个错误,但那是另一个 post)!
感谢 EdBaker 和 fodma1!
显然安装 pip install MySQL-python
是正确的选择。问题在于 mysql_config 的位置。看看这里,这可能对你有所帮助,具体取决于你的 OS:mysql_config not found when installing mysqldb python interface
编辑:
如果安装脚本因 Command "python setup.py egg_info" failed with error code 1 in {...}
而失败,请使用
安装 libmysqlclient-dev
sudo apt install libmysqlclient-dev
应该可以解决问题。感谢@thirupathi-thangavel。
brew 安装mysql
路径将在安装完成后显示,请在以下命令中使用:
sudo PATH=/usr/local/Cellar/mysql/bin/:$PATH pip install mysql-python
如果上述方法无效,请尝试
mysqlclient
pip install mysqlclient
I did not found the above library using python version(3.9) and I installed the mysqlclient and it worked for fine.
我正在尝试启动 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 模块。
这也是我的数据库设置(我没有编写这个项目并且对 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 中导入 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
显示的点是最新的
Requirement already up-to-date: pip in /Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages
我在这里关注了我的问题 404 error when using brew to install mysql
然后按照 fodma1 的建议转到此处 Whosebug。com/a/33928783/2419215
现在大部分都可以使用(有另一个错误,但那是另一个 post)!
感谢 EdBaker 和 fodma1!
显然安装 pip install MySQL-python
是正确的选择。问题在于 mysql_config 的位置。看看这里,这可能对你有所帮助,具体取决于你的 OS: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。
brew 安装mysql
路径将在安装完成后显示,请在以下命令中使用:
sudo PATH=/usr/local/Cellar/mysql/bin/:$PATH pip install mysql-python
如果上述方法无效,请尝试
mysqlclient
pip install mysqlclient
I did not found the above library using python version(3.9) and I installed the mysqlclient and it worked for fine.