andfoy / pywinpty

Pseudoterminals for Windows in Python
MIT License
110 stars 27 forks source link

Problem calling an executable with full path #427

Open JoseRaffucci opened 1 month ago

JoseRaffucci commented 1 month ago

I am running into an issue where if I provide a command that has a full path for an executable, for example: "C:\Program Files\Java\jdk-21\bin\java.exe"

It fails to load giving a FileNotFoundError. After investigating the spawn code I found it tries to find the executable in the PATH env variable. While testing the issue, I modified the environment variable PATH to include the location of the executable but this still failed.

To resolve the issue I commented out lines 79-87 of ptyprocess.py:

  path = env.get('PATH', os.defpath)
  command_with_path = which(command, path=path)
  if command_with_path is None:
      raise FileNotFoundError(
          'The command was not found or was not ' +
          'executable: %s.' % command
      )
  command = command_with_path
  argv[0] = command

I am not sure the spawn code should be testing paths against the environment variable PATH and should be left to the code that is calling spawn. This gives more flexibility and also behaves more closely to the subprocess.Popen would. After commenting out this code everything was working as expected.

Another option to the environment PATH testing is if the "which" call returns None, perhaps a call to os.path.exists(command) or os.path.isfile(command) to verify the file exists.

Depending on how you would like the spawn function to behave, I would be glad to create a pull request.

andfoy commented 1 month ago

Pinging @blink1073, since he might know more information about this mechanism

blink1073 commented 1 month ago

I agree with @JoseRaffucci that we could skip this check and defer to the behavior of subprocess.Popen.

JoseRaffucci commented 1 month ago

Understood, I'll update the project code and create a pull request for this issue.