Home > OS >  Execute an .exe in Python in Ubuntu/WSL
Execute an .exe in Python in Ubuntu/WSL

Time:08-11

How can I execute an .exe file with Python3 on Ubuntu in WSL? From my searches I found os.system, but despite being placed in the correct folder, I cannot find the .exe file. I also tried with os.open with no results.

import os


current = os.chdir('../../../Programmi/OWASP/Zed Attack Proxy/')
os.system("ZAP.exe")

CodePudding user response:

Try using a fully-qualified path:

os.system("../../../Programmi/OWASP/Zed Attack Proxy/ZAP.exe")

CodePudding user response:

To expound on @MichaelMatsaev's answer a bit, what you are attempting to do with the Python example in your question is essentially the same as this Bash construct:

cd ../../../Programmi/OWASP/Zed Attack Proxy/
ZAP.exe

You'll get a command not found from Bash. Pretty much every Linux app will work the same way, since they all ultimately use some form of syscall in the exec family.

When executing any binary in Linux (not just Windows .exe's in WSL), the binary must be either:

  • On the search $PATH
  • Specified with a fully-qualified (relative or absolute) path to the binary.

So in addition to @MichaelMatsaev's (correct) suggestion to use:

os.system("../../../Programmi/OWASP/Zed Attack Proxy/ZAP.exe")

The following would work as well:

os.chdir('../../../Programmi/OWASP/Zed Attack Proxy/')
os.system("./ZAP.exe")

And, while it would be a bit pathologic (i.e. I can't imagine you'd want to do it) for this case, you could even modify the search path inside the code and then call the binary without a fully-qualified path:

os.environ['PATH']  = os.pathsep   '../../../Programmi/OWASP/Zed Attack Proxy/'
os.system("ZAP.exe")

Side-note: AFAIK, there's no reason to attempt to store the os.chdir into the current variable, as os.chdir doesn't return a value.

  • Related