PyMySQL Python 3 Django VirtualEnv MySQLdb

时间:2016-10-10 14:14:43

标签: python mysql django virtualenv

所以昨天当我从我的主目录运行virtualenv时,我已经运行了。我现在已经多次遇到这个问题而没有明确的答案。我对此做了很多研究,但似乎没有黑白回答。今天我把我的virtualenv移到/usr/local/virtualenvs以更好地管理我的项目和中提琴,它又被打破了。 Error loading MySQLdb module: No module named 'MySQLdb'现在是我存在的祸根。这次我似乎无法修复它。我甚至尝试将我所做的重复复制到我的主目录中并遇到同样的问题。我没有从系统中删除任何包。我在virtualenv中安装了相同的软件包。此时我迷失了。我甚至写下了我从新安装中采取的步骤到达工作的virtualenv。我当然不想重新安装。请帮助:)

虚拟环境:

Ubuntu Server 16.04 LTS
python 3.5
django 1.10.2
pymysql 0.7.9

我的settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'auth',
        'USER': 'user',
        'PASSWORD': 'pass',
        'HOST': 'localhost',
        'PORT': '3306',
    },
    'cogs': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'cogs',
        'USER': 'user',
        'PASSWORD': 'pass',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}

错误:

Traceback (most recent call last):
  File "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 25, in <module>
    import MySQLdb as Database
ImportError: No module named 'MySQLdb'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 341, in execute
    django.setup()
  File "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/site-packages/django/apps/config.py", line 199, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/site-packages/django/contrib/auth/models.py", line 4, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/site-packages/django/contrib/auth/base_user.py", line 52, in <module>
    class AbstractBaseUser(models.Model):
  File "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/site-packages/django/db/models/base.py", line 119, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/site-packages/django/db/models/base.py", line 316, in add_to_class
    value.contribute_to_class(cls, name)
  File "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/site-packages/django/db/models/options.py", line 214, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/site-packages/django/db/__init__.py", line 33, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/site-packages/django/db/utils.py", line 211, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/site-packages/django/db/utils.py", line 115, in load_backend
    return import_module('%s.base' % backend_name)
  File "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/usr/local/virtualenvs/rtservice/serviceenv/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 28, in <module>
    raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'

提前致谢。

1 个答案:

答案 0 :(得分:0)

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

manage.pywsgi.py

现在正在工作。