Unable to import 'url_parse' from 'werkzeug.urls' in flask shell execution
Closed this issue · 15 comments
Section 5.8, attempt to run flask shell results in "ImportError: cannot import name 'url_parse' form 'werkzeug.urls' -- Running Python 3.9 on Raspberry Pi Model 4 with Bullseye
Flask introduced breaking changes recently. The solution is to upgrade Flask, Werkzeug and Flask-Login to their latest releases.
pip install --upgrade flask werkzeug flask-login
I tried this solution and it did not work, nor did the one proposed by Klinge earlier. Anything more to try?
Please provide the stack trace of the error.
Hope this is what you requested:
(venv) pi@Four:~/microblog $ flask shell
Usage: flask shell [OPTIONS]
Try 'flask shell --help' for help.
Error: While importing 'microblog', an ImportError was raised:
Traceback (most recent call last):
File "/home/pi/microblog/venv/lib/python3.9/site-packages/flask/cli.py", line 219, in locate_app
import(module_name)
File "/home/pi/microblog/microblog.py", line 1, in
from app import app, db
File "/home/pi/microblog/app/init.py", line 15, in
from app import routes, models
File "/home/pi/microblog/app/routes.py", line 7, in
from werkzeug import url_parse
ImportError: cannot import name 'url_parse' from 'werkzeug' (/home/pi/microblog/venv/lib/python3.9/site-packages/werkzeug/init.py)
I'm sorry, my bad. I missed one instance that I need to fix.
Temporary workaround is to stay on Flask 2.x. Use this command to downgrade:
pip install "flask<3" "werkzeug<3"
This should allow you to continue with the tutorial until I update this code.
Still having problems after downgrades:
pi@Four:/microblog $ source venv/bin/activate/microblog $ pip list
(venv) pi@Four:
Package Version
alembic 1.12.1
blinker 1.7.0
click 8.1.7
Flask 2.3.3
Flask-Login 0.6.3
Flask-Migrate 4.0.5
Flask-SQLAlchemy 3.1.1
Flask-WTF 1.2.1
greenlet 3.0.1
importlib-metadata 6.8.0
itsdangerous 2.1.2
Jinja2 3.1.2
Mako 1.3.0
MarkupSafe 2.1.3
pip 23.3.1
pkg_resources 0.0.0
python-dotenv 1.0.0
setuptools 44.1.1
SQLAlchemy 2.0.23
typing_extensions 4.8.0
Werkzeug 2.3.8
WTForms 3.1.1
zipp 3.17.0
(venv) pi@Four:~/microblog $ flask shell
Usage: flask shell [OPTIONS]
Try 'flask shell --help' for help.
Error: While importing 'microblog', an ImportError was raised:
Traceback (most recent call last):
File "/home/pi/microblog/venv/lib/python3.9/site-packages/flask/cli.py", line 219, in locate_app
import(module_name)
File "/home/pi/microblog/microblog.py", line 1, in
from app import app, db
File "/home/pi/microblog/app/init.py", line 15, in
from app import routes, models
File "/home/pi/microblog/app/routes.py", line 7, in
from werkzeug import url_parse
ImportError: cannot import name 'url_parse' from 'werkzeug' (/home/pi/microblog/venv/lib/python3.9/site-packages/werkzeug/init.py)
The correct import is:
from werkzeug.urls import url_parse
After correcting my omission and downgrading the packages, I am still having problems. Rather than churn both our schedules, I think it best to wait until you have worked out the bug using the latest Flask/Werkzeug packages. I appreciate you prompt attention and your continuing efforts, and I look foward to continuing with the tutorial in its revised version. Thanks.
Are you having the same problem or something else? The changes that I will need to make for Flask 3 is just to find an alternative to the url_parse
function, which the Flask maintainers decided to delete from the project.
If you are having other problems after you got past this one it might be best to mention what the new problem is, in case it is something else I need to be aware of.
@JRGaddy that is because I'm guessing you missed adding the set_password()
method to the User
class. Do a search for set_password
in Chapter 5 to find all the references to it. You must have missed the part where this method is added.
Closing as this does not apply to the new edition of this tutorial.
As per Werkzeug documentation (https://werkzeug.palletsprojects.com/en/3.0.x/changes/#version-3-0-1):
Deprecate the werkzeug.urls module, except for the uri_to_iri and iri_to_uri functions. Use the urllib.parse library instead. #2600
So this function is no longer there anymore.
Changes:
#from werkzeug.urls import url_parse
from urllib.parse import urlparse
and
if not next_page or urlparse(next_page).netloc != '': #or url_parse(next_page).netloc != '':
seem to be working.