I've been programming with Python and have made an EXE file.
I've added a shortcut to it in the startup folder in order to make it autostart with Windows.
The problem is that it takes some time between when the blank black console appears and the actual run of the program (i.e. it takes time until the program actually starts working).
How do I get rid of the blank black console window? I think it happens because Windows takes time to load the file's folders and libraries.
I want that the black window to be hidden at startup using win32gui.ShowWindow(win, 0), but it takes too long for this to happen. It works fine when launched normally but not when run from the startup directory while Windows is starting.
Related
I am writing a script to automate stuff in another program (let's call it the main program) on Windows (the main program is only available for Windows). The script reacts to certain visual elements in the main program by clicking on them. Now I would like to add some way of terminating the script while I'm in fullscreen mode in the main program. Right now I have to alt-tab into the console where my script is running and press ctr+c. The problem here is, that if I alt-tab while my script is running, it might click some random stuff on my desktop.
Ideally, I would like to add some key combination that I have to press while I'm in the main program that would terminate (or pause) the script. I tried to do this with pynput.keyboard, but windows defender marks it as a keylogger (which it technically is) and removes my script. Is there a way of doing this without triggering windows defender?
Alternatively, I guess I could automatically pause my script, whenever the active window is not the main program. I am wondering whether this might be the common approach for cases like mine?
I made a Python program that I do NOT want to turn into .exe or anything else.
However since the program needs python.exe to run, it runs python.exe openly.
I want to know if I can add a piece of code to the program I made so that it runs in the background.
BTW, I do not want to run through cmd.I want it to be a background process automatically.
Basically, I want to add some code into my program so that as soon as it is clicked upon, it runs in background.
I found out the way to do this easily: I just renamed my file.py into file.pyw
Now, it runs without a console. Basically I typed this in the cmd:
ren file.py file.pyw
I have it running a python script in a loop:
#ECHO OFF
:loop
D:\Python\python.exe C:\pythonfile.py
goto loop
PAUSE
How do I make it so when I double-click the batch file, the process runs in the background and the console window isn't showing in the taskbar?
Make the process be hidden completely with windows built in vbs, then call the batch from it. You will then run the vbs file instead of the batch, which will call the batch file.
no_see.vbs
Set MyScript = CreateObject("WScript.Shell")
MyScript.Run "C:\wherever\script\is\batchfile.cmd", 0, False
This will run the batch file completely hidden.
The python script however might still call a console window, so you should consider this.
#ECHO OFF
:loop
D:\Python\pythonw.exe C:\pythonfile.pyw
goto loop
PAUSE
This will run without a console, meaning everything will be hidden.
One concern I have, you are running the .py file in a loop, no sleep nothing meaning you will spawn thousands of python instances. What exactly are you attemping? If you can give me an idea, I can perhaps help in making the solution better.
I'm not sure if there is a way to do that using batch, at least not one that I know of. However, I would recommend doing this with a scheduled task instead if you do not want it running in the taskbar.
Open the Task Scheduler and in the library, create a new task. From there, you can set conditions, triggers, and plenty of other options for how you want it to run, including automatic start at logon, etc.
Just thought of this...alternatively, you could also use the Task View of Windows 10. Open and run the batch file, and then move it to another virtual desktop.
I am using Windows 7. I installed Python and everything is working fine except the fact that as I am saving python scripts on desktop or anywhere, none of them are opening.
.py files are closing in a blink!
.pyw files are not even responding.
Any help appreciated. I have already uninstalled and reinstalled the software suite but the problem still persists.
Here are the pictures of the icons.
What happens when you run a .py file is that a cmd.exe window is opened where Python runs your script. But this window will close as soon as your script finishes.
What you can do in this case:
Add raw_input() (python2) or input() to the end of your script to make it pause for input. That wat you can read any output before the window closes.
Open a cmd window and run your scripts from that.
With regard to .pyw files, they are themselves responsible for creating a window. So unless those scripts create a GUI you probably won't see anything.
I recently made an executable of a Python program with py2exe, and when I ran the executable, a command window appeared for a split second and then disappeared. My program itself never actually ran at all. Everything is still inside the dist folder, so I'm not sure what's actually wrong. Is there a solution for this?
If all your program does is print something and you run it by double-clicking the executable, then it simply closes the console when it finishes running. If you want the window to stay open, run your program from the command line. You can also create a batch file that runs your program and then pauses the console, so that you at least get a "press any key" before the console closes.