AttributeError:模块'time'没有属性'clock'在SQLAlchemy python 3.8.2

时间:2020-06-17 19:19:27

标签: python flask flask-sqlalchemy

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Anirudh\Documents\flask_app\connecting_to_database\application.py", line 2, in <module>
    from flask_sqlalchemy import SQLAlchemy
  File "C:\Users\Anirudh\AppData\Local\Programs\Python\Python38\lib\site-packages\flask_sqlalchemy\__init__.py", line 18, in <module>
    import sqlalchemy
  File "C:\Users\Anirudh\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\__init__.py", line 9, in <module>
    from .sql import (
  File "C:\Users\Anirudh\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\sql\__init__.py", line 8, in <module>
    from .expression import (
  File "C:\Users\Anirudh\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\sql\expression.py", line 34, in <module>
    from .visitors import Visitable
  File "C:\Users\Anirudh\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\sql\visitors.py", line 28, in <module>
    from .. import util
  File "C:\Users\Anirudh\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\util\__init__.py", line 8, in <module>
    from .compat import callable, cmp, reduce,  \
  File "C:\Users\Anirudh\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\util\compat.py", line 234, in <module>
    time_func = time.clock
AttributeError: module 'time' has no attribute 'clock'

3 个答案:

答案 0 :(得分:3)

我找到了一个对我有用的解决方案 我有一个名为 env 的虚拟环境地毯,我在其中安装了 sqlalchemy 所以,env\Lib\site-packages\flask_sqlalchemy_init_.py 里面有这个代码:

   if sys.platform == 'win32':
        _timer = time.clock
   else:
        _timer = time.time

我把时钟改成了 perf_counter() _timer = time.perf_counter()

答案 1 :(得分:2)

我在SQLAlchemy 1.2.10中遇到了这个问题。升级到当前版本(截至目前为1.3.18)已解决了该问题

pip install sqlalchemy --upgrade

答案 2 :(得分:1)

发生错误是因为在python 2中有<ws:Related_Person> <ws:Operation>NONE</ws:Operation> <ws:Related_Person_ID>4f6ba72215184387bfa98781d2148d2d</ws:Related_Person_ID> <ws:Related_Person_Type>Dependent</ws:Related_Person_Type> <ws:Relationship>620.6</ws:Relationship> <ws:Name_Data> <ws:Name_Type>Legal</ws:Name_Type> <ws:First_Name>Sebastian</ws:First_Name> <ws:Last_Name>Santiago</ws:Last_Name> <ws:Formatted_Name>Sebastian Santiago</ws:Formatted_Name> <ws:Reporting_Name>Santiago, Sebastian</ws:Reporting_Name> </ws:Name_Data> <ws:Gender>Male</ws:Gender> <ws:Birth_Date>1991-12-19</ws:Birth_Date> <ws:Tobacco_Use>false</ws:Tobacco_Use> <ws:Full_Time_Student>false</ws:Full_Time_Student> <ws:Disabled>false</ws:Disabled> </ws:Related_Person> <ws:Related_Person> <ws:Operation>NONE</ws:Operation> <ws:Related_Person_ID>ab86773bec50494bbbbff5066bbee0c2</ws:Related_Person_ID> <ws:Related_Person_Type>Dependent</ws:Related_Person_Type> <ws:Relationship>620.6</ws:Relationship> <ws:Name_Data> <ws:Name_Type>Legal</ws:Name_Type> <ws:First_Name>Joachin</ws:First_Name> <ws:Last_Name>Santiago</ws:Last_Name> <ws:Formatted_Name>Joachin Santiago</ws:Formatted_Name> <ws:Reporting_Name>Santiago, Joachin</ws:Reporting_Name> </ws:Name_Data> <ws:Gender>Male</ws:Gender> <ws:Birth_Date>1985-08-08</ws:Birth_Date> <ws:Tobacco_Use>false</ws:Tobacco_Use> <ws:Full_Time_Student>false</ws:Full_Time_Student> <ws:Disabled>false</ws:Disabled> </ws:Related_Person> <ws:Related_Person> <ws:Operation>NONE</ws:Operation> <ws:Related_Person_ID>c4aa5ebf332c48b2bd3648392168a61d</ws:Related_Person_ID> <ws:Related_Person_Type>Dependent</ws:Related_Person_Type> <ws:Relationship>620.3</ws:Relationship> <ws:Name_Data> <ws:Name_Type>Legal</ws:Name_Type> <ws:First_Name>Carmen</ws:First_Name> <ws:Middle_Name>Miranda</ws:Middle_Name> <ws:Last_Name>Santiago</ws:Last_Name> <ws:Formatted_Name>Carmen Santiago</ws:Formatted_Name> <ws:Reporting_Name>Santiago, Carmen Miranda</ws:Reporting_Name> </ws:Name_Data> <ws:Gender>Female</ws:Gender> <ws:Birth_Date>1964-06-22</ws:Birth_Date> <ws:Tobacco_Use>false</ws:Tobacco_Use> <ws:Full_Time_Student>false</ws:Full_Time_Student> <ws:Disabled>false</ws:Disabled> </ws:Related_Person> ,但是在python 3中已将其替换为time.clock()

只需将所有time.perf_counter()替换为time.clock,就可以了。有关更多信息:https://www.webucator.com/blog/2015/08/python-clocks-explained/