不能让pymysql与我的python 3.4.1环境一起工作

时间:2014-06-02 19:05:40

标签: python django pymysql

我可能会做一些明显错误的事情,但我已经存货了很长时间并且必须得到帮助。很抱歉,如果它显而易见且愚蠢。

有人可以看一下吗?我非常感谢你的帮助。

我有python3.4.1并且需要使用pymysql但syncdb失败:

> (yara-proj)~/django-projects/yara-project/yaraSite$> python manage.py syncdb

Traceback (most recent call last):
  File "/Users/Gemeni/.virtualenvs/yara-proj/lib/python3.4/site-packages/django/db/utils.py", line 113, in load_backend
    return import_module('%s.base' % backend_name)
  File "/Users/Gemeni/.virtualenvs/yara-proj/lib/python3.4/importlib/__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1191, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1161, in _load_backward_compatible
  File "/Users/Gemeni/.virtualenvs/yara-proj/lib/python3.4/site-packages/django_mysql_pymysql-0.1-py3.4.egg/mysql_pymysql/base.py", line 29, in <module>
ImportError: cannot import name 'SafeUnicode'

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 "/Users/Gemeni/.virtualenvs/yara-proj/lib/python3.4/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/Users/Gemeni/.virtualenvs/yara-proj/lib/python3.4/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/Gemeni/.virtualenvs/yara-proj/lib/python3.4/site-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Users/Gemeni/.virtualenvs/yara-proj/lib/python3.4/site-packages/django/core/management/base.py", line 280, in execute
    translation.activate('en-us')
  File "/Users/Gemeni/.virtualenvs/yara-proj/lib/python3.4/site-packages/django/utils/translation/__init__.py", line 130, in activate
    return _trans.activate(language)
  File "/Users/Gemeni/.virtualenvs/yara-proj/lib/python3.4/site-packages/django/utils/translation/trans_real.py", line 188, in activate
    _active.value = translation(language)
  File "/Users/Gemeni/.virtualenvs/yara-proj/lib/python3.4/site-packages/django/utils/translation/trans_real.py", line 177, in translation
    default_translation = _fetch(settings.LANGUAGE_CODE)
  File "/Users/Gemeni/.virtualenvs/yara-proj/lib/python3.4/site-packages/django/utils/translation/trans_real.py", line 159, in _fetch
    app = import_module(appname)
  File "/Users/Gemeni/.virtualenvs/yara-proj/lib/python3.4/importlib/__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/Users/Gemeni/.virtualenvs/yara-proj/lib/python3.4/site-packages/django/contrib/admin/__init__.py", line 6, in <module>
    from django.contrib.admin.sites import AdminSite, site
  File "/Users/Gemeni/.virtualenvs/yara-proj/lib/python3.4/site-packages/django/contrib/admin/sites.py", line 4, in <module>
    from django.contrib.admin.forms import AdminAuthenticationForm
  File "/Users/Gemeni/.virtualenvs/yara-proj/lib/python3.4/site-packages/django/contrib/admin/forms.py", line 6, in <module>
    from django.contrib.auth.forms import AuthenticationForm
  File "/Users/Gemeni/.virtualenvs/yara-proj/lib/python3.4/site-packages/django/contrib/auth/forms.py", line 17, in <module>
    from django.contrib.auth.models import User
  File "/Users/Gemeni/.virtualenvs/yara-proj/lib/python3.4/site-packages/django/contrib/auth/models.py", line 48, in <module>
    class Permission(models.Model):
  File "/Users/Gemeni/.virtualenvs/yara-proj/lib/python3.4/site-packages/django/db/models/base.py", line 96, in __new__
    new_class.add_to_class('_meta', Options(meta, **kwargs))
  File "/Users/Gemeni/.virtualenvs/yara-proj/lib/python3.4/site-packages/django/db/models/base.py", line 264, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/Gemeni/.virtualenvs/yara-proj/lib/python3.4/site-packages/django/db/models/options.py", line 124, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/Users/Gemeni/.virtualenvs/yara-proj/lib/python3.4/site-packages/django/db/__init__.py", line 34, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/Users/Gemeni/.virtualenvs/yara-proj/lib/python3.4/site-packages/django/db/utils.py", line 198, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/Users/Gemeni/.virtualenvs/yara-proj/lib/python3.4/site-packages/django/db/utils.py", line 131, in load_backend
    raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: 'mysql_pymysql' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
    'mysql', 'oracle', 'postgresql_psycopg2', 'sqlite3'
Error was: cannot import name 'SafeUnicode'

这是我在settings.py中的内容

# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'mysql_pymysql',
        'HOST': '127.0.0.1',
        'USER': 'root',
        'PASSWORD': 'root',
    }
}

这就是环境的设置方式。

(yara-proj)~/django-projects/yara-project/yaraSite$> ls

manage.py   yaraSite

(yara-proj)~/django-projects/yara-project/yaraSite$> cd yaraSite/

(yara-proj)~/django-projects/yara-project/yaraSite/yaraSite$> ls

__init__.py 
pycache__   
django-mysql-pymysql    
settings.py     
urls.py         
wsgi.py

1 个答案:

答案 0 :(得分:0)

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'HOST': '127.0.0.1',
        'USER': 'root'
        'PASSWORD': 'root',
    }
}
答案是答案。 PyMySQL不是受支持的后端。你需要安装C绑定python-MySQL来使用mysql和Django。

或者您可以尝试此处描述的解决方案How to make Django work with unsupported MySQL drivers such as gevent-mysql or Concurrence's MySQL driver?