pydev debugger is a python debugger used by pydev and pycharm. It seems much more powerful than pdb. Its code is available (https://github.com/fabioz/PyDev.Debugger), and it is easy to install through pip. The package name is pydevd.
However, there is no information on how to use it from python code (outside of pydev or pycharm IDE). The documentation is completely lacking.
Does anyone know how to use it? I would like to do something like that:
import pydevd
pdd = pydevd.debug("python myscript.py")
pdd.set_break_point(file="myscript.py", lineno=12)
pdd.start()
pdd.read_variable("a")
pdd.continue()
well, I agree that the docs are lacking, but the idea is mostly that you'd use it from within the IDE, not programmatically.
The only public API you should use programmaticallyis pydevd.settrace(), which is the API which will setup a breakpoint at the place it's put programmatically (meaning the debugger will stop at that line -- the same effect of having pdb.set_trace() -- and that same API will also connect to the frontend for remote debugging if it's still not connected (i.e.: http://www.pydev.org/manual_adv_remote_debugger.html -- code: https://github.com/fabioz/PyDev.Debugger/blob/a4a58179dab9f9fb93559066f0ef22ac59c59e04/pydevd.py#L1065).
Now, currently the only frontends are PyDev and PyCharm, there's no frontend that gives you a command line... the whole communication happens by connecting to a thread in the debugger backend through a socket (again, there are no real docs, but the code should be easy to read on what the socket accepts and the protocol is pretty simple: https://github.com/fabioz/PyDev.Debugger/blob/a4a58179dab9f9fb93559066f0ef22ac59c59e04/_pydevd_bundle/pydevd_process_net_command.py).
There are unit-tests in pure-python code which exercise that (i.e.: connect to a debugger, and issue commands through sockets -- https://github.com/fabioz/PyDev.Debugger/blob/a4a58179dab9f9fb93559066f0ef22ac59c59e04/tests_python/debugger_unittest.py), so, it shouldn't be that hard to actually do a command-line frontend to it -- it's just that no one had interest on it until now, but if someone would like to contribute a command-line frontend to PyDev.Debugger, it'd be welcome ;)
Related
I have a server side Python script that imports a big package called nltk. It ran at a command prompt, but would not run in Apache server.
I tried the logging and put it as the first import and created a log file immediately after. But nothing is written to the file before the script crashes.
Is there a way to see the "ImportError: no module ..." when the script runs on Apache?
As far as I'm aware, there's no built-in way to tell Apache to simply dump error log messages directly to the browser. I'm betting the error_log output happens in an independent part of the Apache pipeline -- just a guess.
Most of the time when someone wants output errors directly to the browser, that person is a developer working in a web script such as PHP, Python, Perl, or other lang. In order to output error messages that occur within your lang interpretter, you have to format the output and pass it through apache as if it was normal output.
PHP
Some languages provide an easy way to do this, such as the well known error_reporting switch in PHP.
Python
Unfortunately some languages, make this painful or almost impossible and Python is one of them.
At one time this was apparently possible using https://docs.python.org/3/library/cgitb.html
However that library has been deprecated. Seems like the Python community doesn't have much love for developers who want to get immediate feedback about errors in the browser. :(
I'm using Jayamanne's Python version 0.7.0 running on VS Code. The documentation for attaching the debugger to a running process requires adding extra code and configuring ports and addresses. I figure there must be a simpler way by simply selecting or specifying a process name or id.
Apparently, VS Code is capable of it based on the Node.JS documentation and demo. The Python interpreter is also capable of it, since it is how I normally debug applications on Visual Studio 2017.
How do I configure VS Code Python to attach to a process by name or id? Is this possible? If not, why doesn't the documentation say it explicitly?
There's no "easy" way. Follow the documentation you mentioned about "adding extra code and configuring ports and addresses".
You can only "attach" a C debugger to any process because Windows Debugging API has this functionality and provides all the necessary machinery working behind the scenes to make this seemingly easy operation possible (stopping the target process with OS means, creating a helper thread in it, manipulating its memory (incl. replacing machine code with int 3 to set a breakpoint) -- while Visual Stidio uses the source code and PDB files to parse the memory data into C- or C++-level constructs).
This isn't so for Python: while you can likewise break into the process at C level and manipulate an interpreter instance from the helper thread with the C API if you somehow get its address, there's no way provided in that API to make it break from the existing code passed to it and start executing your instructions instead while still being able to access the existing code.
pdb gets you into a debugging session via the "existing code": its sets the sys.settrace callback that the interpreter calls at certain moments to be able to bring up its console at breakpoints and/or wraps your program with itself and catches exceptions.
The debugger you're describing acts the same: only instead of bringing up a console prompt, its machinery acts as a server, accepting commands from a connected IDE client via a socket.
I started develop python / django code recently.
When I do it on my local machine (Ubuntu desktop), I use PyCharm, which I like a lot, because I can set up break points, and, when an exception occurs, it shows me the line in the code to which that exception relates.
Are there any tools available for python / django development in the environment without a graphic UI, which would give me break point and debugging features?
I know, that VIM with the right settings is an excellent coding tool, but I have no idea if it could be used for interactive debugging.
I use PyCharm, which I like a lot, because I can set up break points,
and, when an exception occurs, it shows me the line in the code to
which that exception relates.
Once you configure it correctly, PyCharm supports remote debugging so if you are happy with PyCharm keep using it.
I think the best practice is developing locally, and use Pycharm "sync" to deploy the code from local to remote. If you prefer to code without GUI or in the console mode, you could try "emacs+jedi", it works well in the console mode.
For debug, the best choice is pydev/pdb.
import pdb and then place pdb.set_trace() where you want to start debugging. I find it to be the best tool for debugging Django apps. Use "n" to go to next line "s" to step into a function and "c" to continue. See https://docs.python.org/2/library/pdb.html for more info.
You should also check out runserver plus and Werkzeug from https://github.com/django-extensions/django-extensions
I am attempting to build an educational coding site, similar to Codecademy, but I am frankly at a loss as to what steps should be taken. Could I be pointed in the right direction in including even a simple python interpreter in a webapp?
One option might be to use PyPy to create a sandboxed python. It would limit the external operations someone could do.
Once you have that set up, your website would take the code source, send it over ajax to your webserver, and the server would run the code in a subprocess of a sandboxed python instance. You would also be able to kill the process if it took longer than say 5 seconds. Then you return the output back as a response to the client.
See these links for help on a PyPy sandbox:
http://doc.pypy.org/en/latest/sandbox.html
http://readevalprint.com/blog/python-sandbox-with-pypy.html
To create a fully interactive REPL would be even more involved. You would need to keep an interpreter alive to each client on your server. Then accept ajax "lines" of input and run them through the interp by communicating with the running process, and return the output.
Overall, not trivial. You would need some strong dev skills to do this comfortably. You may find this task a bit daunting if you are just learning.
There's more to do here than you think.
The major problem is that you cannot let people run arbitrary Python code on your webserver. For example, what happens if they do
import os
os.system("rm -rf *.*")
So clearly you have to run this Python code securely. But then you have the problem of securing Python, which is basically impossible because of how dynamic it is. And so you'll probably have to run the Python shell in a virtual machine, which comes with its own headaches.
Have you seen e.g. http://code.google.com/p/google-app-engine-samples/downloads/detail?name=shell_20091112.tar.gz&can=2&q=?
One recent option for this is to use repl.
This option is awesome because the compilers are made using JavaScript so the compilation and execution is made in the user-side, meaning that the server is free of vulnerabilities.
They have compilers for: Python3, Python, Javascript, Java, Ruby, PHP...
I strongly recommend you to check their site at http://repl.it
Look into LXC Containers. They have a pretty cool api that you can use to create lightweight linux containers. You could run the subprocess commands inside that container that way the end user could not mess with your main server.
I am developing a twisted server. I need to control the memory usage. It is not a good idea to modify code, insert some memory logging command and restart the server. I think it is better to use a "remote console", so that I can type heapy command and see the response from the server directly. All I need is a remote console, I can build one by myself, but I don't like to rebuild a wheel. My question is: is there already any remote console for twisted?
Thanks.
twisted.manhole.telnet uses the deprecated module twisted.protocols.telnet. It is recommended to use twisted.conch.manhole instead.
Here are some tutorials of how to use it:
Writing a client with Twisted.Conch -- twisted.conch documentation
Network programming with the Twisted framework, Part 4 -- IBM developerWorks
Twisted Network Programming Essentials - Chapter 10 -- Online book preview
Take a look at twisted.manhole