how to make my console in python not to close? - windows

I'm making a application in python from Windows. When I run it in the console, it stops, shows an error, and closes. I can't see the error becase its too fast, and I can't read it. I'm editing the code with IDLE (the program that came with python when I instaled it), and when I run it with the python shell, there are no errors. I would run it from IDLE, but when I use the console, it has more features.
I don't know why this is happening. I need your help.

Run the program from an already-open terminal. Open a command prompt and type:
python myscript.py
For that to work you need the python executable in your path. Just check on how to edit environment variables on windows, and add C:\PYTHON26 (or whatever directory you installed python to).When the program ends, it'll drop you back to the CMD windows prompt instead of closing the window.Add code to wait at the end of your script. Adding ...
raw_input()
... at the end of the script makes it wait for the ENTER key. That method is annoying because you have to modify the script, and have to remember removing it when you're done.

Run your program from a Windows command prompt. That will not automatically close when the program finishes.
If you run your program by double-clicking on the .py file icon, then Windows will close the window when your program finishes (whether it was successful or not).

Create a text file in the program directory i.e. wherever your script is located. Change the extension to .bat for example text.bat. Then edit the text file and write:
python main.exe
pause
Now you can run the program without typing into the command console by double clicking the bat file, and the console window will not close.

Related

Is there a command in Shell scripting for executing .exe file and running commands automatically inside of it? replacing the user interaction

I have a .sh script file that I'm modifying which runs an .EXE file that opens the Windows command line prompt automatically.
This .exe asks the user for an input (name of the file in the folder workspace that it will read)
I want to automate this step in my shell script so my user doesn't have to interact with this, and run the commands automatically
I read a bit about the expect command but I think that is for Linux only.
Can someone help me, I'm pretty new to Shell scripting and I couldn't find any useful information elsewhere.
I'm assuming that your executable accepts command-line arguments. So, here we go.
You can use the "start" command in Windows Shell. For example:
start C:\path\to\program.exe -argument
If you want to make the script wait until the .exe file finishes running before continuing, you can use the "/wait" command:
start /wait C:\path\to\program.exe -argument
IF all of that doesn't work, please try:
start myprogram.exe /command1 /command2 /command3
Hope it helps,

Run Python program in background - hide from taskbar

I have a Python script which collects some data and sends it over to a database. Using py2exe and Inno Setup, I have been able to create an .exe for distribution and also put the program run automatically when the computer (WINDOWS) starts; but the issue is that the command prompt window shows on the screen and on the taskbar. I don't have anything to show in the command prompt window so how can I make it run in the background without it being shown in the taskbar??
Please help!!! It is also OK if it shows up only as a tray icon.
Try to rename your script file to *.pyw format. It able to execute your script in background mode without command prompt.
That format available from v1.5:
The Windows configuration adds a new main program, "pythonw", and registers a new extension, ".pyw" that invokes this. This is a standard Python interpreter that does not pop up a console window; handy for pure Tkinter applications. All output to the original stdout and stderr is lost; reading from the original stdin yields EOF.
Maybe is late, but you have to force the .exe to run in background using runhidden in Inno Setup:
[Run]
Filename: "{app}\program.exe"; Flags: postinstall runhidden
Instead, if you want to run a python script or batch then you can use pythonw (Winodws OS):
pythonw script.py/script.bat

Python.exe stays open after running batch file in task scheduler

