Trying to run the flask code in Vscode (mac Os x 10.14.15) but getting an error.
First I tried running:
python3 -m flask run
But, it showed
"Could locate a Flask application. You didnt provide Flask_App variable or wsgi.py or app.py was not found in the current directory.
I then ran the following command:
export Flask_app = app.py
(though I didnt need to export the variable since my file name is already app.py)
flask run
Could not import 'app'
Please note that I have verified that Flask version 1.0.3 is installed
Here is the code I'm trying to execute:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def home():
return "Hello, Flask!"
Python script should have been created in env/bin. Program ran successfully after moving .py file in the bin folder.
Related
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
I am writing a flask application with PyCharm. But Pycharm couldn't recognize Flask module. Flask installed globally on my computer as seen on screenshot. I tried virtualenv also but nothing change. Still can't find Flask. I can run Flask anywhere except Pycharm succesfully. My operating system is Windows by the way.
Here is my code snippet
from flask import Flask, render_template, url_for
app = Flask(__name__)
#app.route("/")
#app.route("/home")
def home():
return render_template("home.html")
if __name__ == "__main__":
app.run(debug=True)
Change the name of your file from flask.py to something else and it will work. Naming your file flask.py causes Python to look in your file for the Flask class, and it fails.
Naming a file site.py causes this issue.
It looks like a module named "site" is involved in importing the installed libraries, so by creating a module of the same name I prevent flask from being discovered.
Renaming my file from site.py to something else fixed the issue for me.
I'm working on my first web app on google app engine using python and flask. My machine is running Ubuntu 15.04, 64 bits.
Problem is that after following all the directions in the tutorial https://cloud.google.com/appengine/docs/standard/python/getting-started/python-standard-env, I get an error that python is not able to import flask. I get the following error in the eclipse SDK:
Unresolved import: Flask main.py /flask-app line 2 PyDev Problem
I checked and the flask folder is in my app's "lib" directory.
Any ideas what it is that I'm missing?
Added:
1) I completely removed gcloud, eclipse, pydev, the workspace folder, everything. Then reinstalled step by step and still get the same error.
2) Tried different approaches to include flask in the Pydev $PYTHONPATH:
Copied flask to the app's root directory
Added source folders for the flask folder, the lib folder and the lib/flask folder. Even though the different flask folders appear in the tree, they don't get resolved.
Here's the screenshot:
3) Contents of my main.py file: (the error is on the 2nd line, from...)
import logging
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello():
return 'Hello World!'
#app.errorhandler(500)
def server_error(e):
# Log the error and stacktrace.
logging.exception('An error occurred during a request.')
return 'An internal error occurred.', 500
I'm following the Flask Mega Tutorial, and I'm running into an issue once I get to the second part and restructure my folder structure to match theirs, I cannot import Flask.
My current folder structure is as follows
/FlaskTest
/app
/static, templates etc
/flask
/virtualenv folders etc
/tmp
run.py
as far as I can tell, the folder structures are identical other than naming of the top level directory.
in my __init__.py file (/app/__init__.py), I'm doing as instructed in the tutorial,
from flask import Flask
app = Flask(__name__)
from app import views
I'm getting an Import Error that "cannot import name 'Flask'". I'm guessing the issue is because the flask package was installed to /flask/lib/site-packages.
My question: How can I reference the sub folder of flask/site-packages?
I've read through the python import system documentation and from what I can make of it through the first pass of reading it over, I would need to likely do something like from flask import flask.Flask or something to that effect.
UPDATE: So after cd'ing around the directory and checking pip list, I realized that flask wasn't accessible to my app directory. I ran pip install flask in the app directory. Now my site runs, but I'm not sure if this is the best practice of doing things with Python. Please provide some clarity as what the best practice is for installing packages and where the packages reside.
UPDATE 2: After creating a directory called standalone. In this folder, I created a virtual environment called standalone-test. Once, I did that, I also mkdir'ed app and copied it's contents from FlaskTest so that way the code would be identical. I was able to run the run.py script by using python run.py, but I can't run python -m app like you had said without running into an error. The error is as follows if it helps.
"No module name app.main; 'app' is a package and cannot be directly executed.
I am able to run python run.py as I mentioned, but I'm not able to run the python -m app command as you had mentioned
I think something went wrong in your execution environment. Here are some explanations.
The virtualenv
See the documentation of virtualenv
If you have followed the tutorial:
The flask directory is your virtualenv,
On posix system, you have a flask/bin subdirectory, or
On Windows system, you have a flask\Scripts subdirectory.
I make the assumption that you are on posix system.
To activate your virtualenv, run:
source flask/bin/activate
Your prompt should change to something like: (flask)$.
To list the installed libraries use pip:
pip list
Make sure you see Flask. The tutorial encourages you to install a lot of Flask plugins, so there are a lot of Flask-Something…
If Flask is missing, install it:
pip install Flask
Run your app
Your application is in the app directory, it has an __init__.py file (it's a Python package).
In this file, you have:
from flask import Flask
app = Flask(__name__)
from app import views
From your FlaskTest/ directory, try to run this script like this:
cd FlaskTest/ # if not in this directory
python -m app
This should import Flask, instanciate your app (but don't run it), import the views module.
If app/views.py exist you should have no error.
=> at this point, we have simulated what run.py imports…
Now write run.py in your FlaskTest/ directory:
#!flask/bin/python
from app import app
app.run(debug=True)
Run it like this:
python run.py
Note that the shebang #!flask/bin/python is unusual, but should work in the context of the tutorial.
This should start your http server…
I'm trying to get a Flask "hello world" application working on a Dreamhost shared server, following the instructions on their wiki, but I'm not having any luck.
My Flask application is the "hello world" one from the Flask quickstart guide:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run()
Which I've got in a file called "hello.py" in a folder called mysite, as per the DH wiki instructions. My passenger_wsgi.py file is:
import sys, os
INTERP = os.path.join(os.environ['HOME'], 'flask_env', 'bin', 'python')
if sys.executable != INTERP:
os.execl(INTERP, INTERP, *sys.argv)
sys.path.append(os.getcwd())
from mysite import hello as application
I've tried running the commands in a Python console, and last import line failed until I added the __init__.py file to the mysite directory.
When I try and access the website I just get a 500 error (and nothing in the logs unfortunately, unless they're in logs I can't get to as this is a shared server...).
As this is the most basic of setups (i.e., copied and pasted from a wiki), I can't help feeling that I'm missing something really simple. Or perhaps this isn't possible on a shared server?
Does answering my own question mean I'm talking to myself?
Anyway - I seem to have fixed it. Rather than find a nice helpful error message, I went through all the steps again one at a time, and it turns out it was an import error in the passenger_wsgi.py file. As the app is in the mysite subdirectory, the line:
from mysite import hello as application
should have been (and in fact, now is):
from mysite.hello import app as application
And it works. Which is nice.