E:WF_WorkHappyAutoTest>python manage.py startapp userauth Traceback (most recent call last): File "D:Program FilesPython3.6libsite-packagesdjangodbackendsmysqlase.py", line 16, in <module> import MySQLdb as Database ModuleNotFoundError: No module named 'MySQLdb' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "manage.py", line 21, in <module> main() File "manage.py", line 17, in main execute_from_command_line(sys.argv) File "D:Program FilesPython3.6libsite-packagesdjangocoremanagement\__init__.py", line 401, in execute_from_command_line utility.execute() File "D:Program FilesPython3.6libsite-packagesdjangocoremanagement\__init__.py", line 377, in execute django.setup() File "D:Program FilesPython3.6libsite-packagesdjango\__init__.py", line 24, in setup apps.populate(settings.INSTALLED_APPS) File "D:Program FilesPython3.6libsite-packagesdjangoapps egistry.py", line 114, in populate app_config.import_models() File "D:Program FilesPython3.6libsite-packagesdjangoappsconfig.py", line 211, in import_models self.models_module = import_module(models_module_name) File "D:Program FilesPython3.6libimportlib\__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 978, in _gcd_import File "<frozen importlib._bootstrap>", line 961, in _find_and_load File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 655, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 678, in exec_module File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed File "D:Program FilesPython3.6libsite-packagesdjangocontribauthmodels.py", line 2, in <module> from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager File "D:Program FilesPython3.6libsite-packagesdjangocontribauthase_user.py", line 47, in <module> class AbstractBaseUser(models.Model): File "D:Program FilesPython3.6libsite-packagesdjangodbmodelsase.py", line 121, in __new__ new_class.add_to_class('_meta', Options(meta, app_label)) File "D:Program FilesPython3.6libsite-packagesdjangodbmodelsase.py", line 325, in add_to_class value.contribute_to_class(cls, name) File "D:Program FilesPython3.6libsite-packagesdjangodbmodelsoptions.py", line 208, in contribute_to_class self.db_table = truncate_name(self.db_table, connection.ops.max_name_length()) File "D:Program FilesPython3.6libsite-packagesdjangodb\__init__.py", line 28, in __getattr__ return getattr(connections[DEFAULT_DB_ALIAS], item) File "D:Program FilesPython3.6libsite-packagesdjangodbutils.py", line 207, in __getitem__ backend = load_backend(db['ENGINE']) File "D:Program FilesPython3.6libsite-packagesdjangodbutils.py", line 111, in load_backend return import_module('%s.base' % backend_name) File "D:Program FilesPython3.6libimportlib\__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "D:Program FilesPython3.6libsite-packagesdjangodbackendsmysqlase.py", line 21, in <module> ) from err django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module. Did you install mysqlclient?
解决方案
1、在 Python3.6Libsite-packagesdjangodbackendsmysqlase.py 中 加入以下两行
import pymysql
pymysql.install_as_MySQLdb()
2、再在项目中的init.py 中添加两行
import pymysql
pymysql.install_as_MySQLdb()
3、执行发现 还是报错
Traceback (most recent call last): File "manage.py", line 21, in <module> main() File "manage.py", line 17, in main execute_from_command_line(sys.argv) File "D:Program FilesPython3.6libsite-packagesdjangocoremanagement\__init__.py", line 401, in execute_from_command_line utility.execute() File "D:Program FilesPython3.6libsite-packagesdjangocoremanagement\__init__.py", line 377, in execute django.setup() File "D:Program FilesPython3.6libsite-packagesdjango\__init__.py", line 24, in setup apps.populate(settings.INSTALLED_APPS) File "D:Program FilesPython3.6libsite-packagesdjangoapps egistry.py", line 114, in populate app_config.import_models() File "D:Program FilesPython3.6libsite-packagesdjangoappsconfig.py", line 211, in import_models self.models_module = import_module(models_module_name) File "D:Program FilesPython3.6libimportlib\__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 978, in _gcd_import File "<frozen importlib._bootstrap>", line 961, in _find_and_load File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 655, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 678, in exec_module File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed File "D:Program FilesPython3.6libsite-packagesdjangocontribauthmodels.py", line 2, in <module> from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager File "D:Program FilesPython3.6libsite-packagesdjangocontribauthase_user.py", line 47, in <module> class AbstractBaseUser(models.Model): File "D:Program FilesPython3.6libsite-packagesdjangodbmodelsase.py", line 121, in __new__ new_class.add_to_class('_meta', Options(meta, app_label)) File "D:Program FilesPython3.6libsite-packagesdjangodbmodelsase.py", line 325, in add_to_class value.contribute_to_class(cls, name) File "D:Program FilesPython3.6libsite-packagesdjangodbmodelsoptions.py", line 208, in contribute_to_class self.db_table = truncate_name(self.db_table, connection.ops.max_name_length()) File "D:Program FilesPython3.6libsite-packagesdjangodb\__init__.py", line 28, in __getattr__ return getattr(connections[DEFAULT_DB_ALIAS], item) File "D:Program FilesPython3.6libsite-packagesdjangodbutils.py", line 207, in __getitem__ backend = load_backend(db['ENGINE']) File "D:Program FilesPython3.6libsite-packagesdjangodbutils.py", line 111, in load_backend return import_module('%s.base' % backend_name) File "D:Program FilesPython3.6libimportlib\__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "D:Program FilesPython3.6libsite-packagesdjangodbackendsmysqlase.py", line 38, in <module> raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__) django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.8.1.
4、接着再修改 Python3.6Libsite-packagesdjangodbackendsmysqlase.py
将以下两行就行注释
#注释掉 base.py 中如下部分(35/36行) if version < (1, 3, 3): raise ImproperlyConfigured("mysqlclient 1.3.3 or newer is required; you have %s" % Database.__version__)
再次执行发行创建成功
有的博文,说还有如下报错 ,以及后续的解决办法,但是本人没有遇到,顾未做修改
AttributeError: ‘str’ object has no attribute ‘decode’
#找到operations.py文件(46行,版本不同行数不同哈~自个儿find一下),将decode改为encode #linux vim 查找快捷键:?decode if query is not None: query = query.decode(errors='replace') return query #改为 if query is not None: query = query.encode(errors='replace') return query ———————————————— 原文链接:https://blog.csdn.net/weixin_33127753/java/article/details/89100552
解决原理
Django连接MySQL时默认使用MySQLdb驱动,但MySQLdb不支持Python3,因此这里将MySQL驱动设置为pymysql