I have written a python program that periodically runs with ApScheduler package. The .pyw file runs in the background, but when I restart the PC the program doesn't continue running.
What can I do to run a code automatically in startup a windows 10 pc?
You should consider putting your python file to the Startup folder.
Click Win+R at the same time on your keyboard
Type shell:startup
Drag and drop your python file onto the folder that opened.
Note: I reccomend to put a shortcut of your python file for easier editing.
Related
I created a python file where an event loop is running.
During this event loop, pressing certain hotkeys causes interaction with another app.
If I open the file and run it through PyCharm, it works.
If I run the file in a terminal or as a .py file, the code exits prematurely for some reason so I have to run it with the python -i command or add input() at the end of the code so that the event loop keeps running for the hotkeys to work. Is there a reason why that happens?
Now I would like to autostart this file every time Windows runs. In theory it should be easy:
Change it to .pyw and run it with pythonw.
However, if I do that and click on the file to execute, it doesn't work. No idea what is happening in the background.
If I run the file in a terminal like this: python main.pyw
the script is executed in the terminal, I can see it and the hotkeys work.
Why is .pyw not working for me here? What other alternative do I have to run the file in the background so that I can always use the hotkeys whenever I boot Windows?
I'm trying to run an executable file called CDQ.exe (which is supposed to create a text file) from within a python script. I'm using os.system(CDQ.exe) to run it. When I start the python script, the program freezes upon the os.system() call. I also don't get the text file. When I opened the task manager, I found CDQ was still running, and it kept running until I ended it.
When I try to run the CDQ.exe from the windows file browser by clicking it twice, the program appears for a couple of seconds in the task manager, then it closes, and my text file appears.
The executable file was created from c++ qt, and I'm using windows.
Does anyone know what's going on?
i made a simple macro using pydirectinput (pyautogui failed me so I had to go with pydirectinput ) but I can't use my computer while the script is running can I like tell python to use this macro script on a specific window/program so I can freely use the computer and the macro will do its stuff in the background?
If you want the script to run in the background with no window just rename the file extension to ".pyw"
To stop a script that has no window, go into task manager -> details -> locate pythonw.exe -> End Task
Yes you can and it's very easy todo!
Just Rename your script's file extension to .pyw
.pyw files are used in Windows to indicate a script needs to be run using PYTHONW.EXE instead of PYTHON.EXE in order to prevent a DOS console from popping up to display the output. This patch makes it possible to import such scripts, in case they're also usable as modules.
In Order to close that run file you can just open your "Windows Task Manager" using the shortcut ctrl + shift + Esc and then find pythonw.exe and click on DELETE button or End Task Button in Bottom
I am struggling with an apparently obvious issue. I've been practising python and I have a few scripts and I am trying to run them from windows 10 execute command (WIN + R).
I am doing this by creating a BATCH file of the script, and in theory the file should run with a simple "py script" in the WIN+R window. But it does not, it simply flashes the CMD window for a sec and then disappear.
Everything seems to be set up correctly, I can run the script from CMD, BATCH file are OK (they run with a double click, I included the #pause at the end), the folder where BATCH are is in the env. variables PATH.
So i really don't get what is wrong. Note that If I type the full path in the WIN+R window it works, but that takes quite some time..
Any hints?
A BIG thank you :)
EDIT: thanks for the replies, here is the batch:
#py script.py
#pause
It works on double click.
Ctrl + R => opens run window.
Type in "cmd"
If you have python installed with Path.
python <yourscript.py>
should run just fine.
Otherwise,
Run as below.
path/to/python/python.exe <yourscript.py>
Hope you did the same as above. If the bat runs and closes in a flash. That is not an issue. It happens.
Have a sleep in your bat as well. If you're running on bat.
Add below after your python script execution.
It will make your bat script wait for a thirty seconds before closing and you can verify your stdout.
timeout /t 30
On Windows, executing Python scripts directly, i.e. without calling python before, can be tricky. Often Windows will just open your default Python editor to show you the file, because the .py extension is registered to this editor.
see this post for more info:
How to execute Python scripts in Windows?
You should create a batch file as follows...
#py -3 E:\FOLDER\SUBFOLDER\FILE.py %*
#pause
I have both python 2 and python 3 so I use py -3 as for me py will run Python 2
The ending %* is important
I want to start a python script using the Windows Explorer's right-click context menu.
These are my registry entries to achieve that:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\shell]
[HKEY_CLASSES_ROOT\*\shell\CleanDesktop - Add to persistent files]
[HKEY_CLASSES_ROOT\*\shell\CleanDesktop - Add to persistent files\command]
#="\"C:\\Users\\Admin\\PycharmProjects\\clean_desktop\\wescript.py\" %1"
I also edited the environment variables so that if I simply run the command:
"C:\Users\Admin\PycharmProjects\clean_desktop\wescript.py" "some_argument" in cmd it works just fine.
I tested it with a simple .exe file compiled from an c program
[HKEY_CLASSES_ROOT\*\shell\CleanDesktop - Add to persistent files\command]
#="\"C:\\Users\\Admin\\PycharmProjects\\clean_desktop\\ctest.exe\" %1"
And it works just fine. But if I try the same with a .py file Windows states that: This Application cannot be opened on this PC
Is there a way to achieve what I want without the need of making an .exe file out of the .py file ?