获取“加载MySQLdb模块时出错:没有名为MySQLdb的模块” - 尝试过以前发布的解决方案

时间:2010-06-01 18:18:10

标签: python mysql django virtualenv virtualenvwrapper

对于OSX 10.6用户来说,这是一个备受争议的问题,但我找不到有效的解决方案。这是我的设置:

Python 2.6.1 64位 Django 1.2.1 MySQL 5.1.47 osx10.6 64位

我用--no-site-packages创建了一个virtualenvwrapper,然后安装了Django。当我激活virtualenv并运行python manage.py syncdb时,我收到此错误:

Traceback (most recent call last):
File "manage.py", line 11, in <module>
  execute_manager(settings)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/__init__.py", line 438, in execute_manager
  utility.execute()
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/__init__.py", line 379, in execute
  self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/__init__.py", line 257, in fetch_command
  klass = load_command_class(app_name, subcommand)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/__init__.py", line 67, in load_command_class
  module = import_module('%s.management.commands.%s' % (app_name, name))
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
  __import__(name)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/commands/syncdb.py", line 7, in <module>
from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/sql.py", line 5, in <module>
from django.contrib.contenttypes import generic
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/contrib/contenttypes/generic.py", line 6, in <module>
  from django.db import connection
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/db/__init__.py", line 75, in <module>
  connection = connections[DEFAULT_DB_ALIAS]
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/db/utils.py", line 91, in __getitem__
  backend = load_backend(db['ENGINE'])
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/db/utils.py", line 32, in load_backend
  return import_module('.base', backend_name)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
  __import__(name)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/db/backends/mysql/base.py", line 14, in <module>
  raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

我还安装了MySQL for Python适配器,但无济于事(也许我安装不正确?)。

之前有人处理过此事吗?

22 个答案:

答案 0 :(得分:330)

我遇到了同样的错误,pip install MySQL-python为我解决了这个错误。

备用安装:

  • 如果您没有pip,easy_install MySQL-python应该有效。
  • 如果你的python是由打包系统管理的,你可能必须使用 该系统(例如sudo apt-get install ...

下面,Soli指出,如果您收到以下错误:

  

EnvironmentError: mysql_config not found

...然后你还有一个系统依赖问题。解决这个问题因系统而异,但对于Debian派生系统而言:

sudo apt-get install python-mysqldb

答案 1 :(得分:61)

运行Ubuntu,我必须这样做:

sudo apt-get install python-mysqldb

答案 2 :(得分:57)

添加到其他答案,以下帮助我完成了mysql-python的安装:

virtualenv, mysql-python, pip: anyone know how?

在Ubuntu上......

apt-get install libmysqlclient-dev
apt-get install python-dev
pip install mysql-python

如果您没有适当的权限,请不要忘记在命令的开头添加“sudo”。

答案 3 :(得分:22)

尝试以下命令。他们为我工作:

brew install mysql-connector-c 
pip install MySQL-python

答案 4 :(得分:19)

mysql_config必须在路上。在Mac上,执行

export PATH=$PATH:/usr/local/mysql/bin/
pip install MySQL-python

答案 5 :(得分:14)

pip install mysql-python

引发了错误:

  

EnvironmentError:找不到mysql_config

sudo apt-get install python-mysqldb

修复了问题。

答案 6 :(得分:10)

我是如何运作的:

//web service layer calling the catalogservice
public MyObject insertSomething(MyObject something){
    insert(something);
}

//catalogservice calling validation
public MyObject insert(MyObject something){
    MyValidator.validate(something);
}

//myvalidator throwing exception
public static validate(MyObject something){
    //some validation
    throw new ValidationException(new MissingException("Some message"));
}

采购我的环境后:

virtualenv -p python3.5 env/test

然后,我运行了startproject并在manage.py中,我添加了这个:

pip install pymysql
pip install django

此外,更新了此内部设置:

+ try:
+     import pymysql
+     pymysql.install_as_MySQLdb()
+ except:
+     pass

我的virtualenv中也安装了DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'foobar_db', 'USER': 'foobaruser', 'PASSWORD': 'foobarpwd', } } ,不确定是否需要...

