0

I cannot seem to be able to replicate any minimal example on the flask documentation page when the web app is a subpackage

In [8]: from subpkg.apps.myapp import run_me

In [9]: run_me.main()
 * Running on http://localhost:5050/ (Press CTRL+C to quit)
127.0.0.1 - - [10/Oct/2018 22:38:09] "GET / HTTP/1.1" 404 -

Folder structure is part of a larger python library. For example the only .py files containing code are listed below

pkg/
-- setup.py
-- subpkg/
-- __init__.py
-- -- apps/
-- -- __init__.py
-- -- -- myapp/
-- -- -- -- __init__.py
-- -- -- -- run_me.py
-- -- -- -- web/
-- -- -- -- -- views.py
-- -- -- -- -- __init__.py

Create the example

mkdir pkg; mkdir pkg/subpkg/; mkdir pkg/subpkg/apps; mkdir pkg/subpkg/apps/myapp; mkdir pkg/subpkg/apps/myapp/web
touch pkg/__init__.py; touch pkg/subpkg/__init__.py; touch pkg/subpkg/apps/__init__.py; touch pkg/subpkg/apps/myapp/__init__.py; touch pkg/subpkg/apps/myapp/web/__init__.py; touch pkg/setup.py; touch touch pkg/subpkg/apps/myapp/web/views.py; touch pkg/subpkg/apps/myapp/run_me.py

pkg/setup.py

from setuptools import find_packages, setup
setup(
    name='pkg-myapp',
    packages=find_packages(),
    include_package_data=True,
    install_requires=['flask==0.11.1'],
    entry_points={'console_scripts': ['pkg-myapp=pkg.apps.myapp.run_me:main',],},
)

pkg/subpkg/apps/myapp/run_me.py

from .web import app
def main():
    app.run('localhost', 5050)

pkg/subpkg/apps/myapp/web/views.py

from . import app
@app.route("/")
def index():
    return 'hello world'

pkg/subpkg/apps/myapp/web/__init__.py

from flask import Flask
app = Flask('subpkg.apps.myapp.web')

Refs

Alexander McFarlane
  • 10,643
  • 9
  • 59
  • 100
  • Did you run the `pkg/subpkg/apps/myapp/web/views.py`? I believe you never import this file so the code is never executed, right? – Sraw Oct 10 '18 at 21:46
  • I'll just double check my minimal example. I may have just forgotten the import. I'll respond in a moment but I'll be mad if it's that trivial... – Alexander McFarlane Oct 10 '18 at 21:48
  • @Sraw I can confirm I hate myself - it was as simple as adding one line `from . import views` ... not sure it there is much point in this thread remaining – Alexander McFarlane Oct 10 '18 at 22:02
  • 1
    Hum... At least you solve it now :) – Sraw Oct 10 '18 at 22:03

0 Answers0