OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

"AttributeError: module 'myApp.app' has no attribute 'run'" when trying to run a Flask application with uWSGI

  • Thread starter Thread starter martin1959
  • Start date Start date
M

martin1959

Guest
I have a Flask app utilizing Blueprints which runs in a virtual environment (venv).

The app has a structure

Code:
myApp
├── app
│   ├── about
│   │   ├── __init__.py
│   │   └── routes.py
│   ├── extensions.py
│   ├── __init__.py
│   ├── main
│   │   ├── __init__.py
│   │   └── routes.py
│   ├── models
│   │   ├── contact.py
│   ├── static
│   │   ├── myApp.css
│   │   ├── favicon-16x16.png
│   └── templates
│       ├── about
│       │   └── index.html
│       ├── base.html
│       └── index.html
├── bin
│   ├── activate
│   ├── flask
│   ├── pip
│   ├── python -> python3.12
│   ├── python3 -> python3.12
│   ├── python3.12 -> /usr/bin/python3.12
│   ├── uwsgi
│   └── wheel
├── config.py
├── myApp.db
├── myApp -> /home/janeDoe/myApp
├── myApp.ini
├── include
│   └── ...
├── lib
│   └── ...
├── lib64 -> lib
├── pyvenv.cfg
├── requirements.txt
└── wsgi.py

If I execute

Code:
cd /home/janeDoe/myApp
flask run

the page renders fine.

If I try to run with uwsgi as in

Code:
cd /home/janeDoe/myApp
uwsgi --socket 0.0.0.0:5000 --protocol=http -w wsgi:app

where

Code:
wsgi.py:

from myApp import app
app.run()

and

Code:
app/__init__.py:

from flask import Flask
from config import Config
from app.extensions import db
from flask_favicon import FlaskFavicon

def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)

    # Initialize Flask extensions here
    db.init_app(app)

    # Register blueprints here
    from app.main import bp as main_bp

    app.register_blueprint(main_bp)

    from app.about import bp as about_bp

    app.register_blueprint(about_bp, url_prefix="/about")

    flaskFavicon = FlaskFavicon()
    flaskFavicon.init_app(app)
    flaskFavicon.register_favicon("app/static/favicon-16x16.png", "default")

    @app.route("/test/")
    def test_page():
        return "<h1>Testing the Flask Application Factory Pattern</h1>"

    return app

I get

Code:
...

mapped 72920 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
Traceback (most recent call last):
  File "/home/JaneDoe/myApp/wsgi.py", line 5, in <module>
    app.run()
    ^^^^^^^
AttributeError: module 'myApp.app' has no attribute 'run'
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 1321333, cores: 1)

I would have thought app.run() would be imported from flask in app/__init__.py when create_app is invoked. What do I need to change to have app.run() recognized?
<p>I have a Flask app utilizing Blueprints which runs in a virtual environment (venv).</p>
<p>The app has a structure</p>
<pre><code>myApp
├── app
│ ├── about
│ │ ├── __init__.py
│ │ └── routes.py
│ ├── extensions.py
│ ├── __init__.py
│ ├── main
│ │ ├── __init__.py
│ │ └── routes.py
│ ├── models
│ │ ├── contact.py
│ ├── static
│ │ ├── myApp.css
│ │ ├── favicon-16x16.png
│ └── templates
│ ├── about
│ │ └── index.html
│ ├── base.html
│ └── index.html
├── bin
│ ├── activate
│ ├── flask
│ ├── pip
│ ├── python -> python3.12
│ ├── python3 -> python3.12
│ ├── python3.12 -> /usr/bin/python3.12
│ ├── uwsgi
│ └── wheel
├── config.py
├── myApp.db
├── myApp -> /home/janeDoe/myApp
├── myApp.ini
├── include
│ └── ...
├── lib
│ └── ...
├── lib64 -> lib
├── pyvenv.cfg
├── requirements.txt
└── wsgi.py
</code></pre>
<p>If I execute</p>
<pre><code>cd /home/janeDoe/myApp
flask run
</code></pre>
<p>the page renders fine.</p>
<p>If I try to run with <code>uwsgi</code> as in</p>
<pre><code>cd /home/janeDoe/myApp
uwsgi --socket 0.0.0.0:5000 --protocol=http -w wsgi:app
</code></pre>
<p>where</p>
<pre><code>wsgi.py:

from myApp import app
app.run()
</code></pre>
<p>and</p>
<pre><code>app/__init__.py:

from flask import Flask
from config import Config
from app.extensions import db
from flask_favicon import FlaskFavicon

def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(config_class)

# Initialize Flask extensions here
db.init_app(app)

# Register blueprints here
from app.main import bp as main_bp

app.register_blueprint(main_bp)

from app.about import bp as about_bp

app.register_blueprint(about_bp, url_prefix="/about")

flaskFavicon = FlaskFavicon()
flaskFavicon.init_app(app)
flaskFavicon.register_favicon("app/static/favicon-16x16.png", "default")

@app.route("/test/")
def test_page():
return "<h1>Testing the Flask Application Factory Pattern</h1>"

return app
</code></pre>
<p>I get</p>
<pre><code>...

mapped 72920 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
Traceback (most recent call last):
File "/home/JaneDoe/myApp/wsgi.py", line 5, in <module>
app.run()
^^^^^^^
AttributeError: module 'myApp.app' has no attribute 'run'
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 1321333, cores: 1)
</code></pre>
<p>I would have thought <code>app.run()</code> would be imported from <code>flask</code> in <code>app/__init__.py</code> when <code>create_app</code> is invoked. What do I need to change to have <code>app.run()</code> recognized?</p>
 
Top