break points in pycharm 2020.3 - debugging

I am new to Pycharm and I have the below code with a breakpoint on 'print' (I have more code underneath this break point). When i run on debug, previously it would bring me to the debugger window to show me my variables up to the breakpoint. Now it doesn't and it stays on the console view and i have to manually click on the debugger view. Can someone please help me?
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
print() # breakpoint is here
I have attached a screenshot below with the console window circled at the bottom once i run the code. I need it to go to debugger when i run the code in debug (shortcut = ^D)

Related

Spyder debugger freezes on second restart of code

My task is to repeatedly rerun a code from beginning. As I do not want to run the code to the end I set a breakpoint() command. However, Spyder freezes when the code is the second time called from debug mode. For demonstration a simple program is sufficient:
print('Hello World!')
breakpoint()
After launching Spyder I start the small program by F5. Everything works as expected and I jumped into the debug mode. Now I press again F5 and the code restarts correctly from the debug mode. After again pressing F5 the code is not restarted and instead the IDE freezes. It is not possible by any means to rerun the code again from debug mode. Interrupting the kernel by pressing the red button (see screenshot below) does not help. Trying to leave the debug mode by !q does not leave the debug mode. The only possibility to rerun the code is quite cumbersome, i.e. I have to restart the kernel (Ctrl+.) that takes a while. How to debug code under these circumstances?
Is this a bug of the IDE or expected behavior? How can I restart the code by a simple key press as often as possible from debug mode?
This is a screenshot of the Spyder console with comments in red added.
I use a recent version a WinPython with latest Spyder 5.4.0, Python 3.10.4 64-bit, Qt 5.15.2, PyQt5 5.15.6, Windows 10Pro Version 21H2. The problem also appears with Spyder 5.3.3 A similar problem (or maybe the same) appears with a different Python version.
After many trials, I noticed that there is something strange with IPython console. I noticed that when it hangs after running a code, if I delete all user variables, it worked fine.
Then I tryed to delete all variables before execution, and it work fine.
Therefore I discovered that a solution that worked for me is to go to preferences -> Run -> and untick the option 'Remove all variables before execution'
It is quite annoying because I have to do it manually every time before running, but in this way the spyder does not appear to hang anymore! I hope that the Spyder developers will solve it soon.
-
I automatically solved by typing at the beginning of any script these lines, inspired from the question Code to clear console and variables in Spyder :
try:
from IPython import get_ipython
get_ipython().magic('clear')
get_ipython().magic('reset -f')
import matplotlib.pyplot as plt
plt.close('all')
except:
pass
similar to Matlab in which you normally start your code with
clc
close all
clear all

Why is my Pycharm debug console not loading?

I recently installed Pycharm on a fresh PC. Unfortunately something seems to be off with the installation. When I try to enter the debug console this isn't loading. E.g. for a very simple test case
def test_stable():
1 == 1
2 == 2 # breakpoint in this line
when I add a breakpoint in the last line, it is stopping the execution correctly, but I can't enter the console (i.e. the little wheels keep spinning):
What is wrong with my installation here? Is this a known issue?
Edit:
When clicking the "Show Python Prompt" button a console opens, but the console is not working, there is no output from it:
As I said above I believe this is connected to the strange behavior in the Debug Console tab.
Edit 2:
As recommended by #bad_coder I changed the test to
class Test:
def test_trivial(self):
test = 5
assert 1 == 1 # breakpoint on this line
I still can't use the debug console. I also just started up my old system and can now confirm that there when clicking on console I am directly getting to the console and not the situation of my first image (with the animated spinning wheels).
The button you're looking for is the Python-with-a-prompt button, which switches between the console and test progress.
If someone stumbles across the same issue, I believe the problem here was basically just that the Pycharm version was rather old. After downloading the current version everything works fine.

How to run/test code in visual studio code?

How can I run/text code in visual studio code? What I mean if, for example, I have the following code:
i = 0
while i < 5:
print("test")
i += 1
then I would like to get the output:
test
test
test
test
test
I used jupyter notebook before and this was possible but I can't do this in vscode and they're forcing me to use vscode.
Press F5. You can set a breakpoint by clicking to the left of you line number. See VS Code Debugging for more information. You'll need to install the ms-python extension to get python support (see the above link). Naturally, you can simply invoke your compiler in the terminal as well (Control+` to bring up terminal).

PySide window appears below other windows

I have a PySide app that needs to run as admin. I've used a technique recommended by this post.
import os
import sys
import time
from PySide.QtGui import *
from win32com.shell import shellcon
import win32com.shell.shell as shell
import win32con
def force_elevated():
try:
if sys.argv[-1] != 'asadmin':
script = os.path.abspath(sys.argv[0])
params = ' '.join([script] + sys.argv[1:] + ['asadmin'])
shell.ShellExecuteEx(nShow=win32con.SW_SHOWNORMAL,
fMask=shellcon.SEE_MASK_NOCLOSEPROCESS,
lpVerb='runas',
lpFile=sys.executable,
lpParameters=params)
sys.exit()
except Exception as ex:
print ex
class MyGui(QWidget):
def __init__(self):
super(MyGui, self).__init__()
self.show()
app = QApplication(sys.argv)
force_elevated()
splash = QSplashScreen("logo.png")
splash.show()
time.sleep(2)
gui = MyGui()
app.exec_()
Windows UAC pops up and asks the user to allow admin rights. If the user selects yes, a new instance of the application starts up in admin mode. If the user selects no, the try statement fails, and the program continues to run in non-admin mode.
The problem is, when running in non-admin mode, the application starts up behind other windows, so it's easy to think it's not running at all. After using cx_freeze to make an executable, it can also happen even when you do activate admin mode.
I can't have the window always be on top. I've tried temporarily setting it as always on top (to bring it to front) and then turn it off, but this does not work. I've also tried using various libraries and built in functions to locate the process ID and tell windows to bring it to front. They either haven't worked at all, or don't work reliably.
Does anyone have a reliable way to make sure the PySide window appears on top?
You can bring the window to the front with gui.raise_()

Return to normal execution after debugging a part of code

When I debug a program using IntelliJ' debugger, how can I return to the normal execution of the program after debugging a part of the code ?
I start a program, test it.
When the debugger find breakpoints, it starts the step by step debugging of the program.
I do my stuff and my checks, nothing goes wrong.
I want to exit step by step debugging and go back to a fluid execution of the program (without having to press a button for all actions) until the debugger find a new breakpoint.
How can I do that and exit the step by step debugging?
You can use the "Resume Program" button, which looks like a green triangle (a "play" button"), or the F9 shortcut.
See, e.g., this screenshot from a debugging session I started (mouse cursor and the hovering tooltip show the button's location):
Note: The screenshot was taken in IntelliJ IDEA 14.1.4 so the exact layout may differ according to your specific version, but the same behavior, buttons and shortcuts have existed in IntelliJ for years now.

Resources