Python Screen closing when i run test.exe file - python-3.x

Hi all iam new to Python, I have created test.py script then i made exe using "pyinstaller --onefile SNMP_TeamDynamicCheckV1.py " , now i could see "test.exe file in "C:\Python34\Scripts\dist" path. when i run my script my screen getting closed immediately. how keep my screen After Running test.exe file. if provide with solution it would be helpful to me

I am not sure you like this, but I'll show you an idea.
If you put the code below at the end of your script, the windows will wait for 10 seconds and then close. If you want more or less time, just change the number.
import time
time.sleep(10)

I assume this happens because the resulting exe is a console application. Default behaviour of these is to close the (new) console window when done.
You can either
Run it from existing command line window to see the output,
Modify the program and add keyboard input at the end:
input('Press enter to continue')

Related

Python file immediately closes

As an assignment for one of my classes, the professor had us download a file containing python code as well as a couple of text files that the python code will analyze.
The problem is, when I click to open the python file, the command window opens, then immediately closes. I can get it to stop from closing if I click on it fast enough, but that stops the code from loading, and any keystroke causes it to close again.
It's the same file every other student downloaded and used with no issue. I've tried re-downloading the file, re-installing python, even trying on a different computer. I've also tried opening it by entering the path file name in the command window with no success. Also no luck trying it in Jupyter Notebook or CodeLab. (And of course, no luck experimenting with the slew of possible solutions on previous Stack Overflow, Reddit, etc. questions).
Any ideas on the potential cause and/or solution?
Edit: cause unknown, but the solution was opening the file with Sypder console.
the file closes immediately because It ended and returned 0, to stop that from happening you can add at the end of the python code something like that:
ending = input("press key to exit")
or just import time and use
time.sleep(5)
you can open your file in the cmd/terminal window and you will be able to see the results

How to run python scipt in the background(after user input) even if the terminal is closed

I have a python script that takes some inputs from the user & then executes the code based on the input. The code takes some time to complete; during this code runtime the user can close the terminal(the code is run from a Linux machine)
As soon as the user closes the terminal the script stops as well. I know there are options like nohup but it wouldn't accept any input(where input is required in my script).
How can I fix this?
Requirement is -
Run the script, enter the inputs
Let the code run in the background even if the terminal is closed
Also is there a way to write whatever is being printed in the terminal(during the script runtime) to some file
Linux's screen tmux served my purpose.
There is a work around possible,
you can split your programm into two parts.
And start your background task with:
import os
usr_inp=input("test input: ")
os.popen(f"python3 background_task.py {usr_inp} &")
This should start the other programm in the background, there you can use the input over the sys.argv[1] variable.
(Usually it's not recommended using os.popen)

batch file to close cmd when program not found

I am writing a python program which takes input from the user then create a batch file and open that program and I want that if program not found then it returns that error to python and close the popup
Windows cannot find 'room'. make sure you type correctly, and then try again.
and cmd.
I don't have any idea how to solve this problem.
def batch(text):
bat_file = open("hello.bat","w")
bat_file.write("start "+text)
bat_file.close()
os.startfile("hello.bat")
As I mentioned above I want to close the popup and cmd.

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/

Clearing Python shell

How to clear Python shell ?
I am writing a module in python, I want to save it in a file. what is the best way to do it?
File -> New Window. Put your module in this new window, than save it. To run, just press F5.
Python shell does not get cleared or saved. Perhaps you are using IDLE. It's a confusing piece of software. I'd recommend you to get a real IDE, or at least a proper text editor.
Something that I do in IDLE is print "\n"*100
You can also extend Idle and map that to a key. Read Pythoninstall\lib\Idlelib\extend.txt for details
http://bytes.com/topic/python/answers/737111-extension-idle-clear-screen-but-how-write-screen
If you are just in console, you can do ( which won't work in IDLE):
import os
os.system("cls")
Just copy and paste the code into a new file (In file), and save it. To run, you can go to the run section and select "Run Module", or you can simply press F5.

Resources