How do I start the pylons web application programmatically? - debugging

Normally, we start a pylons web application via command line:
pastser serve --reload development.ini
I wanna know can we start it programmaticly? In a python script file?
I want this because I can start it in IDE, and use the debugger

#!/usr/bin/env python
from paste.script.serve import ServeCommand
ServeCommand("serve").run(["--reload", "development.ini"])
...some IDEs (e.g. pydev) won't support breakpoints in debug mode if you have --reload enabled. Simply remove it from the list you pass to .run() to disable the reload functionality and enable IDE breakpoints.

That sounds like a job for a shell script - you could launch it from a Python script, but a shell script is likely to be cheaper in terms of mental effort.

Related

GoLand IDE: Debug program initiated by shell script (Kubernetes local cluster)

I'm a beginner to Kubernetes, and I tried to understand the source code by debugging from GoLand IDE. Starting a cluster with the provided hack/local-up-cluster.sh script works fine, but I cannot debug in GoLand.
I've tried to edit Run -> EditConfigurations-> +Shell Script with the script path as hack/local-up-cluster.sh, the script could run successfully but it won't stop at any break point.
Can someone please help me on how to set the IDE to debug the code initiated by a shell script?
If you don't use the IDE to launch the process, then you can use the Run | Attach to process feature to attach the IDE debugger to an existing project.
Make sure that the compiled application has the optimizations turned off, if you are using Go 1.10+ the compiler flag is -gcflags="all=-N -l".

Flask debugger not appearing in browser

I'm trying to use the flash debugger, where I made a mistake on purpose to test out the debugger. Whenever the error is shown it is only shown in the terminal, and I can't access the localhost to test it.
I have
app.debug = True
But it only shows the stack trace in the terminal when I run.
flask run
when I run
python routes.py
I see the line where the error happened.
But how can I see the interactive debugger in the browser window?
Try setting the environment variable FLASK_ENV=development
This seems to be the suggested way to enable interactive debugging and other nice to have development features when using the flask run command.
Alternatively you can set FLASK_DEBUG=1 to just enable the interactive debugger

Start background jobs using Pycharm when starting server

I develop my python applications with Pycharm and I love it. Recently, I discovered grunt and the superb watch-action of grunt.
I want to start grunt watch, and maybe some other applications, in the background when I start my Django server using run. I know, how to start applications before launch but Pycharm waits for their exit before starting the django server. So this is not the right way, because I need the application to run in the background.
Any ideads how to achieve this?
You can setup an external tool as a launcher for the background task.
I am currently on mac, hopefully you can adapt the idea with some dos commands. Let's assume the background task can be started from task.py
Step 1: Create a "launcher script", saved as bg_run.sh in this example. Note the fork instruction & to run the task in a new process.
run_task() {
python /Path/To/Task/task.py
}
run_task &
Step 2: Create an external tool in Preferences | Tools | External Tools to run that script.
Step 3: Add that external tool to run before launch in the Run/Debug Configurations.
Note: The question on how to kill that background task is left open ;)

debugging a uwsgi python application using pycharm

Is it possible to debug a uwsgi application using an ide like PyCharm? I can debug flask based apps fine by running them directly from pycharm but cannot even run a uwsgi app from within pycharm.
Do I have to use remote debugging? Is it possible to start a uwsgi app from within pycharm using run?
You can still run your WSGI app outside of uWSGI for development and debugging purposes.
However sometimes this is not possible, for example if your app relies on uWSGI API features.
As far as I know you can't use "Attach to Process" from PyCharm because your WSGI app is running embedded into uWSGI, and there are no visible Python processes. Remote debugging however works like a charm.
Locate pycharm-debug*.egg files in your PyCharm distribution. For example, on OSX both can be found in /Applications/PyCharm.app/Contents
Copy pycharm-debug-py3k.egg next to your Flask app, or copy pycharm-debug.egg instead if you are using Python 2.7
In PyCharm, create a "Python Remote Debug" configuration from "Run/Debug Configurations" dialog. In this example I use localhost and port 4444. This dialog will show you the corresponding pydevd.settrace(...) line.
Add the following code to your app :
import sys
sys.path.append('pycharm-debug-py3k.egg') # replace by pycharm-debug.egg for Python 2.7
import pydevd
# the following line can be copied from "Run/Debug Configurations" dialog
pydevd.settrace('localhost', port=4444, stdoutToServer=True, stderrToServer=True)
In PyCharm, start the remote debugging session. PyCharm's console should display the following line :
Waiting for process connection...
Run your app from uWSGI as usual. It should attach to the debugger, and PyCharm's console should display :
Connected to pydev debugger (build 139.711)
Your app should break on the pydevd.settrace(...) line. You can then continue and use PyCharm debugger as usual (breakpoints and so on)
Not sure how to interpret your question, as you are mixing apples and oranges. Flask is a framework, uWSGI is an application server. I'll try to answer, though.
As far as I know, uWSGI is not pure python, so debugging it in PyCharm will not be trivial, if even it is possible.
However, since you are using uWSGI to run your application, I'm assuming it complies with the WSGI protocol. In that case, for debugging purposes, you can alternatively run it from a simple pure-python application engine like wsgiref.simple_server.WSGIServer.
There is now an official guide on how to do this:
https://www.jetbrains.com/help/pycharm/remote-debugging-with-product.html#
If your code already exists in remote, you only need to follow Create a run/debug configuration
You'll need the IP where the PyCharm is running. When you run the remote debugger from PyCharm, it'll create a debugging server. Your code will connect to this server.
In my case, I'm using Vagrant, with private IP of the guest 192.168.0.3, and the host's private IP is 192.168.0.1.
My code in the remote guests will connect to the debugging server through the host IP. So I need to use my host IP in the code that I want to debug.

