Python Forum
launch .exe from python script in windows
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
launch .exe from python script in windows
#1
Hi

I'm trying to launch a .exe program with a python3.6 script.
I usualy launch my program.exe using command line "cmd" with 5 arguments as follow :


C:\Users\Harold>"C:\Program Files (x86)\EGSesameTriangle\Debug\EGSesameTriangle.exe" 200 1.5 20 80 100

I've tried differnt things with no success !
WIth the 5 arguments:

# python3
# launch_SESAME.py launch SESAME
import subprocess
subprocess.Popen([r"cmd"])
subprocess.Popen([r'"C:\Program Files (x86)\EGSesameTriangle\Debug\EGSesameTriangle.exe" 500 1.5 20 80 200'])
Error:
Traceback (most recent call last):   File "C:\Python35-32\launch_SESAME.py", line 9, in <module>     subprocess.Popen([r'"C:\Program Files (x86)\EGSesameTriangle\Debug\EGSesameTriangle.exe" 500 1.5 20 80 200'])   File "C:\Python35-32\lib\subprocess.py", line 707, in __init__     restore_signals, start_new_session)   File "C:\Python35-32\lib\subprocess.py", line 990, in _execute_child     startupinfo) PermissionError: [WinError 5] Accès refusé Microsoft Windows [version 10.0.14393] (c) 2016 Microsoft Corporation. Tous droits réservés.
With no arguments : the program behave the same way I would have launched it with cmd command line without providing the arguments !

import subprocess
subprocess.Popen([r"cmd"])
subprocess.Popen([r'C:\Program Files (x86)\EGSesameTriangle\Debug\EGSesameTriangle.exe'])
Error:
EGSesame  Generateur de creneaux via Instrunet pour obturateur Sesame, normalises DNI.  Alterne entre 2 phases ou positions du Sesame avec un profil triangle. EG 27 octobre 2016  UTILISATION: EGSesame [gradient_chauffe duree_cyclage niveau1 niveau2 gradient_niveaux]   gradient_chauff : gradient en pourcents par minute, jusqu'au niveau de phase le plus bas. Ex: 500   Duree du cyclage : en heure. Ex: 1.5 pour 1h30   Niveaux des phases: exprime en pourcent du Sesame, de 0.0 a 100.0 (ouvert).   gradient_niveaux : gradient en pourcents par minute, entre les 2 niveaux. Ex: 100 ATTENTION: Pas ou mauvais arguments donnes, reglages par defaut. Microsoft Windows [version 10.0.14393] (c) 2016 Microsoft Corporation. Tous droits réservés.
I'm a total beginner.
Any advice or link welcome.
Reply
#2
Hello,
usually this happens because program wasn't launched with administrator rights. So if you can, run the Python script as administrator.
Reply
#3
You cant execute directly program and parameters in one string with Popen - it tries to run command named "program parameter" and there is no such program. You can either split parameters and use ['program', 'parameter/parameters',... 'parameter'] or let python execute entire string within shell/cmd with parameter shell=True. Latter one is easier to use (or abuse).
Output:
In [2]: subprocess.Popen("/bin/echo 10") --------------------------------------------------------------------------- FileNotFoundError                         Traceback (most recent call last) .... FileNotFoundError: [Errno 2] No such file or directory: '/bin/echo 10' In [3]: subprocess.Popen(["/bin/echo", "10"]) 10 Out[3]: <subprocess.Popen at 0x7fa1f1d8a940> In [4]: subprocess.Popen("/bin/echo 10", shell=True) Out[4]: 10
You are not catching output of your program, if you want to do it, you can use Popen with stdout=subprocess.PIPE or run you program with subprocess.check_output().
Reply
#4
(Mar-20-2017, 03:27 PM)j.crater Wrote: Hello,
usually this happens because program wasn't launched with administrator rights. So if you can, run the Python script as administrator.

No change to run the script as administrator !

(Mar-20-2017, 03:46 PM)zivoni Wrote: You cant execute directly program and parameters in one string with Popen - it tries to run command named "program parameter" and there is no such program. You can either split parameters and use ['program', 'parameter/parameters',... 'parameter'] or let python execute entire string within shell/cmd with parameter shell=True. Latter one is easier to use (or abuse).
Output:
In [2]: subprocess.Popen("/bin/echo 10") --------------------------------------------------------------------------- FileNotFoundError                         Traceback (most recent call last) .... FileNotFoundError: [Errno 2] No such file or directory: '/bin/echo 10' In [3]: subprocess.Popen(["/bin/echo", "10"]) 10 Out[3]: <subprocess.Popen at 0x7fa1f1d8a940> In [4]: subprocess.Popen("/bin/echo 10", shell=True) Out[4]: 10
You are not catching output of your program, if you want to do it, you can use Popen with stdout=subprocess.PIPE or run you program with subprocess.check_output().

