Flask(flask db init):AttributeError:模块“时间”没有属性“时钟”

时间:2020-09-15 10:53:36

标签: python python-3.x flask flask-sqlalchemy flask-migrate

在阅读了Flask教程之后,我偶然发现了这个非常奇怪的问题!设置迁移目录时,在使用set FLASK_APP=sql1.py 设置flask应用的第一步之后,当我运行此命令flask db init时,我得到了错误:

(first_flask_env) C:\Users\aakash\Desktop\python programs>flask db init
Traceback (most recent call last):
  File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\aakash\Anaconda3\envs\first_flask_env\Scripts\flask.exe\__main__.py", line 7, in <module>
  File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\flask\cli.py", line 894, in main
    cli.main(args=args, prog_name=name)
  File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\flask\cli.py", line 557, in main
    return super(FlaskGroup, self).main(*args, **kwargs)
  File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\click\core.py", line 697, in main
    rv = self.invoke(ctx)
  File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\click\core.py", line 1061, in invoke
    cmd_name, cmd, args = self.resolve_command(ctx, args)
  File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\click\core.py", line 1100, in resolve_command
    cmd = self.get_command(ctx, cmd_name)
  File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\flask\cli.py", line 500, in get_command
    self._load_plugin_commands()
  File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\flask\cli.py", line 496, in _load_plugin_commands
    self.add_command(ep.load(), ep.name)
  File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\pkg_resources\__init__.py", line 2472, in load
    return self.resolve()
  File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\pkg_resources\__init__.py", line 2478, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\flask_migrate\__init__.py", line 8, in <module>
    from alembic import __version__ as __alembic_version__
  File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\alembic\__init__.py", line 8, in <module>
    from . import op  # noqa
  File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\alembic\op.py", line 1, in <module>
    from .operations.base import Operations
  File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\alembic\operations\__init__.py", line 1, in <module>
    from .base import Operations, BatchOperations
  File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\alembic\operations\base.py", line 3, in <module>
    from .. import util
  File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\alembic\util\__init__.py", line 6, in <module>
    from .pyfiles import (  # noqa
  File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\alembic\util\pyfiles.py", line 6, in <module>
    from mako.template import Template
  File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\mako\template.py", line 10, in <module>
    from mako.lexer import Lexer
  File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\mako\lexer.py", line 11, in <module>
    from mako import parsetree, exceptions, compat
  File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\mako\parsetree.py", line 9, in <module>
    from mako import exceptions, ast, util, filters, compat
  File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\mako\exceptions.py", line 11, in <module>
    from mako import util, compat
  File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\mako\util.py", line 11, in <module>
    from mako import compat
  File "c:\users\aakash\anaconda3\envs\first_flask_env\lib\site-packages\mako\compat.py", line 124, in <module>
    time_func = time.clock
AttributeError: module 'time' has no attribute 'clock'

同时运行.py脚本时我也得到了完全相同的错误,这是脚本:

from flask import Flask
import os
from flask_sqlalchemy import SQLAlchemy 
from flask_migrate import Migrate

basedir = os.path.abspath(os.path.dirname(__file__))   #Full directory path of the file I'm working with..here, sql1.py

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'data.sqlite')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False 
db = SQLAlchemy(app)

Migrate(app,db) #Here it connect the application "app.py"
                # with the database "db"    
class puppy(db.Model):
    __tablename__ = 'Name Provided by me!!'
    id = db.Column(db.Integer,primary_key = True)
    name = db.Column(db.Text)
    age = db.Column(db.Integer)
    
    def __init__(self,name,age):
        self.name = name
        self.age = age
    def __repr__(self):
        return f"puppy {self.name} is {self.age} year/s old!"

我调查了找到的每个解决方案(herethis),并确保一切正确!在激活 我的环境 时,我运行了此命令,该环境安装了所需的每个软件包,包括 SQLAlchemy和Flask-Migrate

我什至删除了它们(软件包),并且 重新安装了它们的最新版本 ,但仍然收到相同的错误! 我正在使用 Python 3.8.5

3 个答案:

答案 0 :(得分:1)

您所拥有的某些依赖项太旧且与Python 3.8不兼容。至少,您应该更新mako软件包:

pip install --upgrade mako

答案 1 :(得分:0)

我遇到了类似的问题,即使这样做也不得不更新flask_sqlalchemy。我最终不得不创建一个新的虚拟环境,我将在另一个虚拟环境上创建一个小的hello world项目,以查看是否可行。

答案 2 :(得分:0)

对我来说,它给出了同样的错误,所以我从包含 __init__.py

的文件夹中删除了 app.py 文件

删除后我运行了

flask init db

它奏效了:)