1

我已经开始在我的 Django 开发中使用 Pycharm 并且遇到了这个错误。我知道我的 MySQLdb 正在工作,因为我可以在 python shell 中尝试导入它,并且可以在命令行上运行 syncdb 命令。我的问题是我需要向 Pycharm 添加什么才能使 MySQLdb 被识别。如果这有什么不同,我正在使用 Mac OS X。我是否需要在我的 manage.py 中添加一些内容,或者是否需要将其添加到 Pycharm 的首选项中?或者它是不同的东西。谢谢你们的帮助。

这是我得到的错误:

    Traceback (most recent call last):
      File "/Applications/PyCharm.app/helpers/pycharm/django_manage.py", line 21, in <module>
        run_module(manage_file, None, '__main__', True)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 176, in run_module
        fname, loader, pkg_name)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 82, in _run_module_code
        mod_name, mod_fname, mod_loader, pkg_name)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
        exec code in run_globals
      File "/Users/Brandon/PycharmProjects/Database_assignment/manage.py", line 10, in <module>
        execute_from_command_line(sys.argv)
      File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
        utility.execute()
      File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 382, in execute
        self.fetch_command(subcommand).run_from_argv(self.argv)
      File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 261, in fetch_command
        klass = load_command_class(app_name, subcommand)
      File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 69, in load_command_class
        module = import_module('%s.management.commands.%s' % (app_name, name))
      File "/Library/Python/2.7/site-packages/django/utils/importlib.py", line 35, in import_module
        __import__(name)
      File "/Library/Python/2.7/site-packages/django/core/management/commands/syncdb.py", line 8, in <module>
        from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
      File "/Library/Python/2.7/site-packages/django/core/management/sql.py", line 6, in <module>
        from django.db import models
      File "/Library/Python/2.7/site-packages/django/db/__init__.py", line 40, in <module>
        backend = load_backend(connection.settings_dict['ENGINE'])
      File "/Library/Python/2.7/site-packages/django/db/__init__.py", line 34, in __getattr__
        return getattr(connections[DEFAULT_DB_ALIAS], item)
      File "/Library/Python/2.7/site-packages/django/db/utils.py", line 92, in __getitem__
        backend = load_backend(db['ENGINE'])
      File "/Library/Python/2.7/site-packages/django/db/utils.py", line 24, in load_backend
        return import_module('.base', backend_name)
      File "/Library/Python/2.7/site-packages/django/utils/importlib.py", line 35, in import_module
        __import__(name)
      File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 16, in <module>
        raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
    django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

Process finished with exit code 1
4

3 回答 3

0

尝试

点安装 mysql-python

我认为在 Pycharm 中,您可以通过 GUI 安装软件包。但本质上你需要安装mysql-python

如果您收到“权限被拒绝消息”,您可能需要执行 sudo pip install mysql-python

于 2013-03-29T15:39:07.957 回答
0

PyCharm 定义了用于每个项目的解释器,因此您需要仔细检查项目是否设置为使用:

  1. 您设置的 virtualenv
  2. 系统 Python,如果您不使用 virtualenv(您应该使用)。
于 2013-03-30T06:32:25.297 回答
0

如果您使用的是 python 3,mysql-python 将无法安装,因为它不支持 python 3。请改用 mysqlclient。更多信息在这里

于 2015-05-22T13:59:43.253 回答