Sorry but as I said, I'm a total beginner and what you say is not really helpfull as not intelligible for me.
subprocess.Popen("/bin/echo 10")
and
subprocess.Popen(["/bin/echo", "10"])
and
subprocess.Popen("/bin/echo 10", shell=True)
give me all the same error !
Reply
#5
Those was just examples of command with parameter  and as it was linux command, it couldn't work for you on windows machine.

What i ment was to try either
subprocess.Popen(r'"C:\Program Files (x86)\EGSesameTriangle\Debug\EGSesameTriangle.exe" 500 1.5 20 80 200', shell=True)
or
subprocess.Popen([r'"C:\Program Files (x86)\EGSesameTriangle\Debug\EGSesameTriangle.exe"', "500", "1.5", "20",  "80", "200"])
(if i got your command/parameters right)
Reply
#6
(Mar-20-2017, 05:03 PM)zivoni Wrote: Those was just examples of command with parameter  and as it was linux command, it couldn't work for you on windows machine.

What i ment was to try either
subprocess.Popen(r'"C:\Program Files (x86)\EGSesameTriangle\Debug\EGSesameTriangle.exe" 500 1.5 20 80 200', shell=True)
or
subprocess.Popen([r'"C:\Program Files (x86)\EGSesameTriangle\Debug\EGSesameTriangle.exe"', "500", "1.5", "20",  "80", "200"])
(if i got your command/parameters right)

You've got a bunch of extra quotes around the executable name:
subprocess.Popen([r'C:\Program Files (x86)\EGSesameTriangle\Debug\EGSesameTriangle.exe', "500", "1.5", "20",  "80", "200"])
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#7
I remembered that ages ago commands with spaces in name were run in windows terminal like "C:\Program Files\dir name\boo.exe", so I kept OP's double quotes :), but yes, here it is different thing (probably).
Reply
#8
@zivoni


get the following errors

Error:
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ================== RESTART: C:\Python35-32\launch_SESAME.py ================== Traceback (most recent call last):   File "C:\Python35-32\launch_SESAME.py", line 11, in <module>     subprocess.Popen(r'"C:\Program Files (x86)\EGSesameTriangle\Debug\EGSesameTriangle.exe" 500 1.5 20 80 200', shell=True)   File "C:\Python35-32\lib\subprocess.py", line 707, in __init__     restore_signals, start_new_session)   File "C:\Python35-32\lib\subprocess.py", line 990, in _execute_child     startupinfo) FileNotFoundError: [WinError 2] Le fichier spécifié est introuvable >>>
Error:
Traceback (most recent call last):   File "C:\Python35-32\launch_SESAME.py", line 12, in <module>     subprocess.Popen([r'"C:\Program Files (x86)\EGSesameTriangle\Debug\EGSesameTriangle.exe"', "500",  "1.5",  "20",  "80",  "200"])   File "C:\Python35-32\lib\subprocess.py", line 707, in __init__     restore_signals, start_new_session)   File "C:\Python35-32\lib\subprocess.py", line 990, in _execute_child     startupinfo) PermissionError: [WinError 5] Accès refusé
Reply
#9
For the second thing, see my remark about the extra quotes.

When having such errors, make it simple. Change the code to start some known executable (say, notepad.exe). Once you have got that working, change to your executable, then use your parameters...
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Triggering a ps1 script in remote windows server via http python request jasveerjassi 1 365 Jan-26-2024, 07:02 PM
Last Post: deanhystad
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,199 Jun-29-2023, 11:57 AM
Last Post: gologica
  Python script running under windows over nssm.exe JaroslavZ 0 704 May-12-2023, 09:22 AM
Last Post: JaroslavZ
  Launch Python IDLE Shell from terminal Pavel_47 5 1,225 Feb-17-2023, 02:53 PM
Last Post: Pavel_47
  How to compile a Python script for a Windows / Linux executable? netanelst 2 1,319 May-24-2022, 07:02 AM
Last Post: netanelst
  Setup Portable Python on Windows for script starts with double clicks? pstein 0 1,813 Feb-18-2022, 01:29 PM
Last Post: pstein
  Launch Windows Application OEMS1 0 2,119 Mar-26-2021, 07:42 PM
Last Post: OEMS1
  Python script on windows 10 shuts down TamP 2 2,517 Mar-08-2021, 07:19 AM
Last Post: TammyP
Photo Python won`t launch error newbieee 0 1,759 Feb-02-2021, 11:51 PM
Last Post: newbieee
  How to kill a bash script running as root from a python script? jc_lafleur 4 5,883 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020