How to run flask app after reorganization as a package - python

I'm trying to restructure a flask app to a package based on http://flask.pocoo.org/docs/0.10/patterns/packages/. My app is based on http://code.tutsplus.com/tutorials/intro-to-flask-signing-in-and-out--net-29982 . I've now changed it to the screenshot above and am trying to run at command line.
runserver.py:
from app import intro_to_flask
if __name__ == '__main__':
intro_to_flask.routes.run(debug=True)
At the command line:
/mini/app (master) $ python runserver.py
Traceback (most recent call last):
File "runserver.py", line 1, in <module>
from app import intro_to_flask
ImportError: No module named app
What am I doing wrong?

Since the Flask documentation mentions only one __init__.py file in the subdirectory with the Flask application and you have two of them, I think you're confused by the __init__.py file and the usage of imports. In your case the subdirectory with the Flask application is intro_to_flask.
Basically it works like this: from <module> import <object>
The module is a .py file and the object is defined inside the module. In this special case there is a __init__.py file so the module you have to reference to has the name of the directory that contains the __init__.py file.
Assuming your app/intro_to_flask/__init__.py looks like this:
from flask import Flask
app = Flask(__name__)
import intro_to_flask.routes
Your app/runserver.py should look like this:
from intro_to_flask import app
app.run(debug=True)
intro_to_flask is the imported module, app is a public object defined inside the imported module.

I believe you're looking for from intro_to_flask import app if I read the links you posted correctly, and assuming you've setup __init__.py correctly inside the intro_to_flask folder.

Related

python flask import. import error unknown location

i am trying to import create_app from init.py that is located in the website file
but everytime I try to run the code I get
ImportError: cannot import name 'create_app' from 'website' (unknown location)
this is my files
.vscode
env
website
--static
--templates
--__init__.py
--auth.py
--views.py
--models.py
main.py
init.py
from flask import Flask
def create_app():
app = Flask(__name__)
app.config['SECRET_KEY'] = 'hello'
return app
main.py
from website import create_app
app = create_app
if __name__ == '__main__':
app.run(debug=True)
thought this is the only method that isn't working I tried this method to check if the error is from vscode but it is just from this method
I tried
app.py
from flask import Flask
app = Flask(__name__)
#app.route("/")
def home():
return "hello Flask"
and when I write in the terminal `python -m flask run
I would get a website that says "hello Flask"
but when I press the run icon I get nothing
unlike the first one if I run it I would get an import error unknown location
and if I use python -m flask run I would get
Error: could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module not found in the current directory.
thought everything is sync to Github
in both of them I am working in a 'script' environment
A fix I came up with was adding name to the function defining create_app inside the init file.
Example:
from flask import Flask
def create_app(name):
app = Flask(name)
When importing, try from website.templates import create_app. So basically after you put it to import from website, add . following the sub-folder that you desired to import from (in this case it is templates).
I ran into the same thing, but it appears that these files "models, views, etc" isn't in website folder, they are inside static folder which is in website folder, that's why it's not working.
I just fixed them manually.
For someone getting this error later (like myself), be sure to check that your main package's
__init__.py
file is in the correct place.
You forgot create environment variable.
if you using mac or linux export FLASK_APP=main.py
if you windows set FLASK_APP=main.py
And last in main.py should be app = create_app()
try this
in your main.py
from website.init import create_app
or
change file init to init.py
then in your main.py
from website._init_ import create_app

python no module named 'A' __init__ vs package

I have looked up several answers on google but none of them made my problem correct.
I made my python file in directory A
in my __init__.py I have app = Flask(__name__)
When I want to get app from other file I use:
from A import app
but error says no module named 'A' so I changed to
from __init__ import app
now it works
Can anyone tell me why from A import app does not work?

Flask - Unable to run tests from tests folder (cannot perform relative import)

Hi i have the following project structure
project
app/
init.py
views.py
forms.py
models.py
static/
templates/
config.py
project_database.db
requirements.txt
run.py
tests/
test_project.py
run.py
from app import app
if __name__ == '__main__':
app.run()
app/init.py
from flask import Flask
from playhouse.flask_utils import FlaskDB
app = Flask(__name__, instance_relative_config=True)
app.config.from_object('config.DevelopmentConfig')
flask_db = FlaskDB(app)
database = flask_db.database
from app import views
tests/test_project.py
from ..app import app
import unittest
class TestProject(unittest.TestCase):
''' tests'''
When i try to run my tests i get the following error
Traceback (most recent call last): File "tests/test_status_code.py", line 1, in <module>
from ..app import app SystemError: Parent module '' not loaded, cannot perform relative import
If i move the tests file out of the folder it runs but i'd like to have it in the folder so I can have all test files in there.
I am using python 3.5 anf Flask 0.12.2
Thanks
Managed to get this working by doing the following
import sys
sys.path.append('../')
from app import app
import unittest
Class TestProject(unittest.TestCase):
But just cause this works is this right or is there a better more pythonic way?

flask tutorial troubleshooting: ModuleNotFoundError: No module named 'app'

I am having trouble running my run.py file. My file structure looks like this:
With another python file called 'run.py' located in flask/bin along with python3. My run.py file is simply:
#!flask/bin/python3
from app import app
app.run(debug=True)
However running 'python3 run.py' throws the error:
$ python3 run.py
Traceback (most recent call last):
File "run.py", line 2, in <module>
from app import app
ModuleNotFoundError: No module named 'app'
app.py looks like:
from flask import Flask
app = Flask(__name__)
from app import views
I am confused about how to solve this as I have been messing with the directories such as putting app.py into the flask/bin folder and putting it outside of all folders shown in my directory above, but these methods have not worked for me.
your run.py is not able to import app as it can not see app within the bin folder, what happens with python is that all python files are treated as modules and the folders with an init.py file are treated as packages so run.py will start looking for the app package to import the app module however it will search within the bin directory. Read through Python documentation to fully understand the modules and packages. for now you might want to reorganize your application directory to look like this:
dir app
file app.py
dir flask
file run.py
By ensuring that run.py and app directory are at the same level in the directory run.py will be able to import from app now.
I hope that helps

Python 3 idiomatic import with Flask

Flask suggests the following file layout:
runflaskapp.py
flaskapp/
__init__.py
runflaskapp.py contains:
from flaskapp import app
app.run(debug=True)
flaskapp/init.py contains:
from flask import Flask
app = Flask(__name__)
Running this with 'python3 runflaskapp.py' works fine. However it
seems to me that a more Python3onic way of doing this would be to
rename 'runflaskapp.py' as 'flaskapp/main.py' and then run
the whole thing as 'python3 -m flaskapp'. Unfortunately this doesn't
work:
$ python3 -m flaskapp
* Running on http://127.0.0.1:5000/
* Restarting with reloader
Traceback (most recent call last):
File "/home/username/src/flaskapp/__main__.py", line 1, in <module>
from flaskapp import app
ImportError: No module named 'flaskapp'
Does anyone know why and how to fix it?
The -m flag does this:
Search sys.path for the named module and execute its contents as the __main__ module… When a package name is supplied instead of a normal module, the interpreter will execute <pkg>.__main__ as the main module.
In other words, flaskapp is not imported as flaskapp, but as __main__, just like a script would be, and then its __main__ module gets executed.
This means from flaskapp import app will not work, because there is nothing named flaskapp.
But the relative import from . import app will. So, as long as there are no absolute imports anywhere in flaskapp except in your new __main__.py file, that one-liner should do it.

Categories