I am learning Flask.
I was able to run the Hello World tutorial as shown here
Then I tried to build the Flaskr program following the tutorial http://flask.pocoo.org/docs/tutorial/introduction/
I ran into an issue with the Flaskr program accessing the database,specifically "sqlite3.OperationalError
OperationalError: unable to open database file"
so I took a break and went back to seeing if I could run my "Hello World" program.
Now when I go to the url 127.0.0.1:5000/, instead of seeing "hello world" I still see my data base error from the Flaskr program.
It seem like I need to reset the server instance or something? Please help!
kill python task in the task-manager and then run your server
If you're testing or working on multiple projects at the same time, please run each one in a dedicated virtual environment and serve at a different port because by default flask serves at 127.0.0.1:5000.
Use something like this below:
if __name__ == "__main__":
app.run(host='0.0.0.0',port=8001)
You can change the port in each other project and run all of them without any problem.
Happy coding,
J.
Related
I am creating a blog using Flask. However, when trying to run my code on a local server, I am unable to do so, as it comes with this error:
Error: Failed to find Flask application or factory in module "flaskblog". Use "FLASK_APP=flaskblog:name to specify one.
I had typed in, "set FLASK_APP=flaskblog.py" in my terminal prior, then typed in "flask run." What would be the next best steps to take so I can run the code on a local server? Running this on a Windows 10 computer.
If you're using a bash cli, try using "export FLASK_APP=flaskblog.py". Set might only work if you are using something like Windows CMD.
I have spent hours banging my head against the wall on this one!
I am running Python 2.7.10 on a Mac. I have a few Python scripts that I have written which run inside the Atom editor.
I have been trying to run these scripts on a local web server to speed up development. I have tried MAMP (which just throws 500 Internal Server Errors), and now "python -m SimpleHTTPServer" which just displays the python code in the browser and doesn't seem to execute it.
I have chmod +x my .py files. I start the web server in the folder that contains the .py files.
Here is an example...
hello.py
print("hello world")
When I browse to http://localhost:8000/hello.py I get the raw code displayed in the browser.
print("hello world")
If I use terminal and enter "python hello.py" it runs and displays the correct output...
MacBook-Pro-3:folder dj$ python hello.py
hello world
I have tried dozens of tutorials and suggested solutions, but none seem to help. Am I missing something fundamental here?
Thanks!
D
The server that python -m SimpleHTTPServer (or, for future readers, python -m http.server on Python 3) spins will not execute any file. It is merely a server that serves files as the documentation suggests:
The SimpleHTTPServer module can be used in the following manner in order to set up a very basic web server serving files relative to the current directory.
In order to get a server that will actually execute Python code, you'll need to use another tool. I'd start with bottle or flask.
I am new to python and trying to create a simple API. below is the code for the same.
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == '__main__':
app.run(host='0.0.0.0',port=5000)
I have saved this as code as "hello-world.py".
When I am trying to run this file in command prompt using python command
python hello-world.py
Command executed in command prompt
Here I have no error but web page is not getting displayed. Below is the error on web page.
http://localhost:5000/
This site can’t be reached
localhost refused to connect.
Search Google for localhost 5000
ERR_CONNECTION_REFUSED
1st Issue - Even after running the code properly, why the web page is not displayed
Now, I enter python command in my command prompt and then I try running file again python hello-world.py. Here I am getting the below error
>>> File "<stdin>", line 1
python hello-world.py
^
SyntaxError: invalid syntax
2nd Issue - Why I am getting an error while trying run the hello-world.py
Please guide me on how to resolve this issue.
From what you're describing, I think you're trying to use the Flask Quickstart. If so, have you done the following command in your command prompt enviroment?
C:\path\to\app>set FLASK_APP=hello.py
At which point if you run your code and it's correctly structured you should see
Running on http://127.0.0.1:5000/
Because you don't see the "Running" on your prompt I would assume this hasn't been done. If this doesn't work, I would just double check that Flask is installed and that Python sees it, and that you're following the steps in the tutorial correctly.
Edit: Also, I would recommend removing the hyphen in hello-world.py. It may help you avoid errors here and elsewhere in python coding. See the responses to this question.
1st issue - firewall or some other services may be blocking your port.
2nd Issue - you are running in interactive terminal. Please execute the command in command prompt.
I am following this online tutorial to learn Flask with MongoDB. I ran my Flask file in my local machine. with the following command
python connect.py
I don't see any error.
And I wish to see in which path this python file is running. For example, as per the tutorial, the output in the command line shows the following:
Running on http://127.0.0.1:5000/
And in my case, I don't see any such server path which I can go and see, for further development.
Can anyone please let me know how I can get to see this path in my machine?
When you run connect.py, the directory of this file acts as the root of the website. anything after / will be in the same folder as connect.py. When you run it, it will create a new socket which, by default, you can access by going to your browser and navigating to http://127.0.0.1:5000/ this can be changed by putting a HOST and or PORT argument inside of where the app is started, usually in app.run() as most tutorials will use as an example, like so.
if __name__ == '__main__':
app.run(host="192.168.1.11", port=8080, debug=True)
http://192.168.1.11:8080/
To debug a bug I'm seeing on Heroku but not on my local machine, I'm trying to do step-through debugging.
The typical import pdb; pdb.set_trace() approach doesn't work with Heroku since you don't have access to a console connected to your app, but apparently you can use rpdb, a "remote" version of pdb.
So I've installed rpdb, added import rpdb; rpdb.set_trace() at the appropriate spot. When I make a request that hits the rpdb line, the app hangs as expected and I see the following in my heroku log:
pdb is running on 3d0c9fdd-c18a-4cc2-8466-da6671a72cbc:4444
Ok, so how to connect to the pdb that is running? I've tried heroku run nc 3d0c9fdd-c18a-4cc2-8466-da6671a72cbc 4444 to try to connect to the named host from within heroku's system, but that just immediately exits with status 1 and no error message.
So my specific question is: how do I now connect to this remote pdb?
The general related question is: is this even the right way for this sort of interactive debugging of an app running on Heroku? Is there a better way?
NOTE RE CELERY: Note, I've now also tried a similar approach with Celery, to no avail. The default host celery's rdb (remote pdb wrapper) uses is localhost, which you can't get to when it's Heroku. I've tried using the CELERY_RDB_HOST environment variable to the domain of the website that is being hosted on Heroku, but that gives a "Cannot assign requested address" error. So it's the same basic issue -- how to connect to the remote pdb instance that's running on Heroku?
In answer to your second question, I do it differently depending on the type of error (browser-side, backend, or view). For backend and view testing (unittests), will something like this work for you?
$ heroku run --app=your-app "python manage.py shell --settings=settings.production"
Then debug-away within ipython:
>>> %run -d script_to_run_unittests.py
Even if you aren't running a django app you could just run the debugger as a command line option to ipython so that any python errors will drop you to the debugger:
$ heroku run --app=your-app "ipython --pdb"
Front-end testing is a whole different ballgame where you should look into tools like selenium. I think there's also a "salad" test suite module that makes front end tests easier to write. Writing a test that breaks is the first step in debugging (or so I'm told ;).
If the bug looks simple, you can always do the old "print and run" with something like
import logging
logger = logging.getLogger(__file__)
logger.warn('here be bugs')`
and review your log files with getsentry.com or an equivalent monitoring tool or just:
heroku logs --tail