Search code examples
pythonpython-3.xflaskwerkzeug

Troubleshooting Flask error werkzeug routing


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
$ venv\Scripts\activate
(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:\Python38\lib\runpy.py", line 192, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Python38\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\microblog\venv\Scripts\flask.exe\__main__.py", line 9, in <module>
  File "c:\microblog\venv\lib\site-packages\flask\cli.py", line 894, in main
    cli.main(args=args, prog_name=name)
  File "c:\microblog\venv\lib\site-packages\flask\cli.py", line 557, in main
    return super(FlaskGroup, self).main(*args, **kwargs)
  File "c:\microblog\venv\lib\site-packages\click\core.py", line 717, in main
    rv = self.invoke(ctx)
  File "c:\microblog\venv\lib\site-packages\click\core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "c:\microblog\venv\lib\site-packages\click\core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "c:\microblog\venv\lib\site-packages\click\core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "c:\microblog\venv\lib\site-packages\click\decorators.py", line 64, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "c:\microblog\venv\lib\site-packages\click\core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "c:\microblog\venv\lib\site-packages\flask\cli.py", line 767, in run_command
    app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
  File "c:\microblog\venv\lib\site-packages\flask\cli.py", line 293, in __init__
    self._load_unlocked()
  File "c:\microblog\venv\lib\site-packages\flask\cli.py", line 317, in _load_unlocked
    self._app = rv = self.loader()
  File "c:\microblog\venv\lib\site-packages\flask\cli.py", line 372, in load_app
    app = locate_app(self, import_name, name)
  File "c:\microblog\venv\lib\site-packages\flask\cli.py", line 235, in locate_app
    __import__(module_name)
  File "c:\microblog\microblog.py", line 1, in <module>
    from app import app
  File "c:\microblog\app\__init__.py", line 5, in <module>
    app = Flask(__name__) #The name "app" is being passed
  File "c:\microblog\venv\lib\site-packages\flask\app.py", line 558, in __init__
    self.add_url_rule(
  File "c:\microblog\venv\lib\site-packages\flask\app.py", line 66, in wrapper_func
    return f(self, *args, **kwargs)
  File "c:\microblog\venv\lib\site-packages\flask\app.py", line 1216, in add_url_rule
    self.url_map.add(rule)
  File "c:\microblog\venv\lib\site-packages\werkzeug\routing.py", line 1562, in add
    rule.bind(self)
  File "c:\microblog\venv\lib\site-packages\werkzeug\routing.py", line 711, in bind
    self.compile()
  File "c:\microblog\venv\lib\site-packages\werkzeug\routing.py", line 767, in compile
    self._build = self._compile_builder(False)
  File "c:\microblog\venv\lib\site-packages\werkzeug\routing.py", line 1128, in _compile_builder
    return self.BuilderCompiler(self).compile(append_unknown)
  File "c:\microblog\venv\lib\site-packages\werkzeug\routing.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 :)!


Solution

  • 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