How do I call a batch file in jython?

thanks!!

Dani AI

Generated

The message "no module named subprocess" usually means you are running an older Jython that predates a complete stdlib. Jython 2.5+ includes subprocess; 2.2.x did not. Quick check: start the Jython REPL and run import sys; print sys.version. If you are on an older build, upgrade, then try again. Also confirm the .bat file is either in the current working directory or on %PATH%.

Even on Windows where calling a .bat sometimes works directly (as showed), the most reliable, version-agnostic approach is to invoke it through cmd.exe. This avoids quoting issues and gives you full control over working directory, environment, and output capture:

import os, subprocess

bat = r"C:\full\path\to\hello.bat"
workdir = os.path.dirname(bat)

p = subprocess.Popen(
    ["cmd.exe", "/c", bat],
    cwd=workdir,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
    shell=False
)
out, err = p.communicate()
print out
if err:
    print "stderr:", err
print "exit code:", p.returncode

If you are stuck on a build without subprocess, Jython lets you drop down to the JVM. This mirrors what subprocess does under the hood and works well for .bat files:

from java.lang import Runtime

proc = Runtime.getRuntime().exec(["cmd.exe", "/c", r"C:\full\path\to\hello.bat"])
rc = proc.waitFor()
print "exit code:", rc

Tips: use raw strings for Windows paths (r"C:\..."); prefer absolute paths or pass cwd=; if the batch needs environment variables, pass a copy via subprocess.Popen(..., env=envdict). os.system() (as noted) is fine for quick checks, but it cannot capture output and gives you less error handling than subprocess or the Java APIs.

Recommended Answers

All 4 Replies

Here test, subprocess module functions OK in Jython, as it should:

Test batch file batch.bat

@echo off
echo Hello, from batch file

Test in Jython prompt:

>>> import subprocess
>>> subprocess.call("batch.bat")
Hello, from batch file
0
>>>

Hi,

I'm getting the error
InputError: no module named subprocess

is there another way of doing the task??

thanks!!

You might want to update to the latest version of Jython. This one works just fine on my Windows7 machine ...

"""jy_hello_bat.py
run a batch file using Jython

The Windows batch file 'hello.bat' is:
echo Hello, from batch file 

tested with jython2.5.2
"""

import subprocess

subprocess.call('hello.bat')

In my experience most of my normal Python26 code seems to work with Jython 2.5.2 as well.
Jython is actually a real nice way for Java programmers to enjoy Python syntax, and for Python programmers to get used to a little Java.

This might be simpler ...

"""jy_hello_bat2.py
run a batch file using Jython

The Windows batch file 'hello.bat' is:
echo Hello, from batch file 

tested with jython2.5.2
"""

import os

os.system('hello.bat')
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.