How to clear the output time to time using function in PyCharm - python

I want to clear the screen time to time using function in Pycharm. When I tried to run the commands continuously, the screen gets full and seems clumsy.
I've tried the following commands used for CMD Mode and it is not working with Pycharm.
def clear():
os.system('cls')

As far as I am aware, Pycharm has no clear screen for it's console.
Whenever you restart the script that is running, the console will be cleared.
You can also do something like this,
print('\n'*80) # prints 80 line breaks, to emulate clearing the screen

Related

How can I clear a run window while the program is running?

I have a code, and I want to be able to clear the run window after certain actions are performed, however I haven't been able to find a way to clear it aside from clearing it yourself using the 'clear all' button(PyCharm btw). Is it possible to do that?
It seems that you can't clear the pycharm terminal without the clear button, however running it in cmd, the os.system('cls') or os.system('clear') works and will clear the screen.
You can't clear the pycharm window without the clear all button, but the os.system('clear') will clear the window if you run it in cmd instead so use that instead..

How to simulate the run button with a command line in Pycharm

I'm working on a script using the PyAutoGUI module. Sometimes the script gets stuck in a while loop because it's looking for pictures/images that are not shown due to connection problems etc. If this happens I want the program to start again from zero, so I want to simulate the play/run-button in Pycharm with a command line. Is this possible?
What it sounds like you want to do is restart your program if it doesnt respond. A similiar question appears to have been posted here: Python help - Need the ability to restart the script when it hangs or automatically set a timer so I would reccommend having a look at that. Simulating the run button in Pycharm might seem like a good idea at first but it is very specific and a bad practice to simulate user actions like that unless there is absolutely no viable alternative.
Thanks to Patel I managed to solve my issue. You can use ctrl+f5 to restart a script so now when I'm stuck in a while loop I'm using this code:
#Click toolbar Pycharm
pyautogui.moveTo(1672,15,1)
pyautogui.click()
#Rerun the script
pyautogui.hotkey("ctrl","f5")

How to pause script execution in PyCharm Community?

I wrote this piece of code and tried to debug it:
from time import *
for i in range (100):
sleep(1)
print(i)
I first run this script in debug mode, and try to pause it by clicking the pause button, but the pause button doesn't work at all, it just keep printing new numbers.
Then I directly run this script (not in the debug mode), the pause did stop pycharm from printing new numbers, but the script is actually still running in the background, when I resume the script, it prints a lot of numbers all of a sudden.
So how can I correctly pause the script execution?
I installed pycharm and python in a whole new windows 7, it still behaves like this.
The stop and rerun button works perfectly, breakpoints too. But the pause button never works.
The pause ("Pause Output") button only temporarily suspends output to the terminal - it has no effect on the script execution. You may wish to use debug mode with breakpoints instead.
You can add breakpoints into your program by clicking in the space to the left of the text editor (the "Left Gutter", where line numbers appear, if you have them enabled).
See the Pycharm documentation for more information.
Update 2022
We now have a pause button in debug mode :

How to pause program execution in Pycharm (pause button not working)?

While debugging my Python 3.5 pogram in Pycharm 5.0.4, I am trying to hit the pause button to find how why/where the program is hanging (as can be done in Visual Studio).
However, nothing happens: the pause button does not become grey and the resume button stays grey, and in the debugger tool window, "Frames are not available".
I tried with different basic programs, on Linux and on Windows, to no avail.
Is this a bug or am I missing something in how Pycharm debugging is supposed to work?
I also noticed that when a breakpoint is hit, only one thread is suspended and I could see no way to suspend other threads to inspect their stack frames. I would be interested to know how to achieve this thread-specific suspension as well.
Sounds like your program is hanging on a sleep or something of that sort, or maybe on some native code.
If it was a regular python loop the pause python would work.
I believe the problem is with python itself and not the debug tool you are using.
When you pause a python program you pause the interperter and so all threads that are running in the context of the interperter are paused and you can see the them in the frames window. Any thread that show the message "Frames not available in non-suspended state" is not suspended because it is was sleeping when you paused the program.
see this for how to debug c code
Not working python breakpoints in C thread in pycharm or eclipse+pydev
Within PyCharm, there is an option for debugging, that will allow you to step through your code, which may be of more use, rather than trying to pause the program.
You need to insert a break point in the code initially; just click in the grey bar at the line you want to break at:
Then you can press Alt+Shift+F9, or click Run > Debug in the menu to start stepping through the code from that point:
Once you have started the debug mode, click the button highlighted by the red circle - this will enable you to step through the code, looking at the variables, their assignments and if you receive any errors.
If you need to stop at any point, just hit the red Stop button on the left of the debug window.
The console tab will allow you to see what is being printed to the screen (if anything), and at what point, save you having loads of print statements like you would if debugging using Idle or similar IDEs
HTH

GUI program with toggable console?

I've seen some apps allow you to show/hide the console when you need to read log messages. For example Blender3D allows that (blender.org).
I was wondering if this can be done in Python and how.
My main window is a Panda3D (panda3d.org) window.
I've read somewhere that one option is to hide the "real" console (pythonw) and create another console and just redirect everything from the "real" one to it, every time you want to "show" the "real" console. No idea how this can be done.
Or at least a way to choose whether to start the program with the console or without it by reading a configuration file or something.
I'm assuming you are talking about Windows because this console toggling in blender is Windows exclusive. I'm guessing Blender uses GetConsoleWindow and ShowWindow on Windows.
This is how you could do it in python with pywin32:
import win32gui, win32console, win32api, win32con
import time
console_window = win32console.GetConsoleWindow()
time.sleep(1)
win32gui.ShowWindow(console_window, win32con.SW_HIDE)
time.sleep(1)
win32gui.ShowWindow(console_window, win32con.SW_SHOW)
time.sleep(1)
If you run this program with python and not pythonw it will show the console, sleep for a second, hide the console, sleep for another second and then hide it again.
Mind that this code only works on Windows. On other platforms silly stuff like this is not necessary because if you want a program to show a console then you run it from the console.

Categories