分类: python3

AttributeError: module 'time' has no attribute 'clock'错误解决方案1

AttributeError: module 'time' has no attribute 'clock'错误解决方案

运行python程序之后出现如下错误提示: python manage.py shell Traceback (most recent call last): File "manage.py", line 5, in <module> from flask_migrate import Migrate, MigrateCommand File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\flask_migrate\__init__.py", line 5, in <module> from alembic import __version__ as __alembic_version__ File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\alembic\__init__.py", line 8, in <module> from . import op # noqa File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\alembic\op.py", line 1, in <module> from .operations.base import Operations File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\alembic\operations\__init__.py", line 1, in <module> from .base import Operations, BatchOperations File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\alembic\operations\base.py", line 3, in <module> from .. import util File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\alembic\util\__init__.py", line 6, in <module> from .pyfiles import ( # noqa File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\alembic\util\pyfiles.py", line 5, in <module> from mako.template import Template File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\mako\template.py", line 10, in <module> from mako.lexer import Lexer File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\mako\lexer.py", line 11, in <module> from mako import parsetree, exceptions, compat File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\mako\parsetree.py", line 9, in <module> from mako import exceptions, ast, util, filters, compat File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\mako\exceptions.py", line 11, in <module> from mako import util, compat File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\mako\util.py", line 11, in <module> from mako import compat File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\mako\compat.py", line 124, in <module> time_func = time.clock AttributeError: module 'time' has no attribute 'clock'

原因 Python time.clock在Python3.3废弃,在Python3.8中将被移除 解决方法 打开Python3.8中的compat.py文件,找到if win32 or jython:这个位置 把time_func = time.clock这句话注释掉换成time_func = time.perf_counter使用perf_counter()方法 就ok了

修改代码如下:

if win32 or jython:
    # time_func = time.clock
    time_func = time.perf_counter

请写文章内容