I'm trying to schedule in task scheduler:
Run a batch file
Activate a conda environment
Run a python program
Exit command prompt
Everything works fine, except the python.exe window will remain open while the command prompt closes.
My batch file: (the sleep is for the python code to run. It takes a few seconds)
call activate python2
start C:\Users\Chris\Anaconda3\envs\python2\python.exe testtest.py
sleep 30
exit
My python script:
driver = webdriver.Chrome(executable_path="C:\path\to\chromedriver")
driver.get('http://website.com')
# Find email and pw fields and then fill them in
email = driver.find_element_by_id("user_email")
email.send_keys('myemail#email.com')
pw = driver.find_element_by_id("user_password")
pw.send_keys('password')
# Click on sign-in button
driver.find_element_by_class_name("button").click()
time.sleep(5)
# Click on save button to update
driver.find_element_by_class_name("button").click()
# Close driver
driver.close()
Last thing, the program/script is the batch file, no arguments, and the start in is in the directory that the batch file is in.
Any help would be appreciated!
put you python codes in a main() function.
and give:
if __name__ == '__main__':
main()
at the end.
Just tested works for me.
#pk2019 's answer really helped me.
One improvement is to use
drv = webdriver.Chrome()
# Do your things.
...
drv.close()
drv.quit()
No need to do the dirty work of killing task.
I'm not a python expert, but I think you just need to call sys.exit() or quit().
Instead of using sleep and waiting too long or possibly not long enough, call start with the wait option:
call activate python2
start /WAIT C:\Users\Chris\Anaconda3\envs\python2\python.exe testtest.py
exit
If you don't need the batch file to do anything else, you can just start the python script and exit.
I had a similar problem with a Python selenium web scraper running geckodriver.exe Firefox web driver on Windows (executed via Task Scheduler using a .bat file). The problem is that my geckodriver.exe process is still running after the Python script is done ... and I think that running process is preventing the Windows command prompt from closing.
In order to test this hunch, I inserted the tasklist command into the .bat file, both before and after the Python script. It prints out a list of running Windows tasks to the console ... I noticed the geckodriver.exe file was still running the Python script was finished.
The way to kill a Windows process (using taskkill) is described in these two different Stackoverflow responses:
Safely killing a python script on Windows
Batch script to close all open Command Prompt windows
How to close Command Prompt window after Batch file execution in python?
Here is the Windows documentation for taskkill:
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/taskkill
In my case, I added this as the 2nd to last line: taskkill /im geckodriver.exe /f, and the last line was exit. That worked.

Keep Windows command window open

I'm running a command through the Windows shell- an existing command window (cmd.exe). When I execute the command, the window closes, even though it's a freestanding window not tied to the command.
How can I keep the window open to see the output?
You can't simply start a child cmd session because it'll share same window and if your custom tool actively closes its window (I wonder why) then it'll close your console and output will disappear.
There isn't much you can do if a program want to close console window but you can at least save its output to a file (to be inspected later with type). If you're working with that console and you don't want to close it then you can use start cmd to execute it in a new console window. Like this:
start cmd /c tool -args ^> output.txt
tool output will be available in output.txt after it finished.
It appears that the executable is closing the command window. Here is what you could try, may work. open a command shell. In the shell issue "cmd" and open another command shell. Run your executable in the newly opened command shell. You nested cmd will be exited, but you may still be able to see some of the output of your executable.

Lazarus on Windows: Why the extra terminal window?

I built a Lazarus GUI application using Ubuntu and then took it onto Windows to compile. On Ubuntu I run it from the terminal like this:
./prg arg
It runs fine using the argument arg passed to it. On Windows this is what I did:
Create shortcut to exe
Edit shortcut and include the argument.
To run on Windows, I run the shortcut.
It works fine but there's an additional terminal window opening behind the application, when I run the shortcut. When the application exits, the window closes with it. The terminal window is empty.
On Windows, before compiling I had to uncheck the -WG switch in compiler options. This was because althought the app is GUI-based, there is a simple routine that checks for the argument passed at command line and uses Writeln to output a message if there were errors.
My key question is why is this terminal window coming up on Windows and how do I get rid of it or suppress it?
Thanks!
(1) Make sure you have set {$APPTYPE GUI} in your code. Otherwise there will be always a "terminal" opened on windows.
(2) Maybe you are confusing a "real" shortcut (*.lnk) with cmd/batch file? The latter also opens a terminal that dies when the lauched app closes.

Resources