-1

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")
NotTheDr01ds
  • 15,620
  • 5
  • 44
  • 70
Harry
  • 13
  • 2
  • 1
    Do you want to open the file, or execute it? – 9769953 Aug 10 '22 at 12:44
  • I have to start an executable. I am on windows but I am using the Ubuntu app – Harry Aug 10 '22 at 12:49
  • 1
    I'd guess that your path is incorrect, try using an absolute path, e.g. `os.chdir("/mnt/c/Users/.........)`. You can use the other `os` functions to [test for file existence](https://stackoverflow.com/a/82852/2280890). Opening the windows executable on WSL should be fine, is there a reason you're running this from WSL instead of just from windows? – import random Aug 10 '22 at 13:07

2 Answers2

2

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.

NotTheDr01ds
  • 15,620
  • 5
  • 44
  • 70
1

Try using a fully-qualified path:

os.system("../../../Programmi/OWASP/Zed Attack Proxy/ZAP.exe")
NotTheDr01ds
  • 15,620
  • 5
  • 44
  • 70
  • Answers are rarely posed in the form of a question. – Scott Hunter Aug 10 '22 at 12:45
  • @ScottHunter Sometimes that's just a cultural thing. The answer could certainly use some more exposition/explanation, but the suggestion is, I'm fairly confident, correct. Without having tested it (yet, but I will) myself, I'm assuming that `os.system` (just like most binary executions) doesn't execute files in the current directory unless they are on the search path. Calling it with the path is probably necessary. – NotTheDr01ds Aug 10 '22 at 13:50
  • 1
    @nonDucor That's not really part of the question - The question is tagged [tag:windows-subsystem-for-linux], which is a Windows feature that runs Linux distributions in a virtualized (WSL2) and/or syscall translation (WSL1) layer. All versions of WSL provide an Interop feature to allow Windows PE binaries to run the a `binfmt_misc` registration from within the Linux layer. – NotTheDr01ds Aug 10 '22 at 14:01