I'm using Pycharm 4, with flask 0.10.1, python 3.4
It seems that when running a flask application from inside pycharm, if I run it with:
app.run(debug=True)
My breakpoints are ignored. After some googling, I've found that in order to make PyCharm stop on breakpoints, I should run flask with:
app.run(debug=True, use_reloader=False)
Now PyCharm correctly stops on breakpoints, but I miss the autoreloading feature.
Is there any way to make both work together?
Using python 2.7 both things work
I reported this to PyCharm: https://youtrack.jetbrains.com/issue/PY-13976
I'm going to start with the short answer: No, what you want cannot be done with any releases of PyCharm up to 4.0.1.
The problem is that when you use the reloader the Flask application runs in a child process, so the PyCharm debugger is attached to the master process and has no control over the child.
The best way to solve this problem, in my opinion, is to ask Jetbrains to build a "restart on change" feature in their IDE. Then you don't need to use Werkzeug's reloader at all and you get the same functionality direct from PyCharm.
Until Jetbrains decides to implement this, I can share my workaround, which is not terribly bad.
In the "Edit Configurations", set the configuration you are going to use to "Single Instance only" (check box in the top right of the dialog box)
Make sure the configuration is the active one.
Configure your Flask app to not use the Werkzeug reloader.
Press Ctrl-D to start debugging (on Mac, others may have a different shortcut)
Breakpoints should work just fine because the reloader isn't active.
Make any code changes you need.
When you are ready to restart, hit Ctrl-D again. The first time you do it you will get a confirmation prompt, something like "stop and restart?". Say yes, and check the "do not show again" checkbox.
Now you can hit Ctrl-D to quickly restart the debugger whenever you need to.
I agree it is not perfect, but once the Ctrl-D gets into your muscle memory you will not even think about it.
Good luck!
I found that in PyCharm 2018.1.2 there is FLASK_DEBUG checbox in run configuration:
With this after making some changes, saving file triggers reload action.
In my setup, I'm debugging the flask app by running a main.py file which sets some configuration and calls app.run(). My python interpreter is set up in a Docker container.
My issue was that I needed to check Run with Python console.
The problem is because with use_reloader=True the werkzeug application is started in a seperate (child) thread of main application and PyCharm fails to correctly handle breakpoints because they are lost when the thread starts.
You can try to follow this thread: http://forum.jetbrains.com/thread/PyCharm-776 but it seams there was not too much progress on that.
I'd suggest using something Python-ish like pdb, i.e.:
#app.route('/<string:page>')
def main(page):
import pdb; pdb.set_trace() # This line actually stops application execution
# and starts Python debug shell in the console
# where you can examine current scope and continue
# normal code execution at any time.
# You can inject *any* code here.
# For example, if you type `print page` during pause,
# it will output content of "page" variable.
return render_template('index.html')
Try configuring this python running configuration in "Edit Configurations". After that, run in debug mode.
You need to unlock the console.
you start the app in debug mode
then you make something that causes an error.
at the end of the error message from flask is this
Here you enter the PIN flask prints in the console at the start
copy paste this pin into the console and click confirm PIN
now the breakpoints will work
from pycharm 2017 using python 2.7 (in my case with virtual env, but I suppose not necessary) I do:
run...
leave scripts and scripts parameters blank
I put in interpreter options: -m flask run
set the env variables FLASK_APP
than run attach to local process, and finally choose the running process
my use case is to connect from postman to flask rest services endpoints and interrupt on my breakpoints
Related
I'd like to develop a Bokeh application and based on the documentation, it seems best to run the bokeh serve --show app.py locally when developing the application.
After running the command, the application launches in a new browser tab and works correctly. However, it's not clear to me how to edit the code and rerun the application because once the tab is closed, the application continues to run in the terminal and the only way to exit is through kill <pid> which is somewhat inconvenient.
What is a good workflow for developing a Bokeh application?
If you are on a mac, you can stop the application with ctrl+c from terminal. This will shut down the app. Then you edit the code, save it and rerun bokeh serve --show app.py from terminal. That's all.
Note that the application should continue running as long as your browser window is open. If you change the inputs using the widgets in your dashboard it will print out logging in terminal, as it updates the dashboard according to your code.
Closing the tab will only close the client session. To kill the server process, indeed you need to kill the process somehow. Ctrl+c is convenient from the terminal. If you are in an IDE like PyCharm, I suggest setting up your configuration to run bokeh like a python script via python -m bokeh serve --show. Then you can use your IDE's start/stop/restart functionality also.
FYI if you are in Pycharm 2017.x, your script name should be the script or directory (for directory-based apps) of the bokeh app and interpreter options should be -m bokeh serve --show
I typically also set up another configuration for debugging which runs the script like a typical python script (no special interpreter args). This will allow you to use your IDE's debugger for any issues basically up to the initial page load. For any debugging beyond that (i.e. callbacks), I typically use a combination of logging and/or manual pdb.set_trace() calls.
I've been testing out a few .py files with Flask, referring to 127.0.0.1:5000 frequently to see if they're interfacing correctly with the HTML, after running the .py I'm getting the following as normal:
* Running on http://127.0.0.1:5000/
* Restarting with reloader
However, 127.0.0.1:5000 has suddenly stopped updating when scripts are run, remaining as it was after the first time a script was run since my computer has been turned on (restarting the machine is the only way I've found to take a fresh look at my work). To confirm that it's not an issue within .py files or my templates, Flask's hello world example with app.run(debug=True) does not update this localhost page when run. Is there any way to remedy this?
Two things that may or may not be involved with this issue:
(1) I am not using virtualenv but simply running .py files from folder directories on my desktop (following proper format for Flask and the template engine, though). (2) Around the time the problem started I installed SQLAlchemy and its Flash extension, Flask-SQLAlchemy with pip.
After tracking the processes down by running $ netstat -a -o in the command line, it turns out it wasn't a code error, but rather multiple instances of pythonw.exe, which can be taken care of in the Task Manager. I'm not sure why the processes keep running after I close out of all python windows, or why it keeps communicating with 127.0.0.1:5000, however, so thoughts on this would still be appreciated.
Thats right. Just press 'Ctrl+Shift+Esc' to open the task manager.
Scroll down to find the 'python3.exe' files and end the task manually.
The reason is 'ctrl+c' doesnt work for me (it just copies the text on terminal window), so I have to manually kill the python interpreter running in the background. Its hard work, but hey atleast you dont have to restart your computer everytime!!
I'm using Flask 0.9 with Python 2.7.1 within a virtualenv, and starting my app with foreman start
In other apps I've built when I add the following line to my app:
import pdb; pdb.set_trace()
then reload the browser window, my terminal window displays the pdb interactive debugger:
(pdb)
However in my app when I add those lines nothing happens. The browser window hangs and shows a constant state of loading yet nothing shows in the console.
Is there some magic that needs to happen?
This is because you're using Foreman, which captures the standard output.
To debug your app with pdb, you'll need to "manually" run it, using python app.py or whatever you use.
Alternatively, you can use WinPDB (which, despite the name, has nothing to do with the operating system), which will let you remotely debug a Python process. You can even use it when the program is running on another server.
In Gnome, whenever an application is started, the mouse cursor changes from normal to an activity indicator (a spinning wheel type thing on Ubuntu). Is there any way to inform Gnome (through some system call) when the application has finished launching so that the mouse cursor returns to normal without waiting for the usual timeout of 30 seconds to occur.
I have a program in Pythong using GTK+ that is showing the icon even after launching, so what system call do I make?
Normally it happens automatically when you open the application's window.
It may be that the application's launcher just calls an already running instance, in that case it won't be automatically detected. The call you need then is this:
import gtk
gtk.gdk.notify_startup_complete()
Your application can opt out of startup notification by adding
StartupNotify=false
to your application's .desktop file.
Of course, it is friendlier to leave it enabled and participate in startup notification.
I had a similar issue with an application I wrote. I was launching the application through a shell script containing the line
python /path/to/application.py
This launched the application as I expected, but the startup notification did not stop.
It worked correctly once I changed the content of my script to this:
exec "/usr/bin/python" "/path/to/application.py"
Obviously the latter one seems to be the correct way to launch the application, though I don't have enough insight to tell why.
This normally happens automatically when calling the gtk.main() function
I have pydev on eclipse and would like to debug handlers. I put breakpoint on a handler and start project in debug mode. When I click on the hyperlink corresponding to handler the control does not come back to breakpoint. Am I missing something here?
Also the launch is for google app engine application in python.
I'm using eclipse with PyDev with appengine and I debug all the time, it's completely possible !
What you have to do is start the program in debug, but you have to start the dev_appserver in debug, not the handler directly. The main module you have to debug is:
<path_to_gae>/dev_appserver.py
With program arguments:
--datastore_path=/tmp/myapp_datastore <your_app>
I hope it help
The simplest way to debug is to use the builtin python module pdb and debug from the shell.
Just set the trace in the handler you want to debug.
import pdb
pdb.set_trace()
How do U run the server, from within the eclipse or from the shell. If it is from the shell, then how does eclipse know you are even running the application;
You could use an user friendly version of pdb, ipdb that also includes user friendly options like auto complete.