希望它有所帮助,

答案 7 :(得分:8)

以下对我来说非常有效,运行Ubuntu 13.10 64位:

sudo apt-get install libmysqlclient-dev
sudo apt-get install python-dev

现在,导航到您的virtualenv(例如env文件夹)并执行以下命令:

sudo ./bin/pip install mysql-python

我实际上在一个单独的问题中找到solution,我在下面引用它:

  

如果您使用--no-site-packages开关创建了virtualenv   (默认值),然后是系统范围内安装的添加项,例如MySQLdb   不包含在虚拟环境包中。

     

你需要安装带有pip命令的MySQLdb   virtualenv中。使用bin / activate激活virtualenv   脚本,或使用virtualenv中的bin / pip来安装   MySQLdb库也在本地。

     

或者,使用系统站点包创建一个新的virtualenv   使用--system-site-package开关包含。

我认为这也适用于OSX。唯一的问题是获得安装libmysqlclient-devpython-dev的等效命令,因为它们需要编译 我想mysql-python

希望这有帮助。

答案 8 :(得分:8)

试试这个:这解决了我的问题。

  

pip install MySQL-python

答案 9 :(得分:7)

此问题是MySQL for Python适配器安装不完整/不正确的结果。具体来说,我必须编辑mysql_config文件的路径以指向/ usr / local / mysql / bin / mysql_config - 本文将对此进行更详细的讨论:http://dakrauth.com/blog/entry/python-and-django-setup-mac-os-x-leopard/

答案 10 :(得分:7)

sudo apt-get install python-mysqldb在ubuntu中完美运作

pip install mysql-python引发环境错误

答案 11 :(得分:5)

这适用于Red Hat Enterprise Linux Server 6.4版

sudo yum install mysql-devel
sudo yum install python-devel
pip install mysql-python

答案 12 :(得分:4)

您可以安装为pip install mysqlclient

答案 13 :(得分:3)

我升级到OSX Mavericks和Pycharm 3并开始出现此错误,我使用pip并轻松安装并得到错误:

  

命令'/ usr / bin / clang'以退出状态1失败。

所以我需要更新到Xcode 5并再次尝试使用pip进行安装。

pip install mysql-python

解决所有问题。

答案 14 :(得分:2)

这里引发的错误是导入python模块。这可以通过将python site-packages文件夹添加到OS X上的环境变量$ PYTHONPATH来解决。因此我们可以将以下命令添加到.bash_profile文件中:

export PYTHONPATH="$PYTHONPATH:/usr/local/lib/pythonx.x/site-packages/"

*用你正在使用的python版本替换x.x

答案 15 :(得分:2)

如果您使用的是python3,请尝试此操作(我的操作系统是Ubuntu 16.04):

sudo apt-get install python3-mysqldb

答案 16 :(得分:2)

在Windows 8 64位系统上,

pip对我不起作用。 easy_install mysql-python适合我。 如果pip不起作用,您可以使用easy_install来避免在Windows上构建二进制文件。

答案 17 :(得分:1)

我在OSX 10.6.6上遇到了同样的问题。但是,终端上只有一个简单的easy_install mysql-python没有解决它,因为另一个打嗝:

error: command 'gcc-4.2' failed with exit status 1

显然,从XCode3(本机与OSX 10.6一起提供)升级到XCode4后出现此问题。这个较新的版本删除了对构建ppc arch的支持。如果是相同的情况,请尝试在easy_install mysql-python

之前执行以下操作
sudo bash
export ARCHFLAGS='-arch i386 -arch x86_64'
rm -r build
python setup.py build
python setup.py install

非常感谢Ned Deily提供此解决方案。 Check here

答案 18 :(得分:0)

对我来说,只需重新安装mysql-python即可解决问题

pip uninstall mysql-python
pip install mysql-python

答案 19 :(得分:0)

安装命令行工具对我有用:

xcode-select --install

答案 20 :(得分:0)

