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).
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 configured Pydev in eclipse and also installed behave and cucumber to run a sample project in BDD approach.
Each time i right click on the feature file (dirst_behave.feature) and Run as "Cucumber feature", i get the error -
"Launch configuration first_behave.feature references non-existing project python_behave_template-master."
I tried remapping the Python interpreter, but no luck. Any help in getting this issue resolved would be greatly appreciated.
I was using Pycharm IDE to run python scripts, but Behave approach doesn't work in Pycharm community version.(Screenshots attached for reference)
enter image description here
Each time i right click on the feature file (dirst_behave.feature) and Run as "Cucumber feature", i get the error -
"Launch configuration first_behave.feature references non-existing project python_behave_template-master."
I was using Pycharm IDE to run python scripts, but Behave approach doesn't work in Pycharm community version.(Screenshots attached for reference)
You can't run the *.feature script directly from Pycharm IDE.
Try to run your script from terminal:
behave feature/first_behave.feature
Behave works in Eclipse as well as in PyCharm (CE). But there is more handy work involved. Both IDEs allow you to run a generic command. Therefore, it should be possible to run it. This is especially needed when you need a debugger to diagnose the behave test run (but you normally need to provide your own script that imports the behave main() function same as the behave-script does).
PyCharm runs an interactive Python console (IPython in my case), but when I make changes in the code, PyCharm doesn't reimport the modules I've been editing, so the console runs the old code.
More so, if I have an old package installed via Run setup.py Task, Python imports the old one after import mymodule as mm in the console.
One workaround is to edit the code in a file and rerun it without the interactive console, but that's not a very elegant solution.
How can I keep the interactive console up-to date and update modules on-the-fly?
I'm using Python 3.4.3 in Pycharm 4.0.6 currently, and building on Anton's answer above, I add the following to my Starting script:
from importlib import reload
then you can simply use 'reload(my_module)' as you please - however this would be manual for each reload and I'm not aware of a fully automated solution if that's what you're really after.
IPython has a very nice function that recursively reloads all modules. It is called dreload, from deep reload. You just have to manually run in the console:
dreload(myCoolModule)
It is manual, but it doesn't bothers me. If I remember well, it doesn't work if you remove the namespace importing like: from myCoolModule import *
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.
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.