Config module not found when running Flask in Spyder - python

I'm going through the Flask mega-tutorial with the following project structure
my-project/
app/
__init__.py
routes.py
config.py
With my __init__.py file :
from flask import Flask
from config import Config
app = Flask(__name__)
app.config.from_object(Config)
from app import routes
While running __init__.py from Spyder, I had the following error
ModuleNotFoundError: No module named 'config'
I'm new to Flask and Python, and the problem must be that python, run from Spyder, doesn't find the config.py file in the top-level directory. Would you have any recommendations on how to setup Spyder or structure my project? Thank you
ps : the project ran fine when running Flask run from the command line, but I'd like to use Spyder for debug functionalities

Related

im trying to host my flask api on heroku, but when passing the path to my app, i get an import error

the error im getting: ImportError: attempted relative import with no known parent package
My folder structure:
-Backend
__init__.py
run.py
procfile
__init.py:
has the create app method
run.py:
from . import create_app
if __name__ == "__main__":
app = create_app("config")
app.run(debug=True)
procfile:
web: gunicorn run:app
EDIT:
I rearranged the app structure by which:
Backend
init.py
procifle
extensions.py
src
init.py
run.py
init.py:
empty
src/init.py:
from ..extensions import db,migrate
has the create app method
src/run.py:
from src import create_app
if __name__ == "__main__":
app = create_app("config")
app.run(debug=True)
so now the new error is:
from ..extensions import db
ImportError: attempted relative import beyond top-level package
Edit #2:
another thing to point out is, that suppose in the run.py i do the following:
from Backend import create_app()
I get the following error: no module named "Backend" why could that be?
Has anyone faced a similar issue and what can I do to solve it?
Update based on new information I think we can get you home.
1.) Move run.py next to the procfile (same dir). That "run file" should be in that top level dit at /app/run.py in Heroku.
A good basic pattern for file organization is to "module" everything (a dir with a __init__.py file). Then all imports are from the top level run.py [where the process is running from] walking subfolders. As long as you traverse down into imports things stay pretty stable. If you are trying to navigate up dirs it becomes problematic.
Resolve any imports, it can be tricky at first but soon becomes a way of thinking.
2.) Move extensions.py into src (for now) and import from src import foo
The following is the pattern in all my active Django and similar apps running in Heroku. Same layout I have been using for many years. In the following screenshot manage.py is the Django equivalent of your run.py. I am not using the docker image on this app, so that is not in the equation, just code and dependencies.
Your problems are totally understandable, it's easy to take for granted all the times other Python devs do this.

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

flask run vs. python

I'm having difficulty getting my flask app to run by using the "python" method. I have no problems using
export FLASK_APP=microblog.py
flask run
but attempting to use
python microblog.py
will result in the following error
ImportError: No module named 'app'
where my microblog.py file looks like this:
from app import app, db
from app.models import User
#app.shell_context_processor
def make_shell_context():
return {'db': db, 'User': User}
and my __init__.py file looks like this:
from flask import Flask
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager
app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
login = LoginManager(app)
login.login_view = 'login'
from app import routes
if __name__ == '__main__':
app.run()
The folder these files reside in is called 'app'. I've tried putting the
if __name__ == '__main__':
app.run()
In the microblog.py file as well, which was also unsuccessful.
I also have folders for templates/static/etc. and a routes file where I run all my #app.route() commands
Again, everything works fine when I run "flask run" in terminal, it just breaks down when I try using "python microblog.py". I'm trying to launch the app on AWS and just about every tutorial requires the applications to be called using the "python" method. Any help is appreciated.
[Solved]
I had my directories mixed up. I pulled microblog.py back outside of the app folder and was able to run python microblog.py with no issues and without having to edit anything.
In hindsight, I probably should have posted the file structure of everything right in the beginning.
In this line from app import app, db you're trying to import a module called app.app, but it apparently does not exists in your project source.
Try to create a file named app.py inside app/ path, copy and paste all content of __init__.py file inside of it.
Your __init__.py must be blank. It's just indicate that your path app/ is a importable module.
See this article.
I have tried this and it worked for me on windows.
$env:FLASK_APP="hello.py"
flask run
That's it

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

Flask large project setup and error importing views

I am just starting up my first Flask project and I am trying to set it up as packages so the app can be broken up. My file structure is:
project/
setup.py
config.py
requirements.txt
.gitignore
appname/
__init__.py
view.py
static/
stylesheets
templates/
html
I have followed a number of tutorials and some have used python setup.py to run the project and others have used flask run. First off, what is the difference?
Now in my init.py the code is:
from flask import Flask, render_template
from appname import views
app = Flask(__name__)
My views.py has:
from appname import app
from flask import render_template
#app.route('/')
def index():
return render_template("index.html")
So far I have been exclusively trying flask run but I get the error:
AppException: The file/path provided (appname) does not appear to exist
If I replace the from appname import views with the views code it works fine. So the issues lies with how I am importing but I feel like I have tried every sort of importing format I know...
Well, the first thing that I see is an infinite loop of imports:
appname imports appname.views, and
appname.views imports appname.
You can break the infinite loop of imports like this: in __init__.py, remove the import of views which is not used:
from flask import Flask
app = Flask(__name__)
To run your application, you must tell Flask where you application is. To do that, you need to export an environment variable, like this:
export FLASK_APP=appname/__init__.py
flask run
* Serving Flask app "appname"
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Everything is explained in the Quick start page of the documentation.
Ooops, I forgot your first question: python setup.py is used to run setuptools tasks (like the make command with makefile). It has nothing to do with Flask. See Building and Distributing Packages with Setuptools.
You're trying to import views whereas your module is called view.py see python docs on python packaging

Categories