我使用 pip 安装 MySQL-python 库克服了同样的问题。当我第一次在settings.py中更改数据库设置并执行makemigrations命令时,您可以看到我的控制台上显示的消息(解决方案是遵循以下消息,只是看到它)。

  (vir_env) admins-MacBook-Pro-3:src admin$ python manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/core/management/__init__.py", line 312, in execute
    django.setup()
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/local/Cellar/python/2.7.12_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/contrib/auth/models.py", line 41, in <module>
    class Permission(models.Model):
  File "/Users/admin/Desktop/SetUp1/vir_env/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/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/db/models/base.py", line 324, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/admin/Desktop/SetUp1/vir_env/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/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/db/__init__.py", line 36, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/db/utils.py", line 240, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/db/utils.py", line 111, in load_backend
    return import_module('%s.base' % backend_name)
  File "/usr/local/Cellar/python/2.7.12_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/admin/Desktop/SetUp1/vir_env/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

最后,我克服了以下问题:

(vir_env) admins-MacBook-Pro-3:src admin$ pip install MySQLdb
Collecting MySQLdb
  Could not find a version that satisfies the requirement MySQLdb (from versions: )
No matching distribution found for MySQLdb
(vir_env) admins-MacBook-Pro-3:src admin$ pip install MySQL-python
Collecting MySQL-python
  Downloading MySQL-python-1.2.5.zip (108kB)
    100% |████████████████████████████████| 112kB 364kB/s 
Building wheels for collected packages: MySQL-python
  Running setup.py bdist_wheel for MySQL-python ... done
  Stored in directory: /Users/admin/Library/Caches/pip/wheels/38/a3/89/ec87e092cfb38450fc91a62562055231deb0049a029054dc62
Successfully built MySQL-python
Installing collected packages: MySQL-python
Successfully installed MySQL-python-1.2.5
(vir_env) admins-MacBook-Pro-3:src admin$ python manage.py makemigrations
No changes detected
(vir_env) admins-MacBook-Pro-3:src admin$ python manage.py migrate
Operations to perform:
  Synchronize unmigrated apps: staticfiles, rest_framework, messages, crispy_forms
  Apply all migrations: admin, contenttypes, sessions, auth, PyApp
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states... DONE
  Applying PyApp.0001_initial... OK
  Applying PyApp.0002_auto_20170310_0936... OK
  Applying PyApp.0003_auto_20170310_0953... OK
  Applying PyApp.0004_auto_20170310_0954... OK
  Applying PyApp.0005_auto_20170311_0619... OK
  Applying PyApp.0006_auto_20170311_0622... OK
  Applying PyApp.0007_loraevksensor... OK
  Applying PyApp.0008_auto_20170315_0752... OK
  Applying PyApp.0009_auto_20170315_0753... OK
  Applying PyApp.0010_auto_20170315_0806... OK
  Applying PyApp.0011_auto_20170315_0814... OK
  Applying PyApp.0012_auto_20170315_0820... OK
  Applying PyApp.0013_auto_20170315_0822... OK
  Applying PyApp.0014_auto_20170315_0907... OK
  Applying PyApp.0015_auto_20170315_1041... OK
  Applying PyApp.0016_auto_20170315_1355... OK
  Applying PyApp.0017_auto_20170315_1401... OK
  Applying PyApp.0018_auto_20170331_1348... OK
  Applying PyApp.0019_auto_20170331_1349... OK
  Applying PyApp.0020_auto_20170331_1350... OK
  Applying PyApp.0021_auto_20170331_1458... OK
  Applying PyApp.0022_delete_postoffice... OK
  Applying PyApp.0023_posoffice... OK
  Applying PyApp.0024_auto_20170331_1504... OK
  Applying PyApp.0025_auto_20170331_1511... OK
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying sessions.0001_initial... OK
(vir_env) admins-MacBook-Pro-3:src admin$ 

答案 21 :(得分:0)

我遇到了类似的情况,例如您在Mac OS X上的virtualenv中使用python3.7和django 2.1。 尝试运行命令:

<=5 or between 6 and 15 or >=16

并在项目文件夹中编辑pip install mysql-python pip install pymysql 文件,并添加以下内容:

__init__.py

然后运行:import pymysql pymysql.install_as_MySQLdb() python3 manage.py runserver