简体   繁体   English

Django没有正确连接到MySQL,抛出“ImportError:无法导入名称'NULL'”

[英]Django not connecting to MySQL properly, throws “ImportError: cannot import name 'NULL'”

I want to use Django (using Python 3) with MySQL. 我想在MySQL中使用Django(使用Python 3)。 To do so, I installed mysqlclient . 为此,我安装了mysqlclient To first test the waters with MySQL, I also created a new Django project and app, changed the DATABASES configuration in "settings.py" to settings I prepared in MySQL, and ensured that MySQL server is running (the database I created for testing does not have any tables within it at this point). 为了首先使用MySQL测试水域,我还创建了一个新的Django项目和应用程序,将“settings.py”中的DATABASES配置更改为我在MySQL中准备的设置,并确保MySQL服务器正在运行(我为测试创建的数据库)此时没有任何表格)。

But when I do 但是,当我这样做

python manage.py runserver

in terminal command, I get the following error: 在终端命令中,我收到以下错误:

ImportError: No module named '_mysql_exceptions'
ImportError: No module named '_mysql_exceptions'
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x103efaea0>
Traceback (most recent call last):
  File "/Users/shtryts/Django-1.10.1/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/Users/shtryts/Django-1.10.1/django/core/management/commands/runserver.py", line 113, in inner_run
    autoreload.raise_last_exception()
  File "/Users/shtryts/Django-1.10.1/django/utils/autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "/Users/shtryts/Django-1.10.1/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/Users/shtryts/Django-1.10.1/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/Users/shtryts/Django-1.10.1/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/shtryts/Django-1.10.1/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/Users/shtryts/Django-1.10.1/django/apps/config.py", line 199, in import_models
    self.models_module = import_module(models_module_name)
  File "/Library/Frameworks/Python.framework/Versions/3.5/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 "/Users/shtryts/Django-1.10.1/django/contrib/auth/models.py", line 4, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/Users/shtryts/Django-1.10.1/django/contrib/auth/base_user.py", line 52, in <module>
    class AbstractBaseUser(models.Model):
  File "/Users/shtryts/Django-1.10.1/django/db/models/base.py", line 119, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/Users/shtryts/Django-1.10.1/django/db/models/base.py", line 316, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/shtryts/Django-1.10.1/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 "/Users/shtryts/Django-1.10.1/django/db/__init__.py", line 36, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/Users/shtryts/Django-1.10.1/django/db/utils.py", line 211, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/Users/shtryts/Django-1.10.1/django/db/utils.py", line 115, in load_backend
    return import_module('%s.base' % backend_name)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/Users/shtryts/Django-1.10.1/django/db/backends/mysql/base.py", line 31, in <module>
    from MySQLdb.converters import Thing2Literal, conversions       # isort:skip
  File "/Users/shtryts/.virtualenvs/django/lib/python3.5/site-packages/MySQLdb/converters.py", line 35, in <module>
    from _mysql import string_literal, escape_sequence, escape_dict, escape, NULL
ImportError: cannot import name 'NULL'

Should I be modifying a .py file within MySQLdb folder? 我应该修改MySQLdb文件夹中的.py文件吗? If so, what should I do so that the Django project properly connects with the database? 如果是这样,我应该怎么做才能使Django项目与数据库正确连接?

I'm testing in a virtualenv, Mac OS X 10.10.3. 我正在测试一个virtualenv,Mac OS X 10.10.3。 Thanks in advance for the help. 先谢谢您的帮助。 (Also, I did not find a similar question as mine, but apologies if this has been addressed here before). (另外,我没有找到与我类似的问题,但如果此处已在此处解决过道歉)。

EDIT: I failed to share my DATABASE setting, so here it is. 编辑:我没有分享我的DATABASE设置,所以在这里。

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test1',
        'USER': 'admin',
        'PASSWORD': 'dbadmin',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}

Settings.py, the django project I created for testing purposes, mysqlclient installs, django framework, they're all inside virtualenv. Settings.py,我为测试目的创建的django项目,mysqlclient安装,django框架,它们都在virtualenv中。 Originally, mysqlclient (includes MySQLdb) was installed via "pip install" under the path /Library/Frameworks/Python.framework/Versions/3.5... I made a copy of the installed folders and placed them under the python site-package folder within virtualenv. 最初,mysqlclient(包括MySQLdb)是通过路径/Library/Frameworks/Python.framework/Versions/3.5下的“pip install”安装的......我制作了已安装文件夹的副本并将它们放在python site-package文件夹下在virtualenv内。 From what I understand Django does not have a problem locating those folders and files. 根据我的理解,Django在查找这些文件夹和文件时没有问题。

which pip output: /Users/shtryts/.virtualenvs/django/bin/pip. pip输出:/Users/shtryts/.virtualenvs/django/bin/pip。 And my own code is in .virtualenvs/django/test/mySQLtest 我自己的代码是在.virtualenvs / django / test / mySQLtest中

Ok, there are several problems here. 好的,这里有几个问题。

First of all you cannot copy files from one python installation or virtual installation to another python installation or virtualenv. 首先,您无法将文件从一个python安装或虚拟安装复制到另一个python安装或virtualenv。 Please delete your current virtualenv and make a new one, detailed instructions are here: http://docs.python-guide.org/en/latest/dev/virtualenvs/ 请删除您当前的virtualenv并制作一个新的,详细说明如下: http//docs.python-guide.org/en/latest/dev/virtualenvs/

but briefly 但简单地说

rm -rf /Users/shtryts/.virtualenvs/django
virtualenv /Users/shtryts/.virtualenvs/myvirtualenv

I recommend not using django as the name of the virtualenv. 我建议不要使用django作为virtualenv的名称。 Use a name that shows which version of python is being used. 使用一个名称来显示正在使用的python版本。 Now activate the virtualenv before going to the next step. 现在激活 virtualenv,然后再进行下一步。

Then you should install all the requirements with pip such as 然后你应该用pip安装所有的要求,比如

pip install django 

Please note that 'MysqlDB' is not compatible with python 3x please see here for details: https://docs.djangoproject.com/en/1.10/ref/databases/#mysql-db-api-drivers 请注意'MysqlDB'与python 3x不兼容,请参阅此处了解详情: https ://docs.djangoproject.com/en/1.10/ref/databases/#mysql-db-api-drivers

Last but not least. 最后但并非最不重要的。 one does not put one's own code inisde the django installation folders. 一个人不会把自己的代码放在django安装文件夹中。 instead in your home or workspace 而是在你的家或工作区

django-admin starproject myproject

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM