我使用 Windows 10
和 Python 3
在 Django 1.9.7
中使用网络应用程序
我无法将应用程序连接到 MySQL
数据库。
我使用 MySQL
通过 PYMySQL
<安装了与 cmd
建立连接的特定工具/ p>
使用pip install PYMySQL
(bvc_env) C:\Users\Web\Desktop\PyDev\projects\bvc_env\src>pip install pymysql
Collecting pymysql
Using cached PyMySQL-0.7.5-py2.py3-none-any.whl
Installing collected packages: pymysql
Successfully installed pymysql-0.7.5
以下是Settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db_app',
'HOST': 'host_app',
'PORT': '3306',
'USER': 'root',
'PASSWORD': 'admin',
}
}
然后我保存并开始使用此代码进行迁移:
(bvc_env) C:\Users\Web\Desktop\PyDev\projects\bvc_env\src>python manage.py migrate
我收到此错误
Traceback (most recent call last):
File "C:\Users\Web\Desktop\PyDev\projects\bvc_env\lib\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 "C:\Users\Web\Desktop\PyDev\projects\bvc_env\lib\site-packages\django\core\management\__init__.py", line 353, in execute_from_command_line
utility.execute()
File "C:\Users\Web\Desktop\PyDev\projects\bvc_env\lib\site-packages\django\core\management\__init__.py", line 327, in execute
django.setup()
File "C:\Users\Web\Desktop\PyDev\projects\bvc_env\lib\site-packages\django\__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\Web\Desktop\PyDev\projects\bvc_env\lib\site-packages\django\apps\registry.py", line 108, in populate
app_config.import_models(all_models)
File "C:\Users\Web\Desktop\PyDev\projects\bvc_env\lib\site-packages\django\apps\config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\Web\Desktop\PyDev\projects\bvc_env\lib\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 "C:\Users\Web\Desktop\PyDev\projects\bvc_env\lib\site-packages\django\contrib\auth\models.py", line 4, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "C:\Users\Web\Desktop\PyDev\projects\bvc_env\lib\site-packages\django\contrib\auth\base_user.py", line 49, in <module>
class AbstractBaseUser(models.Model):
File "C:\Users\Web\Desktop\PyDev\projects\bvc_env\lib\site-packages\django\db\models\base.py", line 108, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "C:\Users\Web\Desktop\PyDev\projects\bvc_env\lib\site-packages\django\db\models\base.py", line 307, in add_to_class
value.contribute_to_class(cls, name)
File "C:\Users\Web\Desktop\PyDev\projects\bvc_env\lib\site-packages\django\db\models\options.py", line 263, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "C:\Users\Web\Desktop\PyDev\projects\bvc_env\lib\site-packages\django\db\__init__.py", line 36, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "C:\Users\Web\Desktop\PyDev\projects\bvc_env\lib\site-packages\django\db\utils.py", line 212, in __getitem__
backend = load_backend(db['ENGINE'])
File "C:\Users\Web\Desktop\PyDev\projects\bvc_env\lib\site-packages\django\db\utils.py", line 116, in load_backend
return import_module('%s.base' % backend_name)
File "C:\Users\Web\Desktop\PyDev\projects\bvc_env\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "C:\Users\Web\Desktop\PyDev\projects\bvc_env\lib\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'
但这引起了我的注意,我不知道如何解决这些问题
ImportError: No module named 'MySQLdb'
或Error loading MySQLdb module: No module named 'MySQLdb'
问题更新费用
pip install mysql-python
安装其他连接器,但它似乎不适用于Python 3
通过将
pymysql
导入您的manage.py file
来获得我的解决方案,这是完整的代码。
#!/usr/bin/env python
import os
import sys
import pymysql
pymysql.install_as_MySQLdb()
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "bvc.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)