Open .exe on external terminal in Python - python

I would like to run a .exe file on external terminal.
With this code I managed to run the .exe but it starts on the same terminal I call the script
import subprocess
subprocess.call(["C:/Users/Alessandro/Downloads/BOSCH-GLM/BOSCH-GLM/dist/glm50.exe"])
Is there a way to run it on external terminal?

Try using subprocess.Popen instead of subprocess.call.
Take a look at the discussion here. You can ignore the .communicate() call.

Related

Python opens executable, but it doesn't close on its own

Little background: Code::Blocks is an IDE with a C++ integrated compiler. When creating a C++ project, it creates a .exe file so you can run the project.
So now I want to run that executable file using a Python script (Using VSCode). I tried subprocess.call(), subprocess.run() and subprocess.Popen(), and all of them start the background process, but it doesn't compile, so it just keeps running on the Task Manager. If I run it manually (by double-clicking it) then it opens, it closes and I get my correct answer on the output file.
This is the C++ project folder for the problem "kino" :
This is a photo with the .exe on the Task Manager :
And this is my Python code:
process = subprocess.run([r'C:\Users\Documents\kino\kino.exe'], shell = True)
If you still don't understand my problem, here is a video describing it.
Use pywin32 to get it done.
Something like this will solve your issue
import win32com.client
app = win32com.client.Dispatch("WScript.Shell")
app.Run('Path/Yourexe.exe')
subprocess.run would not capture the output of the spawn process, however there is no reason for it to keep running in background. Try the following example, I just checked this in my linux(with a simple 'hello world' program) so if it does not works for you then it could be OS specific issue.
p = subprocess.Popen(['C:\Users\Documents\kino\kino.exe'], stdout=subprocess.PIPE)
#out, err = p.communicate()
print(p.stdout.read())
The way I solved my problem was by running the executable using the ./ command. So, I did something like
process = subprocess.run('./kino.exe', shell = True)

Using Python script to run a full independent powershell script

I am a noob, self-motivated programmer, and had been researching methods to use my Python script to run a Powershell file that will copy and image and place the image into Excel.
I've used the subprocess, call, and Popen commands in an attempt to call and run the Powershell program from the Python script, but none has worked.
Some of the examples I found only called different functions of a Powershell script, but when I tried those settings it didn't work for my program. All of the setup for my Powershell has been established so that it can run with my PC, and also runs well when launched independently from Python.
What I would like to ask is if I had, for example, a My_program.py file and a Moving_image.ps1 file. I want to use my .py file to run/execute my .ps1 file, while both programs are located in the same path (C:\Users\Scripts).
What line of code(s), imports, and other program setup's would I need in my Python file to simply run the independent .ps1 file from my Python script?
I don't need the Powershell script to return anything to the Python script. I would like for it to simply run the copy and paste the command I sent it.
Thank you. Any type of guidance that will lead to this program actually functioning properly will be most appreciated!
Here's what worked for me (testing on linux):
python script test.py
from subprocess import call
call(["/opt/microsoft/powershell/6.0.4/pwsh", "./test.ps1"])
powershell script test.ps1
Import-Module '/home/veefu/pwshmodules/testMod'
Write-Output "Hello from test.ps1"
Get-HelloWorld
testMod.psm1 module, stored at /home/veefu/pwshmodules/testMod
function Get-HelloWorld {
Write-Output "Hello World (from Module)"
}
result when running the python script:
Hello from test.ps1
Hello World (from Module)
On windows you'll probably have to provide the complete path, C:\Windows\system32\MicrosoftPowerShell\1.0\powershell.exe and you may have to pass /file .\yourOtherScript.ps1 in the second argument to call

How to run .exe file with command from python shell or idle

Well I have tried all of methods available on StackOverflow
1) !myfile.exe args
2) subprocess.call() and 3) subprocess.POpen() as per What's the difference between subprocess Popen and call (how can I use them)?
4) os.system as per How to run external executable using Python?
My program starts, but I am not seeing any output window, Actually my.exe programme is compiled using VisualStudio and I am taking few inputs in that .exe program using StdIn,
I have tried os.wait(), subprocess.wait()`` os.sleep(3) command my still output comes and disappears.
I have also tried this how to run an exe file with the arguments using python but problem is same.
What does my .exe program do?
Actually I am giving one file ".bin" file as command argument to my program. Further my program prompts user to take few strings as input using Std In.
If you run your python program from the CMD you can insert a time.sleep() in the python code, which will make the output stay a bit longer.
Otherwise, in order to be in line with your question a bit more, try making an external temp script that will run your .exe with a pause command in it or something.

How to start child cmd terminals in separate windows, from python script and execute scripts on them?

I have been trying rather unsuccesfully to open several terminals (though one would be enough to start with) from say an ipython terminal that executes my main python script. I would like this main python script to open as many cmd terminals as needed and execute a specific python script on each of them. I need the terminal windows to remain open when the script finishes.
I can manage to start one terminal using the command:
import os
os.startfile('cmd')
but I don't know how to pass arguments to it, like:
/K python myscript.py
Does anyone have any ideas on how this could be done?
Cheers
H.H.
Use subprocess module. Se more info at. Google>>python subprocess
http://docs.python.org/2/library/subprocess.html
import subprocess
subprocess.check_output(["python", "c:\home\user\script.py"])
or
subprocess.call(["python", "c:\home\user\script.py"])

Python subprocess module equivalent for double-click in Windows

I want to open a file using the subprocess module as though the file was double-clicked in Explorer. How do I do that?
I tried the following line:
subprocess.call("C:/myfile.csv", shell=True)
which throws an error saying:
The syntax of the command is
incorrect.
'C:\' is not recognized as
an internal or external command,
operable program or batch file.
How do I emulate a double-click using subprocess? Basically I want to open a CSV file in Excel 2007.
os.startfile(r'C:\myfile.csv')
(Win32 only. For Mac, run a process with 'open filename'; on Linux/freedesktop-in-general, 'xdg-open filename'.)
I think part of your problem is you're using a unix style slash / as a path separator, instead of the windows backslash . It looks like windows is interpreting /myfile.csv as an argument for the program C:, which is why you're getting that message.
However if you corrected that, I think you'd just get it saying that C:\myfile.csv isn't a program.
I know that this is a bit late, but to do that in python 2.x (not sure about 3) you should use the subprocess module, referencing Popen. Here is the code:
import subprocess
subprocess.Popen(r'explorer /select, "C:\"')
It basically opens the file, and then opens it in the default program.

Categories

Resources