Python I am trying to use alembic in python flask and it is not working

  • Thread starter Thread starter rgtr
  • Start date Start date
  • Tags Tags
    flask Python
AI Thread Summary
The discussion centers around issues encountered while using Alembic with Flask-Migrate in a local Python Flask application. Users report errors related to missing extensions and installation problems, particularly with package names being case-sensitive. A key solution identified is the need to add `migrate = Migrate(app, db)` in the `__init__.py` file to resolve the KeyError related to 'migrate'. There is also a mention of discrepancies between different tutorials and official documentation, highlighting that they present alternative methods for achieving the same functionality. Overall, the conversation emphasizes troubleshooting installation and configuration issues in a Flask environment.
rgtr
Messages
90
Reaction score
8
TL;DR Summary
I am trying to use alembic in flask. I am following Miguel Grinberg's tutorial but I am getting an error. More details below
https://blog.miguelgrinberg.com/post/how-to-add-flask-migrate-to-an-existing-project

I am using Visual Studio Code and have a database file already created called test.db. My environment variables for test.db name is SQLALCHEMY_DATABASE_URI and the value is sqlite:///test.db In the actual code to the access the environment variable I use SQLALCHEMY_DATABASE_URI = os.environ['SQLALCHEMY_DATABASE_URI']

This website is only running on my local machine if that makes a difference. When I type
pip install flask-migrate flask db migrate.

Here is the current error



The link below shows the previous error. The odd part is that the error below was showing up I already have Flask-Login installed. I also got a similar error with Flask-Mail and Flask-WTF but I already pip installed them and the code was working. So IOW I had to pip install some things twice even though the code was working to avoid errors while running in the terminal pip install flask-migrate and flask db migrate.



Also does my current database have to be up to date?How do I fix this?

Just one thing I need to add I am using a setup similar to this.

https://github.com/CoreyMSchafer/code_snippets/tree/master/Python/Flask_Blog/11-Blueprints

Thanks
 
Technology news on Phys.org
Maybe the order of module install is important, or try rebooting your machine to see if that sorts things out once you start your app again.

Otherwise you might need to uninstall both and install in reverse order alembic then flask or flask then alembic.

Personally, I tend to use anaconda over pip as it bring a lot of preinstalled modules with it at the ost of a larger disk footprint. I've not used alembic before only flask and without using the flask-login feature.
 
  • Like
Likes bikashdaga
Please post Python code using the </> button so we can read it more easily.
 
From the error message it seems that you are trying to access
Python:
current_app.extensions.migrate.migrate
. Is this correct?
 
Correct.
Sorry the reason I didn't post the python code in code blocks because it was so little code and just error messages.

Also I restarted my machine many times and it didn't make a difference
 
Last edited:
The instructions say you should use
Bash:
pip install Flask-Migrate
Why are you using
Bash:
pip install flask-migrate
? Note that Python is case sensitive but Windows is not.

also have you done
Bash:
flask db init
and
Bash:
flask db migrate -m "Initial migration."
?
 
  • Like
Likes jim mcnamara
Sorry this was a careIess question on my part. I tried exactly what you are saying and am getting this error during flask db init.