Meteor: Debug on server side

Does anyone know a good method to debug server side code?
I tried enable Node.js debug then use node-inspector but it does not show any of my code.
I end up using console.log but this is very inefficient.
Update: I found the following procedure works on my Linux machine:
When you run Meteor, it will spawn two processes
process1: /usr/lib/meteor/bin/node /usr/lib/meteor/app/meteor/meteor.js
process2: /usr/lib/meteor/bin/node /home/paul/codes/bbtest_code/bbtest02/.meteor/local/build/main.js --keepalive
You need to send kill -s USR1 on process2
Run node-inspector and you can see your server code
On my first try, I modify the last line on meteor startup script in /usr/lib/meteor/bin/meteor to
exec "$DEV_BUNDLE/bin/node" $NODE_DEBUG "$METEOR" "$#"
and run NODE_DEBUG=--debug meteor on command prompt. This only put --debug flag on process1 so I only see meteor files on node-inspector and could not find my code.
Can someone check this on Windows and Mac machine?
In Meteor 0.5.4 this has become a lot easier:
First run the following commands from the terminal:
npm install -g node-inspector
node-inspector &
export NODE_OPTIONS='--debug-brk'
meteor
And then open http://localhost:8080 in your browser to view the node-inspector console.
Update
Since Meteor 1.0 you can just type
meteor debug
which is essentially a shortcut for the above commands, and then launch node inspector in your browser as mentioned.
Update
In Meteor 1.0.2 a console or shell has been added. It may come in handy to output variables and run commands on the server:
meteor shell
Meteor apps are Node.js apps. When running a Meteor app with the meteor [run] command, you can configure the NODE_OPTIONS environment variable to start node in debug mode.
Examples of NODE_OPTIONS environment variable values:
--debug
--debug=47977 - specify a port
--debug-brk - break on the first statement
--debug-brk=5858 - specify a port and break on the first statement
If you export NODE_OPTIONS=--debug, all meteor command run from the same shell will inherit the environment variable. Alternatively, you can enable debugging just for one run, with NODE_OPTIONS="--debug=47977" meteor.
To debug, run node-inspector in a different shell, then go to http://localhost:8080/debug?port=<the port you specified in NODE_OPTIONS>, regardless of what node-inspector tells you to run.
To start node.js in debug mode, I did it this way:
open /usr/lib/meteor/app/meteor/run.js
before
nodeOptions.push(path.join(options.bundlePath, 'main.js'));
add
nodeOptions.push('--debug');
Here are additional practical steps for your to attach debugger eclipse:
use '--debug-brk' instead of '--debug' here, because it's easier for me to attach node.js using eclipse as debugger.
add 'debugger;' in the code where you want to debug.(I prefer this way personally)
run meteor in console
attach to node.js in eclipse(V8 tools, attach to localhost:5858)
run, wait for debugger to be hit
when you start meteor in your meteor app folder, you'll see that "debugger listening on port 5858" in console.
On Meteor 1.0.3.1 (update to Sergey.Simonchik answer)
Start your server with meteor run --debug-port=<port-number>
Point browser to http://localhost:6222/debug?port=<port-number>
Where <port-number> is a port you specify.
In your code add a debugger; where you want to set your break point.
Depending on where debugger; is invoked, it will either break on your client or server browser window with inspector opened.
I like to set breakpoints via a GUI. This way I don't have to remember to remove any debugging code from my app.
This is how I managed to do it server side for my local meteor app:
meteor debug
start your app this way.
Open Chrome to the address it gives you. You MAY need to install https://github.com/node-inspector/node-inspector (it might come bundled with Meteor now? not sure)
You'll see some weird internal meteor code (not the app code you wrote). Press play to run the code. This code simply starts up your server to listen for connections.
Only after you press play you'll see a new directory in your debugger folder structure called "app". In there are your meteor project files. Set a breakpoint in there one the line you want.
Open the local address of your app. This will run your server side code and you you should be able to hit your breakpoint!
Note: you have to reopen the inspector and go through this process again each time your app restarts!
As of Meteor 1.0.2 probably the best way for server-side debugging is directly via the new built-in shell: with running server run meteor shell. More info here: https://www.meteor.com/blog/2014/12/19/meteor-102-meteor-shell
I am not sure why it was not working for you.
I am able to use it by following steps on console (Mac).
$ ps
$ kill -s USR1 *meteor_node_process_id*
$ node-inspector &
Above steps are mentioned on https://github.com/dannycoates/node-inspector. It is for attaching node-inspector to running node process.
I wrote a small meteor package called meteor-inspector which simplifies the use of node-inspector to debug meteor apps. It internally manages the lifecycle of node-inspector and hence, the user does not need to restart the debugger manually after some files have changed.
For more details and concrete usage instructions take a look at https://github.com/broth-eu/meteor-inspector.
for meteor 1.3.5.2, run
meteor debug --debug-port 5858+n
n is a non-zero number, this will cause node-inspector use 8080+n as web port.
WebStorm, the powerful IDE free for open source developers, makes it much easier to debug server-side.
I've tested it on Windows, and the configuration was painless - see my answer.
A inspector that solve my issues is meteor server console. Here is the process I followed to install it:
In your project folder, add the smart package server-eval:
mrt add server-eval
For Meteor 1.0:
meteor add gandev:server-eval
Restart meteor.
Download crx Chrome extension file from here.
Open extensions page in Chrome and drag crx file to extensions page.
Restart Chrome.
Check the web inspector out to eval server side code:
In comparison with node-inspector, I have a clearer output.
If you prefer to use nodeJS' official debugger you can call NODE_OPTIONS='--debug' meteor and then (on a different shell) node debug localhost:5858.

Resources