This is probably a fairly simple question, but I have gone thoroughly overwhelmed trying to figure it out.
I want to start another program in Windows from Python 2.6. I have the "command-line argument" figured out so that if I create shortcut and double-click on it the other program opens, does what it needs to, and then closes.
I started with the subprocess library, but that seemed not to work. I got overwhelmed looking at all of the different versions of "popen"
How do I run in an external program from Python like I had double-clicked on a shortcut?
Something like below maybe?
import os
os.system("notepad")
Or:
from subprocess import call
call(["notepad"])
The below question has some excellent answers:
Calling an external command in Python
Related
This has been troubling me for the past hour. My python script won't run; however, if I select 'check module', input the code, and run the same thing it works. It's a very simple script:
import pyscreenshot as getImage
im = getImage.grab(bbox=(1300,800,1500,850))
im.save("screen.png")
Try running through terminal and it will work.
Although I do not know why this happens exactly (probably ask those who maintain it) I can tell you it is because of some terminal dependant function that is created while running.
The IDLE is not actually a terminal so it cannot run exactly like a terminal (although it outputs the same content, it is not a terminal). For example running os.get_terminal_size() under the IDLE will not work yet the terminal will. There are also some functions in PIL that perform in the same way.
Anyway this post shows a pretty similar code and it is mentioned it doesn't work under the IDLE.
You need to set childprocess=False as IDLE has problems with multiprocessing. IDLE runs user code in a separate process so with pyscreenshot IDLE hangs and seems like the module won't work. You may go through this to know more.
An option is to turn off multiprocessing by setting childprocess=False.
Try:
import pyscreenshot as getImage
im = getImage.grab(bbox=(1300,800,1500,850), childprocess=False)
im.save("screen.png")
I'm bored and just making something for the heck of it in Python. I saw someone typing with spaces between all their letters and decided to make a python script that does this. It was pretty easy, but then I wanted to take it a step further, because copy/pasting from console takes time, so I want to have this script put spaces after every keyboard press but only when I have Discord as the active window. The only things I could find that could give you the active window are from 5-15 years ago, and are all outdated. They say use win32gui, and I pipinstalled it, but it doesn't seem to work.
EDIT: For clarification, I ran "pip install win32gui" and it installed, I opened a python shell and typed "import win32gui" and it said no such module
I looked through the modules and found win32 and according to the help command win32gui is part of its package, so I tried win32.win32gui and it says there's no such attribute
I'm new to coding, I'm not entirely sure what I'm doing.
It seems win32gui isn't recognized in shell which I was using to test if it worked or not, however after just adding it to an actual python script it works just fine.
When I use IPython along with the -wthread option, it spawns a python subprocess, which appears as a Mac OS X application.
My problem is that when I send commands to that application (for example plotting with matplotlib), the window is updated behind all my other windows. I would like to be able to call a python command to switch this python window to the front (I do that manually with ⌘-tab, but I have to find the python application first, and there might be several ones).
Is there a python script to detect which application IPython has spawned, and how to then automatically switch to it in OS X?
(I'm stating the problem in OS X, but the issue should be similar on other systems).
Edit: let me break this down in two problems:
how to know which Mac OS X application python is running in? (probably possible with some IPython witchery)
how to tell Mac OS X to put the focus on that application? (maybe using applescript)
Could be either:
Making a new python script that tracks grandchild processes of another script might be tricky. The IPython documentation has an example to monitor spawned processes by pid; JobControl. JobControl only kills the processes but I imagine adding a command to change window focus would be fairly easy.
From what I've read, the Tk gui does not properly set window focus on macs. If your 'matplotlib' or otherwise uses the Tk gui, this may be the problem. -source-
I am not very familiar with OS X, so either run with those, clarify your situation or let me know if I'm too far off.
Here is my full solution, with an IPython magic function.
Install appscript (see this question about switching apps programmatically in OS X), and put the following code in a script called activate.py in your ~/.ipython folder.
import appscript
import sys
appscript.app(pid=int(sys.argv[1])).activate()
Now, edit your ~/.ipython/ipy_user_conf.py configuration file and define the magic function:
def wxactivate(self, arg):
import wx
pid = wx.GetProcessId()
ip = self.api
import os
here = os.path.dirname(__file__)
import subprocess
subprocess.Popen([os.path.join(here, 'activate.py'), str(pid)])
Now you just have to register this magic IPython function by putting the following somewhere in that same configuration file:
ip.expose_magic('wxactivate', wxactivate)
Now, after you run IPython -wthread, you can call %wxactivate and you will switch to the corresponding Python application!
(note that the reason why one has to run the call to appscript's activate() in another process in not clear to me; it may have to do with some threading problem... any explanation would be appreciatated)
Could someone explain to me how can I run my py2exe program, a console program, without the terminal on Windows?
I'm trying to make a program that re-sizes windows and it supposed to start with windows, so I want it hide out but still running...
Use the setup() function like this:
setup(windows=['myfile.py'])
See the list of options for setup().
Not really understand your requirement, but you can try start /MIN. type start /? on the command line to see its help page.
Would you consider compiling it into an EXE (using py2exe or some such) and adding it to your list of startup programs?
Sounds like what you want is for your script to run as a Windows Service instead of a Windows program. Something like this tutorial might be helpful:
http://islascruz.org/html/index.php?gadget=StaticPage&action=Page&id=6
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