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
Related
i have a strange experience with Python code, so:
Im trying to learn Flask with some tutorial, and i managed to follow some with nice result, and now im trying to start my own project to make sure i understood the stuff, but i find myself stuck by hours for this problem
I have this directory structure:
/projectName/
/venv/
/website/
/static/
/templates/
/__init__.py
/otherPython.py
/app.py
app.py
from website import create_app
app = create_app()
if __name__ == '__main__':
app.run(debug=True)
init.py
from flask import Flask
def create_app():
# I create the app and give the secret key
app = Flask(__name__)
app.config['SECRET_KEY'] = 'stackoverflow'
return app
The Problem
Cannot find reference 'create_app' in 'imported module website'
Enviroment:
Win10 pro 10.0.19045
python 3.10
PyCharm 2022.1.4 (community edition)
Does somebody have any clue on what im missing, or if im allucinating and dont see a clear code/path error?
In a identical project it works, i already tryied to mark ProjectFolder as Source Root, already tryed to clean caches and restart, and i cannot understand why as a identical import with same folder structura and method name, in one project work(the tutorial one), and now in mine doesnt.
I dont know if that is helpfull but after from website import if i do ctrl+space i do not see the init.py file nor the create_app() method.
In my main Python file, I import another script of mine called helper_1.py (from the subfolder my_helpers) like this:
from my_helpers.helper_1 as h1
However, when I now try to start my server (or deploy it to Heroku), the server will crash with the error notice:
ModuleNotFoundError: No module named 'my_helpers'
I do have a Procfile, requirements.txt, runtime.txt, and wsgi.py.
The content of my wsgi.py is:
from app.main import app
if __name__ == "__main__":
app.run()
MY QUESTION:
Where and how do I have to declare my custom modules (own scripts) so they are properly detected when starting the Flask server?
Everything works fine if I leave out the external reference to my custom module.
The folder my_helpers needs to be a package. To do this put an __init__.py file inside the my_helpers folder. This maybe fix your problem.
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.
After successfully completing the pythonanywhere flask tutorial (pa.com), Miguel Grinberg's the "Flask Mega Tutorial" (fmt) beckoned. Sadly, I've not even made it to "Hello, World". Here's what I have done:
In pa.com attempting to follow fmt verbatim is a no go:
python3 -m venv flask
leads to an error of
ensurepip is not available
and we do not have sudo access.
Undeterred, I reasoned that all Miguel is asking us to do is distribute the functionality we see in one file in the pa.com tutorial (flask_app.py) into a few files that will make the building of a full app easier. Since pa.com is already setting up my base web app with flask and python 3.4, not being able to set up the virtual env. did not seem to be a block, at least not at first.
Per the fmt, in the base dir of the pa.com (pwd -> home/{username}/microblog) -- which is where the flask_app.py file that successfully generates the pa.com tutorial page lives -- I set up app and tmp directories, and create app/__init__.py, app/views.py and the run.py files as directed by fmt
Hitting the app page (run.py the only file in the main directory) generates an Unhandled Exception on the page.
Changing the name to flask_app.py (which seems to be what pa.com expects on flask installations) generates the same error.
Modifying the content of the flask_app.py code to:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return "working from flask_app.py"
Generates a successful output from the app, whereas having the same code in a file named run.py generates the same Unhandled Exception error.
the lines:
from app import app in both run.py and views.py and
from app import views in __init__.py
all make me wonder... where is this "app" module coming from? But aside from being puzzled by that question, no other ideas on how to proceed from here. Any suggestions? Would really like to get set up on pa.com and work through this tutorial/book.
Feel like I am missing something basic, but not sure what.
First rule is: don't use app.run() on PythonAnywhere - that's what run.py is trying to do. That's fine for your own PC, but on PA it will cause an error. It's fine to have the file there, but don't try and import from that file in your wsgi config.
Instead, you just need to import the flask app variable, which Miguel gets you to put in app/__init__.py (that's slightly confusing, a variable called app, and a folder called app, but we can deal with it!)
To do that, you'll want to add the folder that contains the app folder to your sys.path. You also need to "rename" the app variable to application as you import it:
# assuming we have /home/myusername/microblog/app/__init__.py:
path = '/home/myusername/microblog'
if path not in sys.path:
sys.path.append(path)
# now we can import the app variable from the app folder's __init__
# and rename it to application
from app import app as application
More info: a brief guide to flask on pythonanywhere and a guide to debugging imports and sys.path problems in your pythonanywhere wsgi file
from microblog import app as application
This is fixed my solution.
Best Regards
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.