首页 > 编程语言 >Mac-pycharm执行python3 manage.py makemigrations报错处理

Mac-pycharm执行python3 manage.py makemigrations报错处理

时间:2023-02-05 15:55:39浏览次数:58  
标签:makemigrations Frameworks py framework 报错 File line 3.8

Mac-pycharm执行python3 manage.py makemigrations报错处理

1 报错:NameError: name '_mysql' is not defined

参考:https://blog.csdn.net/weixin_35757704/article/details/120929396

在django的setting.py同一目录下的__init__.py中添加:

import pymysql
pymysql.version_info = (1, 4, 0, "final", 0) # 这里指定版本
pymysql.install_as_MySQLdb()  # 使用pymysql代替mysqldb连接数据库

附:完整报错

frantichow@FrantiChowdeMac-mini djangoProject01 % python3 manage.py makemigrations
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/MySQLdb/__init__.py", line 18, in <module>
    from . import _mysql
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/MySQLdb/_mysql.cpython-38-darwin.so, 0x0002): symbol not found in flat namespace '_mysql_affected_rows'

During handling of the above exception, another exception occurred:

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 "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/management/__init__.py", line 357, in execute
    django.setup()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/apps/config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/contrib/auth/models.py", line 2, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
    class AbstractBaseUser(models.Model):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/base.py", line 117, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/base.py", line 321, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/options.py", line 204, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/__init__.py", line 28, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/utils.py", line 201, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/utils.py", line 110, in load_backend
    return import_module('%s.base' % backend_name)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/backends/mysql/base.py", line 15, in <module>
    import MySQLdb as Database
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/MySQLdb/__init__.py", line 24, in <module>
    version_info, _mysql.version_info, _mysql.__file__
NameError: name '_mysql' is not defined

2 报错:AttributeError: 'str' object has no attribute 'decode'

解决:修改/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/backends/mysql/operations.py的第146行源码

        if query is not None:
            #query = query.decode(errors='replace')
            query = query.encode(errors='replace')

附:完整报错

frantichow@FrantiChowdeMac-mini djangoProject01 % python3 manage.py makemigrations
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 "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/management/commands/makemigrations.py", line 101, in handle
    loader.check_consistent_history(connection)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/migrations/loader.py", line 283, in check_consistent_history
    applied = recorder.applied_migrations()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 73, in applied_migrations
    if self.has_table():
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 56, in has_table
    return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/backends/base/base.py", line 256, in cursor
    return self._cursor()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/backends/base/base.py", line 233, in _cursor
    self.ensure_connection()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/backends/base/base.py", line 217, in ensure_connection
    self.connect()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/backends/base/base.py", line 197, in connect
    self.init_connection_state()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/backends/mysql/base.py", line 231, in init_connection_state
    if self.features.is_sql_auto_is_null_enabled:
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/utils/functional.py", line 80, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/backends/mysql/features.py", line 82, in is_sql_auto_is_null_enabled
    cursor.execute('SELECT @@SQL_AUTO_IS_NULL')
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/backends/utils.py", line 103, in execute
    sql = self.db.ops.last_executed_query(self.cursor, sql, params)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/backends/mysql/operations.py", line 146, in last_executed_query
    query = query.decode(errors='replace')
AttributeError: 'str' object has no attribute 'decode'
frantichow@FrantiChowdeMac-mini djangoProject01 % 

标签:makemigrations,Frameworks,py,framework,报错,File,line,3.8
From: https://www.cnblogs.com/frantichow/p/17093479.html

相关文章

  • Python黑客编程之tcp代理
    目的写一款socket代理工具,其实就是在原来两极通信中再加一极,中间极用来转发socket的流量,可以在中间层面实现流量的拦截和篡改代码代理importsocketimportsysim......
  • python collection Chainmap Counter
    chainmap是一个方便的工具类。它是使用链的方式将多个dict链在一起,并不是真正的生成一个新的dict,从而允许程序可以这获取任意一个dict所包含的所有key对应的value。但是......
  • 整理我遇到的 Python 的疑难问题
    1如果字典里一个键指向一个实例,深拷贝会拷贝出一个新的实例吗?不会:classfoo:def__init__(self):print('doinitfoo')a={'cls':foo()}a#......
  • 容器报错docker: Error response from daemon: driver failed programming external c
    在启动容器时的容器时,会出现报错:Errorresponsefromdaemon:driverfailedprogrammingexternalconnectivityonendpointXXX(端口映射或启动容器时报错)  原因:在......
  • python 3.python入门
    1.编程语言介绍1.1分类机器语言#机器语言用二进制代码0和1描述的指令称为机器指令,由于计算机内部是基于二进制指令工作的,所以机器语言是直接控制计算......
  • python Listbox插入数据导致GUI崩溃
    提问: 我用windnd模块做了个文件拖入获取路径的功能,想把获取到的路径插入到列表,但是GUI会崩溃,下面是原代码求帮忙看看咋回事importwindndimportthreadingimporttk......
  • pycharm个性化设置操作演示
    2023-02-0512:34:07星期日设置需求:PyCharm显示当前python文件下的函数和类的列表教程来袭请看如下图与项目目录平行位置>>>找到齿轮>>>鼠标左键>>>给showmembers......
  • python内置函数len()
    len()len()函数用于返回对象(字符串、字节、元组、列表等)的长度(元素个数)len()函数的语法:len(s)代码示例print(len(range(10)))print(len(["1","2","3"]))print(len......
  • python内置函数range()
    range()函数介绍range()函数实际上表示一个不可变的数字序列类型,通常用于在for循环中指定特定的次数。range()的格式:classrange(stop)classrange(start,stop[,ste......
  • Python黑客编程之类nc工具
    目的用pythonsocket编写一款类似NetCat的工具,可以在服务器上远程执行命令,从服务器上下载文件代码服务端和客户端用同一套代码,用-l参数进行区分importargparsei......