open -a KeyboardViewer
I found that it's possible to launch on screen keyboard via the above terminal command.
I am trying to open the OSK from my python code. How can I launch it ( osX)
import os
cmd = 'open -a KeyboardViewer'
os.system(cmd)
the above code will simply launch the virtual keyboard.
Related
I am running python3 in Windows 10. Within my program, I try to launch another python instance.
os.system("python -m http.server")
print("All Done")
or
os.system("python")
I want to see a Window shell that pops up and allow me the see the output and interact with it. But instead the program just continues. I don't even see a window pops up. What am I missing?
If you want to open a new window, you need to use start cmd
If you want to open a new window running a command from cmd, that's start cmd /c "command"
e.g
import os
os.system('start cmd /c "python -m http.server"')
However, if you really just want to run another python process, you can import and run the server directly from code - refer docs
I've written a batch file that opens a new command terminal.
I'm using Windows10 with Python3, and I'm using the popen() function from the subprocess module.
Opening the command window succeeds. However, I can't push/pipe the code that I want the new command window to run.
The code I want to run in the new command window is in the same directory, and it's called async.py.
How can I run that script on the new command window that I have opened?
Kindly note that I have already read the articles:
How to execute a command prompt command from python
but to no avail.
This is my script:
from subprocess import Popen, CREATE_NEW_CONSOLE
Popen('cmd', creationflags=CREATE_NEW_CONSOLE)
the new window opens.
How to run the script "async.py" in that window?
I also tried:
runpy.run_module(mod_name='async')
but it runs the script in the same window.
I also tried:
os.popen('C:\\Users\\Me\\PycharmProjects\\async.py')
which does absolutely nothing.
To recap the question:
How do I run the Python script in the new terminal window after successfully opening it with subprocess?
Try adding /C after cmd and then feeding the command async. Like this...
Popen('cmd /C async.py', creationflags=CREATE_NEW_CONSOLE)
You may need to do full paths. Also if you require a second window to start, you may need to run a command like this:
start c:\windows\system32\cmd.exe /C c:\whatever\location\async.py
I'm learning Python by the book 'Think Python.'
My computer's OS is Windows 10.
I tried using os.popen ('14.8 Pipes' in http://www.greenteapress.com/thinkpython/html/thinkpython015.html) but the book provides an example on Unix.
I want to run Unix on Python script out of curiosity.
I already installed Git bash and Virtual box, but I don't know how to connect and to use Python.
The example provided by the book is:
14.8 Pipes
Most operating systems provide a command-line interface, also known as
a shell. Shells usually provide commands to navigate the file system
and launch applications. For example, in Unix you can change
directories with cd, display the contents of a directory with ls, and
launch a web browser by typing (for example) firefox.
Any program that you can launch from the shell can also be launched
from Python using a pipe. A pipe is an object that represents a
running program.
For example, the Unix command ls -l normally displays the contents of
the current directory (in long format). You can launch ls with
os.popen1:
>>> cmd = 'ls -l'
>>> fp = os.popen(cmd)
It appears you are getting tripped up converting the ls command which lists directory contents on *nix to a Windows command. If you search for "what is the windows version of ls" in a search engine, you will discover that Windows provides similar functionality through dir. For more useful conversions check out the conversion table on lemonda.net.
Changing the code to
>>> cmd = 'dir' and calling it via
>>> fp = os.popen(cmd)
Should enable the example to run on Windows.
if you have the latest update of windows 10 you can use Ubuntu, a version of Linux, from your command prompt by just typing the word 'bash' and waiting on it to download some files. if you see a $ at the end of your command line you got it. after that just type 'sudo apt-get install python' and enter your windows password when it asks for your password hit the 'Y' key when it asks if you are sure. then you should be able to go from the book from there.
EDIT: I believe you have to run cmd as administrator to install bash. You also may have to enable it by hitting the 'windows key + r' to open the run window then type 'appwiz.cpl to open the uninstall window and then click the button on the left of the screen that says 'Turn Windows features on or off', then wait for that to load and then go check the box by 'Windows Subsystems for Linux (Beta)' and then trying to type 'bash' in an elevated cmd prompt.
So I am trying to do following:
I have Cygwin enabled with screen and ssh daemon in Windows 7.
I create a new screen using the command screen -dmS "my_screen" on my Windows machine.
I ssh to the Windows machine from my Linux machine.
I attach to it from my unix machine using screen -d -r my_screen
Now I try to launch a Windows application, for example notepad.exe.
Now I want to a automate this using Python. The objective is to just manually ssh to Windows and then run a Python script which will do the above steps. I have written the following script but it is not working:
import shlex
import os
import time
import subprocess
cmdString = "screen -d -r default_screen"
cmdArgs=shlex.split(cmdString)
p=subprocess.Popen(cmdArgs)
cmds = "./notepad.exe"
cArgs=shlex.split(cmds)
pp=subprocess.Popen(cArgs)
This is not working. :( Basically to get the screen I will probably need to import pty package or tty. But pty & tty are not supported in Windows. I am able to attach to the newly created screen but then attempt to launch the Windows program like notepad for example fails. It hangs and the windows GUI is not launched as it would when down manually.
I am still exploring this but I will appreciate it if someone can point me to the right way to do it.
I put the screen command in the bash profile script of cygwin user. This is working now.
On the bash terminal, I can issue "Ctrl-Shift-T" to open a new terminal window. How can I do this from a python script which I run on the bash terminal?
Ctrl-Shift-T has absolutly nothing to do with bash but the terminal emulator you are using!
If you want to open a new windows you just use the subprocess module and execute the terminal command (gnome-terminal, xterm etc.)
But in my experience Ctrl-Shift-T opens, not a new window, but a new tab in the current window. That is a bit trickier. But here is a sample how you would do that within a bash-script. This however seems like something that would work on your local machine. But it don't gives me good vibes. Is there another way you could accomplish your task that would be more failsafe on other machines as well? In that case I would recommend it.
I rewrote the bash script i linked to as a Python script. Just make sure you have the tools xprop, xdotool and wmctrl installed.
import subprocess
wid = None
xprop_out = subprocess.check_output(['xprop', '-root'])
for line in xprop_out.splitlines():
if '_NET_ACTIVE_WINDOW(WINDOW)' in line:
wid = line.split()[-1]
if wid:
subprocess.check_call(['xdotool', 'windowfocus', wid])
subprocess.check_call(['xdotool', 'key', 'ctrl+shift+t'])
subprocess.check_call(['wmctrl', '-i', '-a', wid])
else:
print 'Failed to find window ID'