Troubleshooting Flask error werkzeug routing

Question:

I am following a tutorial to do the simplest “Hello World” Flask app as described in:
https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world

For context, I have created the directory at the root of the C drive to avoid the potential path issues. I am running Python 3.8 and Flask 1.0.2 and werkzeug 0.15.2

Before issuing the flask command I am doing two things. Creating a virtual environment and installing a local flask version

$ python -m venv venv
$ venvScriptsactivate
(venv) $ pip install flask

And also setting the FLASK_APP environment variable

(venv) $ set FLASK_APP=microblog.py

When I issue the “flask run” command, the top level script is run (contents here)

from app import app

Which calls the __init__.py file in the app folder that contains:

from flask import Flask

app = Flask(__name__) #The name "app" is being passed

from app import routes

And the second line fails with the following error output:

Traceback (most recent call last):
  File "C:Python38librunpy.py", line 192, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:Python38librunpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:microblogvenvScriptsflask.exe__main__.py", line 9, in <module>
  File "c:microblogvenvlibsite-packagesflaskcli.py", line 894, in main
    cli.main(args=args, prog_name=name)
  File "c:microblogvenvlibsite-packagesflaskcli.py", line 557, in main
    return super(FlaskGroup, self).main(*args, **kwargs)
  File "c:microblogvenvlibsite-packagesclickcore.py", line 717, in main
    rv = self.invoke(ctx)
  File "c:microblogvenvlibsite-packagesclickcore.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "c:microblogvenvlibsite-packagesclickcore.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "c:microblogvenvlibsite-packagesclickcore.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "c:microblogvenvlibsite-packagesclickdecorators.py", line 64, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "c:microblogvenvlibsite-packagesclickcore.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "c:microblogvenvlibsite-packagesflaskcli.py", line 767, in run_command
    app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
  File "c:microblogvenvlibsite-packagesflaskcli.py", line 293, in __init__
    self._load_unlocked()
  File "c:microblogvenvlibsite-packagesflaskcli.py", line 317, in _load_unlocked
    self._app = rv = self.loader()
  File "c:microblogvenvlibsite-packagesflaskcli.py", line 372, in load_app
    app = locate_app(self, import_name, name)
  File "c:microblogvenvlibsite-packagesflaskcli.py", line 235, in locate_app
    __import__(module_name)
  File "c:microblogmicroblog.py", line 1, in <module>
    from app import app
  File "c:microblogapp__init__.py", line 5, in <module>
    app = Flask(__name__) #The name "app" is being passed
  File "c:microblogvenvlibsite-packagesflaskapp.py", line 558, in __init__
    self.add_url_rule(
  File "c:microblogvenvlibsite-packagesflaskapp.py", line 66, in wrapper_func
    return f(self, *args, **kwargs)
  File "c:microblogvenvlibsite-packagesflaskapp.py", line 1216, in add_url_rule
    self.url_map.add(rule)
  File "c:microblogvenvlibsite-packageswerkzeugrouting.py", line 1562, in add
    rule.bind(self)
  File "c:microblogvenvlibsite-packageswerkzeugrouting.py", line 711, in bind
    self.compile()
  File "c:microblogvenvlibsite-packageswerkzeugrouting.py", line 767, in compile
    self._build = self._compile_builder(False)
  File "c:microblogvenvlibsite-packageswerkzeugrouting.py", line 1128, in _compile_builder
    return self.BuilderCompiler(self).compile(append_unknown)
  File "c:microblogvenvlibsite-packageswerkzeugrouting.py", line 1119, in compile
    co = types.CodeType(*code_args)
TypeError: code() takes at least 14 arguments (13 given)

In addition to the answer below, make sure that you have your PATH variable correctly setup. In my case, I had an Anaconda install and a regular Python install that had conflicting versions. In the end, the application ran through the Anaconda Prompt. To fix this kind of issue, try adding the Anaconda paths in the bash_profile to the PATH variable in windows

Happy Flasking :)!

Asked By: Ernesto

||

Answers:

I got the server up and running. Here’s the project structure I have:

.
├── app
│   ├── __init__.py
│   └── routes.py
├── microblog.py
└── venv

and here are the three source files he mentions in the tutorial

__init.py__ ⬇️

from flask import Flask

app = Flask(__name__)

from app import routes

routes.py ⬇️

from app import app

@app.route('/')
@app.route('/index')
def index():
    return "Hello, World!"

microblog.py ⬇️

from app import app

After setting up the source code, I ran export FLASK_APP=microblog.py and then ran flask run. On flask run, you should see something like this:

enter image description here

Here are the dependency versions on my end:

enter image description here

Answered By: Zach Valenta

It looks like flask (werkzeug module) was not compatible with python3.8 github issue If youre using werkzeug <0.15.5 consider either downgrading to python 3.7 (and pointing the python3 link from the virtual environment to python3.7) OR upgrading your werkzeug module to 0.15.5 or higher.

Its Usually cause of dependencies version

create new virtual-env and install packages as per the version it will fix the error

Answered By: Tajwar Bilal

import name ‘parse_rule’ from ‘werkzeug.routing’

will pop up if using python with version >= 3.8. Try downgrade Werkzeug to 2.0.0

pip install Werkzeug==2.0.0
Answered By: Filipus Samuel

Actually, this problem already become a real issue in the official GitHub repo. Based on the suggestion on Github, the quick workaround is to downgrade Flask to v2.1.2, since the newest version requires Werkzeug >= 2.2.0 and Flask v2.1.2 just requires Werkzeug>=2.0. Your requirments.txt should look like this

Flask==2.1.2
flask-restx==0.5.1
Werkzeug==2.1.2

I already try it out, and it works, and I try it cause I got the same issues. The way you to the downgrade (in any case someone wondering)

# I use pip3, cause I'm on Linux

pip3 install Flask==2.1.2
pip3 install Werkzeug==2.1.2

There’s actually some blog post regarding this issue. Which says you only need to downgrade the Werkzeug, but if you do that, it will just lead to this error


__init__() got an unexpected keyword argument 'unbound_message'

Answered By: Alif Irhas
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.