I am trying to debug some c++ codes called from a python program (through python-c++ binding). My original approach was to start ipython, open lldb and attach the ipython process in lldb. Then go to ipython to run my python program. This works fine.
Now I want to take advantages of CLion IDE by attaching the ipython process inside CLion. However, When I did the attach process from CLion, it seems to attach to the process to the pydev debugger like below instead of attach the process to lldb:
Attaching to a process with PID=18016
/Library/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python /Applications/CLion.app/Contents/plugins/python/helpers/pydev/pydevd_attach_to_process/attach_pydevd.py --port 51406 --pid 18016
Connected to pydev debugger (build 183.5429.37)
However, I want the process to be attached to lldb, not pydev_debugger.
How do I force the attach_process in CLion to attach the process to lldb instead of pydev_debugger? Thanks!
There must be two entries in the attach dialog corresponding to the target process in question: attach using the pydev debugger and the native debugger. You may try filtering the list by the process PID in order to check that.
Please find a similar report in the CLion issue tracker here:
Inside the "Attach to Local Process..." dialog there're two kinds of processes you're able to attach to: one comes from CLion (attach with LLDB or GDB), another is provided by the Python plugin (attach with Python Debugger). Since the process you intend to attach to is a Python interpreter, the latter (the Python debugger) is suggested by default, while you should rather use the former (the CLion debugger) for debugging C/C++ code.
Related
I'm using Linux Eclipse (pydev) as IDE to develop python scripts that are launched by an application written in C++. I can debug the python script without problems in the IDE, but the environment is not real (the C++ program sends and receives messages through the stdin/stdout and it's a complex communication channel that I can't fully reproduce writing the messages by hand).
Until now I was using log messages to debug (poor man's debug) but it's getting too complex. When I do something similar in PHP I can just leave xdebug listening and add breakpoints in Netbeans. Very neat and easy. Is it possible to do something like that in Python 3.X (with Eclipse or other IDE)?
NOTE: I know there is a Pydev / Attach to Process functionality, but it doesn't work. Always fails to attach.
NOTE2: There is also a built-in "breakpoint()" in Python 3.7 but it links to a debugger and if also fails, the IDE never gets the control.
After some research, this is the best option I have found. Without any other solution provided, I post it just in case anyone has the same problem.
Python has an integrated debugger: pdb. It works as a module and it doesn't allow to use it if you don't have the window control (i.e. you launch the script).
To solve this there are some coders that have created modules that add a layer on pdb. I have tried some and the most easy and still visual interesting is rpudb (but have a look also to this).
To install it:
pip3 install https://github.com/msbrogli/rpudb/archive/master.zip
(if you install it using the pip3 install rpudb command it will install an old version only valid for python 2)
Then, you use it just adding an import and a function call:
import rpudb
.....
rpudb.set_trace('127.0.0.1', 4444)
.....
Launch the program and it will stop in the set_trace call. To debug it (and continue) open a terminal and launch a telnet like this:
telnet 127.0.0.1 4444
You will have a visual debugger in front of you with the advantage that you can not only debug local programs, but also remote (just change the IP).
I was able to attach PyCharm to a running python process and use break points using PyCharm attach to process
I created a bash script which exec a python script, should work the same with C++
I am writing Python scripts in Pycharm with IPython installed. So I can use Python Console in Pycharm to type Python commands and check the immediate output of the codes. However, when I run a script file after pressing 'Run' button (Shift+F10), all the variables and functions are not visible to the Python Console. This is, however, the feature of Spyder, another popular Python IDE. So here is my question: how can I configure Pycharm so that running a Python script file is visible for Python Console? Thanks
You could also run the part of your code you want to test/check in the console by selecting it and then right clicking and clicking on "Execute Selection in Console Alt-Shift-E". That's what I use sometimes when the debugger is not helpful. After running the code (you can also just "run" functions or classes) the console knows the functions and you can use the same features that Spyder has. However, be aware that when you change the code you need to run it in the console once to update the console definitions!
You can not. But you can use pdb (which will break code execution where you need it and you will be able to do the same things, as in the Python Console).
And, which is better and more powerful, you can use PyCharm's debugger. It represents all available variables in tree-like structures and is really handy.
I am using PythonQT to execute python script (because I need to call c++ methods from python script)
My winpdb version is 1.4.6 and machine is CetOS 6.5
Now I want to enable debugging in python script
I have added rpdb2.start_embedded_debugger('test') inside the script and called PythonQt.EvalFile() function, now script is waiting for debugger.
I have opened winpdb UI from terminal and attached to the debugger. I am able to do the “Next”, “Step into” etc. and all local variables are visible correctly
But when I am trying to detach the debugger it is not detaching. Status showing “DETACHING” and nothing happening and I cannot even close winpdb. The only way to exit is kill winpdb.
If I run the same script file from terminal it is working properly (python ) and detaching as expected.
By looking the logs I have found that ,If I run from terminal then the debug channel is encrypted but when from PythonQt debug channel is NOT encrypted, not sure this have any relation with detaching
By further looking into rpdb2.py code I have found that Winpdb is hang on the line self.getSession().getProxy().request_go(fdetach) of request_go(self, fdetach = False): in rpdb2.py
The port 51000 is still in established mode
Please advise me on this.
Finally i have fixed the issue by using the latest version of python QT
I know how to set-up run configurations to pass parameters to a specific python script. There are several entry points, I don't want a run configuration for each one do I? What I want to do instead is launch a python script from a command line shell script and be able to attach the PyCharm debugger to the python script that is executed and have it stop at break points. I've tried to use a pre-launch condition of a utility python script that will sleep for 10 seconds so I can attempt to "attach to process" of the python script. That didn't work. I tried to import pdb and settrace to see if that would stop it for attaching to the process, but that looks to be command line debugging specific only. Any clues would be appreciated.
Thanks!
You can attach the debugger to a python process launched from terminal:
Use Menu Tools --> Attach to process then select python process to debug.
If you want to debug a file installed in site-packages you may need to open the file from its original location.
You can to pause the program manually from debugger and inspect the suspended Thread to find your source file.
I'd like to call a separate non-child python program from a python script and have it run externally in a new shell instance. The original python script doesn't need to be aware of the instance it launches, it shouldn't block when the launched process is running and shouldn't care if it dies. This is what I have tried which returns no error but seems to do nothing...
import subprocess
python_path = '/usr/bin/python'
args = [python_path, '&']
p = subprocess.Popen(args, shell=True)
What should I be doing differently
EDIT
The reason for doing this is I have an application with a built in version of python, I have written some python tools that should be run separately alongside this application but there is no assurance that the user will have python installed on their system outside the application with the builtin version I'm using. Because of this I can get the python binary path from the built in version programatically and I'd like to launch an external version of the built in python. This eliminates the need for the user to install python themselves. So in essence I need a simple way to call an external python script using my current running version of python programatically.
I don't need to catch any output into the original program, in fact once launched I'd like it to have nothing to do with the original program
EDIT 2
So it seems that my original question was very unclear so here are more details, I think I was trying to over simplify the question:
I'm running OSX but the code should also work on windows machines.
The main application that has a built in version of CPython is a compiled c++ application that ships with a python framework that it uses at runtime. You can launch the embedded version of this version of python by doing this in a Terminal window on OSX
/my_main_app/Contents/Frameworks/Python.framework/Versions/2.7/bin/python
From my main application I'd like to be able to run a command in the version of python embedded in the main app that launches an external copy of a python script using the above python version just like I would if I did the following command in a Terminal window. The new launched orphan process should have its own Terminal window so the user can interact with it.
/my_main_app/Contents/Frameworks/Python.framework/Versions/2.7/bin/python my_python_script
I would like the child python instance not to block the main application and I'd like it to have its own terminal window so the user can interact with it. The main application doesn't need to be aware of the child once its launched in any way. The only reason I would do this is to automate launching an external application using a Terminal for the user
If you're trying to launch a new terminal window to run a new Python in (which isn't what your question asks for, but from a comment it sounds like it's what you actually want):
You can't. At least not in a general-purpose, cross-platform way.
Python is just a command-line program that runs with whatever stdin/stdout/stderr it's given. If those happen to be from a terminal, then it's running in a terminal. It doesn't know anything about the terminal beyond that.
If you need to do this for some specific platform and some specific terminal program—e.g., Terminal.app on OS X, iTerm on OS X, the "DOS prompt" on Windows, gnome-terminal on any X11 system, etc.—that's generally doable, but the way to do it is by launching or scripting the terminal program and telling it to open a new window and run Python in that window. And, needless to say, they all have completely different ways of doing that.
And even then, it's not going to be possible in all cases. For example, if you ssh in to a remote machine and run Python on that machine, there is no way it can reach back to your machine and open a new terminal window.
On most platforms that have multiple possible terminals, you can write some heuristic code that figures out which terminal you're currently running under by just walking os.getppid() until you find something that looks like a terminal you know how to deal with (and if you get to init/launchd/etc. without finding one, then you weren't running in a terminal).
The problem is that you're running Python with the argument &. Python has no idea what to do with that. It's like typing this at the shell:
/usr/bin/python '&'
In fact, if you pay attention, you're almost certainly getting something like this through your stderr:
python: can't open file '&': [Errno 2] No such file or directory
… which is exactly what you'd get from doing the equivalent at the shell.
What you presumably wanted was the equivalent of this shell command:
/usr/bin/python &
But the & there isn't an argument at all, it's part of sh syntax. The subprocess module doesn't know anything about sh syntax, and you're telling it not to use a shell, so there's nobody to interpret that &.
You could tell subprocess to use a shell, so it can do this for you:
cmdline = '{} &'.format(python_path)
p = subprocess.Popen(cmdline, shell=True)
But really, there's no good reason to. Just opening a subprocess and not calling communicate or wait on it already effectively "puts it in the background", just like & does on the shell. So:
args = [python_path]
p = subprocess.Popen(args)
This will start a new Python interpreter that sits there running in the background, trying to use the same stdin/stdout/stderr as your parent. I'm not sure why you want that, but it's the same thing that using & in the shell would have done.
Actually I think there might be a solution to your problem, I found a useful solution at another question here.
This way subprocess.popen starts a new python shell instance and runs the second script from there. It worked perfectly for me on Windows 10.
You can try using screen command
with this command a new shell instance created and the current instance runs in the background.
# screen; python script1.py
After running above command, a new shell prompt will be seen where we can run another script and script1.py will be running in the background.
Hope it helps.