[CODE title="error message"]Traceback (most recent call last):
File "C:\Users\nmyle\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\nmyle\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "C:\Users\nmyle\AppData\Local\Programs\Python\Python310\Scripts\flask.exe\__main__.py", line 7, in <module>
File "C:\Users\nmyle\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\cli.py", line 995, in main
cli.main(args=sys.argv[1:])
File "C:\Users\nmyle\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\cli.py", line 601, in main
return super().main(*args, **kwargs)
File "C:\Users\nmyle\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 1053, in main
rv = self.invoke(ctx)
File "C:\Users\nmyle\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 1659, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "C:\Users\nmyle\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 1659, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "C:\Users\nmyle\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 1395, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "C:\Users\nmyle\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 754, in invoke
return __callback(*args, **kwargs)
File "C:\Users\nmyle\AppData\Local\Programs\Python\Python310\lib\site-packages\click\decorators.py", line 26, in new_func
return f(get_current_context(), *args, **kwargs)
File "C:\Users\nmyle\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\cli.py", line 445, in decorator
return __ctx.invoke(f, *args, **kwargs)
File "C:\Users\nmyle\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 754, in invoke
return __callback(*args, **kwargs)
File "C:\Users\nmyle\AppData\Local\Programs\Python\Python310\lib\site-packages\flask_migrate\cli.py", line 45, in init
_init(directory, multidb, template, package)
File "C:\Users\nmyle\AppData\Local\Programs\Python\Python310\lib\site-packages\flask_migrate\__init__.py", line 98, in wrapped
f(*args, **kwargs)
File "C:\Users\nmyle\AppData\Local\Programs\Python\Python310\lib\site-packages\flask_migrate\__init__.py", line 122, in init
directory = current_app.extensions['migrate'].directory
KeyError: 'migrate'[/CODE]
 
I'm afraid I have found working with pip on Windows creates some problems which are very hard to pin down. My solution has been to only do fairly trivial stuff with Python on Windows using Jupyter notebooks. Anything more serious I do on Linux, either as a native OS or in a VM - and I particularly wouldn't want to do anything persistent like run a flask server on Windows.

So I only have two suggestions to offer:
Keep trying random combinations of installing and uninstalling packages and hope something works.
Install VirtualBox and create a Linux VM - I have had most success with Linux Mint XFCE edition.
 
I noticed in your errors that you’re using python 3.10? If so then maybe drop back to 3.6 or something as the packages you want may not support it yet.
 
  • #10
Keyerror: Migrate
Seems to be this:
add
https://lifesaver.codes/answer/flask-db-init-fails-keyerror-migrate-196

Solution:​

Adding migrate = Migrate(app, db) into __init__.py resolved the issue. (I didn't realize this was necessary for the cli functionality and database initialization, assumed it was related only to database migrations afterwards.)
 
  • Like
Likes rgtr
  • #11
Thanks I will try this.
 
  • #12
willem2 said:
Keyerror: Migrate
Seems to be this:
add
https://lifesaver.codes/answer/flask-db-init-fails-keyerror-migrate-196

Solution:​

Adding migrate = Migrate(app, db) into __init__.py resolved the issue. (I didn't realize this was necessary for the cli functionality and database initialization, assumed it was related only to database migrations afterwards.)
Why are the instructions different between the official flask documentations and Miguel's tutorial? Is it because of the age of the tutorial?

https://flask-migrate.readthedocs.io/en/latest/

https://blog.miguelgrinberg.com/post/how-to-add-flask-migrate-to-an-existing-project
 
  • #13
rgtr said:
Why are the instructions different between the official flask documentations and Miguel's tutorial? Is it because of the age of the tutorial?
No, its because they use two different alternatives of achieving the same result.

https://flask-migrate.readthedocs.io/en/latest/
Python:
migrate = Migrate(app, db)

https://blog.miguelgrinberg.com/post/how-to-add-flask-migrate-to-an-existing-project
Python:
migrate = Migrate()
migrate.init_app(app, db)

Actually the Grinberg API is also shown in the docs at
https://flask-migrate.readthedocs.io/en/latest/#command-reference
Python:
migrate = Migrate()
migrate.init_app(app, db)
 
  • #14
pbuk said:
No, its because they use two different alternatives of achieving the same result.

https://flask-migrate.readthedocs.io/en/latest/
Python:
migrate = Migrate(app, db)

https://blog.miguelgrinberg.com/post/how-to-add-flask-migrate-to-an-existing-project
Python:
migrate = Migrate()
migrate.init_app(app, db)

Actually the Grinberg API is also shown in the docs at
https://flask-migrate.readthedocs.io/en/latest/#command-reference
Python:
migrate = Migrate()
migrate.init_app(app, db)
Sorry I meant
Code:
flask db init

flask db migrate -m "Initial migration."

flask db upgrade


vs Miguel's ...
 
Back
Top