Is there a reset function in the Python console? - python-3.x

Is there a function that can be called from within the Python console that has the exact same effect as doing the following:
Calling
exit()
to exit the console, followed by calling
python
at the command prompt to re-enter a fresh python console.
NB: I am not seeking to just clear or de-clutter the screen, because using CTRL+L accomplishes that quite easily.

If you want to get rid of the variables defined in the session then this should do your job.
import gc
del <variable_name>
gc.collect()

Related

why is pdb on Python 3 ignoring my CTRL+C and just displaying "--KeyboardInterrupt--"

Under pdb in Python 2 I could CTRL-C and quit my Python program.
Under Python 3.6 I doesn't do that, just displays --KeyboardInterrupt-- and ignores me. In order to quit, I need to CTRL-D instead, but that will also terminate my bash session if I "do one too many".
Can I bring back CTRL+C behavior?
Python 3.6, macOS Sierra.
I still consistently have the same behavior. Maybe it’s a glitch in my environment but otherwise I can’t see the benefit of explicitly ignoring a user’s request to terminate a program with CTRL+C.
However, I have found that hitting ‘q’ triggers pdb quit which mostly ends up with the desired result, termination of the program. And, unlike CTRL+D has no risk of closing the launching bash session either.
edit - this did the trick 😀
(I have code that is made to trap exceptions, which is why a simple q/quit was hit or miss)
as per Is there a way to prevent a SystemExit exception raised from sys.exit() from being caught?
🧨 : note its warning about no cleanup code being run
# ~/.pdbrc
alias qq import os; os._exit(1)

How to exit one python script from another python script but keep the first python script running?

I've a GUI and I'm running another python script from GUI script. Now if user enters incorrect values I want to show error message and stop right there. Basically terminate second script at that point itself and not do anything further. But I'd like the first script (GUI) to stay open. How can I do this? Return function may not be very useful here because return function just stops the current function. I'm calling one function from another several times. Is there an easy way of doing this?
For tkinter you can use messagebox.
from tkinter import messagebox
messagebox.showerror("Error", "Message")
messagebox.showwarning("Warning", "Message")
messagebox.showinfo("Info", "Message")

I can't get pythons output viewer to run

I just downloaded python on a new pc and now whenever I try to create a script the output viewer just closes down immediately when I launch it as a python file.
My original script was
import secrets
secrets.token_hex(32)
but it just closes down immediately. I thought that it was something with my code so I tried to just make the simple "hello world" script.
(print) "I hope this work"
and I had the same result as the first script, the output window opened up then immediately closed down.
I can get it to work using the python shell but I prefer doing all my coding using notepad++ and it would a real pain in the behind if I can't get that to work.
This is expected behavior if you run your scripts as you described, by opening them in Explorer. Your script completes execution in a terminal window, then closes immediately.
If you absolutely insist on running them on double click and still want to see your console, I suggest you create a .bat file at your python path with contents like this:
python -i %1
and them bind your double-click handler to use that file on .py extension. That way, Python will execute your file and go into CLI mode, preserving your terminal window and allowing you to type further commands.
My other guess would be that you want a console plugin to work right within Notepad++, in that case use nppexec: https://sourceforge.net/projects/npp-plugins/files/NppExec/

Press enter stops computing python

I'm doing some engineering analysis with the help of a FEA program and Python. When the analysis ends I need to press a key to continue. But this is not a normal press any key to continue. Every code executed with the scripts stops. Like a handput debug break. Nothing runs until I press something or switch windows.
I cant use send keys and subprocesses because running code completely stops. Only solution I could come up with is to use another script in another command window with simple send keys command. This extra script is useless if computer is used or another window is active.
I'm a beginner level programmer and maybe I'm missing something simple. I guess the problem is caused by the FEA programs code but I'm not sure. So is there any way to prevent my code from stopping? Thank you for your time.
It seems that the FEA program does the windowing and you cannot do much about it. I actually automate scripting in DIANA FEA. For this program I would try something like pywinauto.
https://github.com/pywinauto/pywinauto
And call your python script from another python script.
from pywinauto import Desktop, Application
import time
app = Application().start("FEA_program.exe my_python_script.py")
while True:
time.sleep(5)
# send key presses to the app every arbitrary seconds

Turn off echo of getpass.getpass() in IDLE

I would like to enter a password into the IDLE terminal in Windows without an echo.
Normally entering passwords is possible with python using the function getpass.getpass(), but in IDLE there comes up the following warning message:
GetPassWarning: Can not control echo on the terminal.
Warning: Password input may be echoed.
I found out, this is because IDLE replaces the sys.stdin with a different object:
if sys.stdin is not sys.__stdin__:
return fallback_getpass(prompt, stream)
However, I could not find a solution. Does anybody have an answer or another way to enter a password into the IDLE terminal without an echo?
As an IDLE maintainer with some familiarity with IDLE internals, I do not believe that it is possible to suppress character display but not otherwise change behavior in response to input('prompt') without major changes to IDLE code.
But even that would be insufficient for getpass.getpass since it calls one of unix_getpass, win_getpass, or default_getpass, and the first two use system_specific low-level functions that bypass stdin.
In terms of design intent: IDLE is, as its name says, a program development environment. Developed programs normally, and sometimes must be, executed by Python directly, without going through IDLE. Python normally runs attached to a terminal window. IDLE's Shell is based on a tkinter Text widget, which is a multiline editor, not a terminal. This is why one can enter, recall, and edit complete multiline statements rather than only a single line at a time.

Resources