1

While following a Flask tutorial I stumbled around this very strange problem! While setting up the Migrate directory , after the first step of setting the flask app using set FLASK_APP=sql1.py, when I ran this command flask db init I got this error:

(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'

I'm getting the exact same error while running my .py script also,here's the script:

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!"

I looked into every solution I found (here and this)and made sure everything was correct! I ran this command when my environment was activated which had installed every package needed including SQLAlchemy and Flask-Migrate.

I even deleted them (packages) and re-installed their latest versions but still getting the same error! I'm using Python 3.8.5

Kashi
  • 99
  • 2
  • 11

3 Answers3

2

You have some dependencies that are too old and incompatible with Python 3.8. At the very least, you should update the mako package:

pip install --upgrade mako
Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152
1

I had a similar issue I had to update flask_sqlalchemy even after doing this I had issues. I ended up having to make a new virtual environment, I would make a small hello world project on another virtual environment to see if that works.

apexprogramming
  • 403
  • 2
  • 14
0

For me, it was giving the same error, so I removed __init__.py file from the folder which has app.py

After removing I ran the

flask init db

it worked :)