Issue2023

classification
Title: subprocess cannot run Windows batch file from current dir which was not starting dir
Type: Severity: normal
Components: Library Versions: Jython 2.7
Milestone:
process
Status: open Resolution: accepted
Dependencies: Superseder:
Assigned To: Nosy List: adamburke, fwierzbicki, jeff.allen, mniklas, santa4nt, zyasoft
Priority: Keywords:

Created on 2013-03-04.10:04:14 by mniklas, last changed 2019-05-06.11:36:03 by adamburke.

Files
File name Uploaded Description Edit Remove
dir_batch_test.py mniklas, 2013-03-04.10:04:13 Program that creates batch and tries to run it from selected directory
Messages
msg7899 (view) Author: MichaƂ Niklas (mniklas) Date: 2013-03-04.10:04:13
I have problem when my Jython program tries to execute
Windows batch file which is the current directory, but this directory
is not the directory the program was started in.

Test program: dir_batch_test.py is attached.

In CPython this works well:

C:\share\mn>python dir_batch_test.py
Trying to execute: "batch_test.bat" in dir: "C:\share\mn\test"
file exists: C:\share\mn\test\batch_test.bat

C:\share\mn\test>time /t
10:16


In Jython I got strange exception:

C:\share\mn>jython dir_batch_test.py
Trying to execute: "batch_test.bat" in dir: "C:\share\mn\test"
file exists: C:\share\mn\test\batch_test.bat
Exception while trying to execute batch_test.bat
Traceback (most recent call last):
  File "dir_batch_test.py", line 21, in execute_cmd_args
    proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  File "D:\jython2.5.3\Lib\subprocess.py", line 751, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "D:\jython2.5.3\Lib\subprocess.py", line 1265, in _execute_child
    raise OSError(e.getMessage() or e)
OSError: Cannot run program "batch_test.bat" (in directory "C:\share\mn\test"): CreateProcess error=2, The system cannot find the file specified

I think this is a bun in Jython subprocess module.
msg9366 (view) Author: Jim Baker (zyasoft) Date: 2015-01-09.03:36:12
This is still an issue with 2.7 on Windows

Need to see if there's some problem preventing the chdir from setting the desired working directory for the subprocess
msg12143 (view) Author: Adam Burke (adamburke) Date: 2018-10-18.04:56:01
I have hit a similar issue and have a possible cause and workaround. 

I am not clear on why yet, but it seems the normal behaviour when calling out to a Windows executable from Java is for a very limited environment to be available. The command line has to be explicitly invoked to get the context of standard commands and batch files.

Eg

>>> subprocess.call(['dir'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Adam\jython\jython\dist\Lib\subprocess.py", line 535, in call
    return Popen(*popenargs, **kwargs).wait()
  File "C:\Users\Adam\jython\jython\dist\Lib\subprocess.py", line 892, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\Adam\jython\jython\dist\Lib\subprocess.py", line 1402, in _execute_child
    raise OSError(errno.ENOENT, os.strerror(errno.ENOENT))
OSError: [Errno 2] No such file or directory
>>> subprocess.call(['cmd','/c','dir'])
 Volume in drive C is OS
 Volume Serial Number is E447-FAFD

 Directory of C:\Users\Adam\jython\jython

...

Coming from a more Unix-y background I find this quite unintuitive, but there you go.

Related stackoverflow answer here:
https://stackoverflow.com/questions/18893284/how-to-get-short-filenames-in-windows-using-java

I have been able to invoke batch files successfully on the latest Jython using cmd /c and subprocess.

I am not sure what the correct target behaviour for jython should be here. I guess CPython can be a guide to whether cmd /c should be invoked by default when calling out to subprocess under windows. That is the extent of my research for now.
msg12486 (view) Author: Jeff Allen (jeff.allen) Date: 2019-05-02.20:16:27
@Adam: I think you'll find the same in CPython. The subprocess command has to find an executable file, while a lot of commands are built into the cmd executable for which (unlike Unix) no corresponding executable exists.

As for the original, I think Jim is right: Java won't let you change the real working directory. Jython tries to simulate it, but it may be better for it not to pretend.
msg12496 (view) Author: Adam Burke (adamburke) Date: 2019-05-06.11:36:03
Got distracted after posting that comment. Confirm CPython is the same

C:\Users\Adam\jython\jython4>python
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.call(['dir'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Adam\AppData\Local\Programs\Python\Python35\lib\subprocess.py", line 557, in call
    with Popen(*popenargs, **kwargs) as p:
  File "C:\Users\Adam\AppData\Local\Programs\Python\Python35\lib\subprocess.py", line 947, in __init__
    restore_signals, start_new_session)
  File "C:\Users\Adam\AppData\Local\Programs\Python\Python35\lib\subprocess.py", line 1224, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
>>> subprocess.call(['cmd','/c','dir'])
 Volume in drive C is OS
 Volume Serial Number is E447-FAFD

 Directory of C:\Users\Adam\jython\jython4

06/05/2019  09:27 PM    <DIR>          .
06/05/2019  09:27 PM    <DIR>          ..
08/01/2019  05:23 PM               394 .gitignore
08/01/2019  05:23 PM               349 .hgignore
08/01/2019  05:23 PM             5,584 .hgtags
History
Date User Action Args
2019-05-06 11:36:03adamburkesetmessages: + msg12496
2019-05-02 20:16:27jeff.allensetnosy: + jeff.allen
messages: + msg12486
2018-10-19 09:20:07adamburkesettitle: subproces cannot run Windows batch file from current dir which was not starting dir -> subprocess cannot run Windows batch file from current dir which was not starting dir
2018-10-18 04:56:02adamburkesetnosy: + adamburke
messages: + msg12143
2015-01-09 03:36:12zyasoftsetresolution: accepted
messages: + msg9366
nosy: + zyasoft
versions: + Jython 2.7, - Jython 2.5
2013-03-20 03:56:08santa4ntsetnosy: + santa4nt
2013-03-04 17:43:22fwierzbickisetnosy: + fwierzbicki
2013-03-04 10:04:14mniklascreate