How to terminate the running function in Python Shell? - python

I thought this is an easy task but I googled around and had no clue at all.
I'm trying to run the pyhook tutorial and once I get to pythoncom.PumpMessages(), the function will stay idle and wait for Windows events.
I don't want to close down the window nor restart the shell because of all imported library and defined functions. Is there any way to termindate just the function?
I tried ctrl+c but it doesn't work at all.
Thanks a lot.

Related

How to persist Python turtle graphics screen

I am learning Python (v 3.7) on a Mac using PyCharm. As I practice using the turtle library, the program runs without error, outputs the correct graphics, but then the graphics screen disappears immediately after the code completes runnning. Adding time.sleep(5) at the end of my program persists it and also shows that the focus changed from PyCharm to a Python program menu (which I can't find or turn on in the hope of keeping running).
When I use Thonny, the output persists, so I can check my work. How can I make it persist in PyCharm?
A well-structured Python turtle program will end with a call to mainloop() or one of its variants (exitonclick(), done()) This turns control over to the underlying tkinter event handler which will keep your window open, awaiting user events. Without this, the program simply ends and the window closes.
You don't need time.sleep() nor input("Press Enter to continue..."). Some Python programming environments clash with mainloop() but even those tend to disable it behind the scenes so the same code works everywhere.
First, do not use PyCharm to test. If you like it, more power to you, but personally, I have had many issues with output and PyCharm. Learn the command line, since you are using Mac. CodeAcacdemy and LinuxJournery have really good resources on that subject. Second, if you are still having issues, please reinstall trutle, Python 3.7 (there were some issues wiht it displaying on Mac), and macOS itself. Have a great day!
https://linuxjourney.com/
https://www.codecademy.com/learn/learn-the-command-line
Problems getting pygame to show anything but a blank screen on Macos Mojave

Just writing 'import' in a python script breaks my mouse cursor forcing a hard reset

I'm totally baffled by this.. Trying to code a python script on my Debian Stretch system but after running a 4 lined script my mouse cursor would turn into a cross and blocking any click actions from working and forcing me to hard reset the system by holding the off button!
I tested a few times and found out that my script just needs to contain an import for it to break, nothing else - literally a one-lined:
import pxssh
And run
./bug.py
System broken. Mouse cursor looks like a cross and cannot click anywhere. Hard reset required.
I found someone else with what appears to be the exact same issue, there's an uploaded image of it but I don't know if it was python causing it for him/her. https://askubuntu.com/questions/918261/why-does-my-cursor-keep-changing-to-a-black-cross-and-how-do-i-revert-it
If I use my keyboard and re-run it again, I get this error:
import-im6.q16: unable to grab mouse `': Resource temporarily unavailable # error/xwindow.c/XSelectWindow/9182.
You're not running Python! You're accidentally running this as a shell script. Run it as
python bug.py
or include the shebang line:
#!/usr/bin/env python
Currently, you seem to be running a completely unrelated program named import, designed for screen capture.
This has just happened to me, and without this thread I would never have found the cause. I just would like to add that you can stop the 'import' program by running:
pkill -9 import
And that should fix everything.

Check if Python program is running, in Windows

I am in the process of creating a Python program (lets call it Program1), which will terminate randomly. I need another program to check if Program1 has been terminated, and if so, to restart it after a given amount of time. Should I use another Python program to do this, or is there a way this can be done in Windows e.g. similar to a cron job?
From what I understand, you can use sys.exit(), exit() and quit() to kill a Python program, but do all of these also kill all other running Python programs?
Thanks
Reliably determining whether a specific program is still running or not can be done in multiple ways.
A good way to do so would be using a specific lock file. You can find how to do this in Python described in this thread.
To answer the second part of your question: sys.exit(), exit() and others will only stop the currently running Python process, not any others that might be running concurrently.

Multiple Windows with a Multithreaded pyqt application

I wrote a small multiprocessing application and then wrote a PyQt front end for it. When I run the script by calling it from the command line with Python (or by calling run from the Spyder IDE), it runs exactly as I would expect and works nicely.
But if I try to use Py2Exe to make an executable to give it to a friend, it starts behaving oddly. When the users hits the botton that really starts the process and invokes the multithreading portion, it spawns multiple Qt windows that look like the original. It then essentially locks up. Closing one of the new windows that it spawns causes it to reopen that window. Attempting to close the original generates a message that it is not responding.
I would appreciate any help or suggestions about where to look.
I'm not positive about this without looking at your code, but there are some extra considerations when using Py2Exe with multithreading.
Take a look at this link and maybe it has something to do with your problem.
Someone has a similar sounding issue here

Python script waiting for some program being launched and then starting another program. (Windows)

I would like to write a python script that will finally be converted to .exe (with pyinstaller lets say) and added to windows startup applications list. This program (once launched) should 'monitor' user and wait until user will start a specified program (say program1.exe) and then my python program should launch another specified program (program2.exe).
I know that there is something like subprocess to launch another program from python script but I was not able to make it work so far ;/ And as it comes to this part where I need to monitor if user does start specified program I have no idea haw to bite on this. I don't expect a complete solution (although it would be very nice to find such ;p) but any guides or clues would be very helpfull.
For the monitoring whether or not the user has launched the program, I would use psutil: https://pypi.python.org/pypi/psutil
and for launching another program from a python script, I would use subprocess.
To launch something with subprocess you can do something like this:
PATH_TO_MY_EXTERNAL_PROGRAM = r"C:\ProgramFiles\MyProgram\MyProgramLauncher.exe"
subprocess.call([PATH_TO_MY_EXTERNAL_PROGRAM])
If it is as simple as calling an exe though, you could just use:
PATH_TO_MY_EXTERNAL_PROGRAM = r"C:\ProgramFiles\MyProgram\MyProgramLauncher.exe"
os.system(PATH_TO_MY_EXTERNAL_PROGRAM)
Hope this helps.
-Alex

Categories