How to run exe file from python?
Solution 1
Problem solved. Thanks everyone, the issue was administrative privileges.. Started pycharm as administrator. And like i said - i was able to see the file with os.listdir(), but when i try to run it, errors start to pop up. I think the main issue is os.system() inherit current privileges from python proccess.
Solution 2
Problem is that the system
command doesn't work. It "works", but in a separate subprocess that exits right away. Current directory doesn't propagate up to the calling process (also, as you're not checking the return code, the command wouldn't fail, even with a non-existing directory. Note that it happens here, as the directory name has spaces in it and isn't quoted...).
You would have to use os.chdir
for that, but you don't even need it.
If you want to run a command in a specific location, just pass the absolute path of the command (and since it's using string literals, always use r
prefix to avoid that some \t
or \n
chars are interpreted as special characters...). For instance with python 3, with the command line provided there's an error (but okay in python 2...):
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 5-6: truncated \UXXXXXXXX escape
So always use raw prefix. Here's how I would rewrite that:
current_dir = r"C:\Users\User\AppData\Windows\Start Menu\Programs"
subprocess.Popen(os.path.join(current_dir,"file.exe"))
And if you really need that the current directory is the same as the exe, use cwd
parameter. Also get the return value of Popen
to be able to wait/poll/kill/whatever and get command exit code:
p = subprocess.Popen(os.path.join(current_dir,"file.exe"),cwd=current_dir)
# ...
return_code = p.wait()
As a side note, be aware that:
p = subprocess.Popen("file.exe",cwd=current_dir)
doesn't work, even if file.exe
is in current_dir
(unless you set shell=True
, but it's better to avoid that too for security/portability reasons)
Note that os.system
is deprecated for a lot of (good) reasons. Use subprocess
module, always, and if there are argument, always with an argument list (not string), and avoid shell=True
as much as possible.
Solution 3
Can you see if this works
import os
# change the current directory
# to specified directory
os.chdir(r"C:\Users\User\AppData\Windows\Start Menu\Programs")
subprocess.Popen("file.exe")
Moshe
Updated on June 04, 2022Comments
-
Moshe almost 2 years
I try to run a exe (In the background) from a specific path in my local python project, with the os.system libary. I have managed to change folders like 'cd' Command, but i can't run the file.
This is for a python project running on Windows 64BIT, Python 3.5.3
the file.exe is at the "programs" dir.
import os os.system("cd C:\Users\User\AppData\Windows\Start Menu\Programs") subprocess.Popen("file.exe")
Error:
{OSError}[WinError 193] %1 is not a valid Win32 application
I saw other posts about this issue but i couldn't resolve it. Any ideas?
-
Brandon Bailey over 4 yearshave you tried os.system("./file.exe")?
-
SyntaxVoid supports Monica over 4 yearsI might be wrong, but I think changing directory with an
os.system
call only changes the directory for that call. A shell is opened, the command is executed, and the shell is closed. Useos.chdir
instead to change the scripts working directory. -
Moshe over 4 years@SyntaxVoid It worked for me. os.system() changed the path for the next commands as well
-
SyntaxVoid supports Monica over 4 years@Moshe, very strange. That is not how it acts on my machine, even if I run python as admin
os.getcwd(); os.system("cd Desktop"); os.getcwd()
will display the same directory and no change will happen. This happens regardless of me being in an interactive python shell, or running a .py file.
-
-
Moshe over 4 yearsNot working, for some reason i cant reach the file from python. although i see it in os.listdir()