I've noticed how easy it is to debug a Python script from Eclipse. Simply set breakpoints and run a Python script from the debug menu. But is it possible to start a Python Interactive Interpreter instead of running a particular Python script, whilst still having Eclipse breaking on breakpoints? This would make it so much easier to test functions.
Thanks for any help
Still looking for a simple/ish way to start the debugger in Eclipse->PyDev that lets me use the interactive debugger. None of the answers as of yet is acceptable
You can explicitly write code to setup a breakpoint in your script, and then "remote debug". That means having pydevd in the pythonpath of your script wherever it is running, and running the eclipse pydev remote debugger on your devbox. If it's all happening on the same machine, this is fairly straightforward.
If not, you'll need to specify the hostname of the dev machine running the python remote debugger in the call to settrace(). You'll also need pydevd available on the machine running the script.
I have got this working in the past without having to install eclipse+pydevd on the machine running the script. It's not totally straightforward, and if you go that route I'd recommend checking that the pydevd versions match or at least you know they're compatible. Otherwise you end up wasting time debugging the debugger.
For details see: Pydev Remote Debugger
what about this, in the script, you can write a function, say onlyForTest, then each time you write a new function and want to test it, you can just put it in the onlyForTest function, then specify some arguments required by the new function you just write, then open the interactive python shell, import the script, call the onlyForTest function, check out the result.
Related
I just started using python and pycharm. I'm a bit confused of what is the run configuration in pycharm do and what is the different between the just run ?
A run configuration (not just in PyCharm, for example JetBrains IntelliJ also has them, in fact most IDEs have this concept) is a compilaton of settings to be used when running a program.
Let us stay with Python for the sake of simplicity. You might think that when you execute your script by typing in your command prompt...
python myscript.py
...that there are no settings or configuration involved. You are just running your script, right?
Not quite, you are in fact using what you could call an implicit run configuration, i.e. are whatever defaults and environment settings happen to take effect.
Some examples you will also find in PyCharm Python run configurations:
Script path is just the script you are calling, in the example myscript.py since we specified that on the command line.
Python interpreter is whatever Python interpreter is first in your path.
Parameters is empty in our example, as we didn't specify any on the command line.
Working directory is the current directory, where we are with our command prompt.
Enviromnent variables are those that happen to be set in our shell.
All of these and more can be defined in a run configuration (or multiple different run configurations if you need) for your project.
You can then select these conveniently from the dropdown menu, and the one currently selected will be used to execute your program when you press the green play button.
What is the difference between using a run configuration and just run in PyCharm?
If you just run your program, you are telling PyCharm that it should just use the project default configuration for the specific file type.
In other words, you are using a run configuration as well there, just the unmodified default configuration.
I understand that I can set which debugger is launched by setting sys.breakpointhook(), but what do I set it to in order to launch PyCharm's IDE?
To clarify, I want PyCharm's debugger to launch when it encounters the breakpoint() builtin, especially if I'm running the program from PyCharm.
If you're wondering "why ever do this when you could just run from the debugger?" I'm trying to debug some code that responds differently when started with the debugger.
PyCharm uses the pydevd debugger https://pypi.org/project/pydevd-pycharm/. So assuming you want to debug a Python application started outside of PyCharm, you have two options:
either attach the process, see https://www.jetbrains.com/help/pycharm/attaching-to-local-process.html#attach-to-local or
start Pycharm as debug server and than connect to it from your application, see https://www.jetbrains.com/help/pycharm/remote-debugging-with-product.html.
The second option is probably closer to what you have in mind, i.e. instead of setting the debugger using sys.breakpointhook, you install the debugger using pydevd_pycharm.settrace(...) which then connects to PyCharm (where you can set the breakpoints in the source files).
Installing the custom breakpointhook by pointing sys.breakpointhook to the pydevd breakpointhook (i.e. pointing to the "Pycharm Debugger") will be done by pydevd (https://github.com/fabioz/PyDev.Debugger/blob/37d804c7ac968694ce29c93392e3bed6fda641f0/pydevd.py#L95, line 117 to be precise).
Ueli's answer got me 99% of the way there. Here's the summary.
If you want the PyCharm debugger to start, all you need to do is import it. There are a couple ways:
You can use the pydevd-pycharm.egg from the PyCharm installation (<PyCharm directory>/debug-egg/pydevd-pycharm.egg) or install the pydevd-pycharm package using pip.
If you're running things locally, the simplest would just be to add
sys.path.append("<PyCharm directory>/debug-egg/pydevd-pycharm.egg")
import pydevd_pycharm
somewhere in the program you are debugging.
If you're Trying to debug on a remote machine, you will need to install pydevd-pycharm:
pip install pydevd-pycharm~=<version of PyCharm on the local machine>
before adding
import pydevd_pycharm
Somewhere in the program.
The instructions for how to import pydevd_pycharm is coppied from step 4 of https://www.jetbrains.com/help/pycharm/remote-debugging-with-product.html#remote-debug-config (as linked in Ueli's answer).
I'm currently trying to debug issues in Caffe for Windows PyCaffe.
Because of a bug in Python Tools for Visual Studio, PTVS doesn't work so I'm using PyCharm and trying to attach to PyCaffe's process through Visual Studio 2013. That is, I'm running PyCharm debugger on a Python script with a breakpoint set at the point where I call the Python entry point into PyCaffe.
I debug the Python script in PyCharm which calls modules written in C++ in VS. I want to debug those modules in C++. So I'm trying to attach to the PyCharm or Python processes with breakpoints set in VS.
The problem is that the breakpoint isn't firing at the entry point in PyCaffe in the Visual Studio C++ code.
Has anyone successfully gotten this kind of thing to work or is there an alternative way of doing this?
I faced a similar problem a few years ago, trying to
debug a user-mode driver wish was loading automatically from a RPC, the solution I found was:
Download Debugging Tool for Windows (depending on your system x86 or x64), wish is free,from the Microsoft Download.
Add "__asm int 3"(this is asm instruction for a hardware breakpoint) in the place on the C code where you whant to the break point to take effect.
Compile the C program, nomally with debug info (this part is important,because help the Debugger to find the source file), this should not make any problems.
Attach the Debugger to the running process (in my case was the explorer process), in your case should be the process that make first call to the library, for example if PyCharm create a process for the new python code, the debugger need to be attached to that process(you can manage this by setting a breakpoint in python, wish will give you time for doing the attach process), the easy way for me to figure this out was to let the process crash, because "__asm int 3" cause a process crash if a debugger is not attach, you can take advantage of this waiting for the crash and when windows shows you the screen "Process ... has detected a problem an need to be closed", you know who is the process that you are looking for.
This could be painful the first time but for me quite effective, because you
can see the data flow from one program to another,
We attach to one process and allow to set breakpoints within code that has not started from the VS debugger. But one important issue is that we often debug/run the app in the VS, for example, we debug the Web code that runs under IIS, we will attach to the IIS process or others.
Your project is different from the above sample, you run/debug your app in Pycharm (not the VS), but you want use the VS Attach to process function, so it would have a limitation. As you said that you debug script in PyCharm, and want to call C++, so you would check that whether the PyCharm supports a similar feature like the attach tool in VS.
I installed pydev and eclipse to compile a django project. Everything looks good but one thing makes me crazy. When i change something in the code, i click to save it and hope to see the effect of changes. However, I cannot see the effect of what i change unless I terminate the program and re-run as as shown below. It is such a pain...
I used to use Pycharm, but it expired. In Pycharm, when the program runs once, i do not need to run it again and again. I can easily see the effect of my changes on the code by clicking save button. Is it possible to see the same thing in pydev and eclipse? Do you guys also see this issue?
To debug Django with the autoreload feature, the Remote Debugger must be used and a patch must be applied to your code (just before the if _name_ == "_main_": in your manage.py module):
import pydevd
pydevd.patch_django_autoreload(
patch_remote_debugger=True, #Connect to the remote debugger.
patch_show_console=True
)
So, doing that, starting the remote debugger and making a regular run should enable all the regular breakpoints that are put inside Eclipse to work in the Django process with the Remote Debugger (and the --noreload that PyDev added automatically to the launch configuration must be removed).
I have plans on improving that so that the debugger will automatically add tracing to spawned processes (probably for the next release), but on PyDev 3.3.3 this still requires doing this manual patch and using the remote debugger.
The above is related to a debug run. Now, on to the regular run...
When you do a run as > pydev: django, it should create a run configuration (which you can access at run > run configuration). Then, open that run configuration > arguments and remove the '--noreload' (leaving only the 'runserver' argument).
Then, you can simply rerun it with Ctrl+F11 (if you've set it to launch the previously launched application as indicated in http://pydev.org/manual_101_run.html)
-- (Or alternatively you can run it with: Alt + R, T, 1).
The only problem is that if you kill the process inside Eclipse it's possible that it leaves zombie processes around, so, you can use the pydevd.patch_django_autoreload(patch_show_console=True) as indicated above to open a console each time it spawns a new process (you can do a pydevd|<- ctrl space here to add an import to pydevd).
Note that this should work. If it's not working, make sure you don't have zombie processes around from a previous session (in windows you can do: taskkill /F /IM python.exe to make sure all Python processes are killed).
Another note (for when you don't actually have automatic reload): Ctrl+Shift+F9 will kill the currently running process and re-run it.
EDIT: I got it working, I went into the pycassa directory and typed python pycassaShell but the 2nd part of my question (at the bottom there) is still valid: how do I run a script in pycassaShell?
I recently installed Cassandra and pycassa and followed the instruction from here.
They work fine, except I cant get pycassaShell to load. When I type pycassaShell at the command prompt, I get
'pycassaShell' is not recognized as an internal or external command,
operable program or batch file.
Do I need to set up a path for it?
Also, does anyone know if you can run ddl scripts using pycassaShell? It is for this reason that I want to try it out. At the moment, I'm doing all my ddl in the cassandra CLI, I'd like to be able to put it in a script to automate it.
You probably don't want to be running scripts with pycassaShell. It's designed more as an interactive environment to quickly try things out. For serious scripts, I recommend just writing a normal python script that imports pycassa and sets up the connection pool and column families itself; it should only be an extra 5 or so lines.
However, there is an (undocumented, I just noticed) optional -f or --file flag that you can use. It will essentially run execfile() on that script after startup completes, so you can use the SYSTEM_MANAGER and CF variables that are already set up in your script. This is intended primarily to be used as a prep script for your environment, similar to how you might use a .bashrc file (I don't know of a Windows equivalent).
Regarding DDL statements, I suggest you look at the SystemManager class.