[IronPython] import subprocess
Eric.Wyler at bentley.com
Eric.Wyler at bentley.com
Thu Jul 5 22:09:12 CEST 2007
Matt,
I had the same problem and ended up solving it by using
System.Diagnostics.Process instead of subprocess. As far as I know,
subprocess doesn't work under IronPython. I don't remember all of the
reasons as to why it doesn't work, but, for example, os.pipe doesn't
exist under IronPython like in CPython.
Unfortunately, it's a bit of extra work if you're using subprocess to
capture stdout, stdin, etc. Here's a snippet of some code I used for
this. It's not very straightforward, unfortunately, because I wanted to
combine the stdout/stderr streams. Also, the only way to run a command
like you would type it on the command line is to run it through cmd.exe.
You can search around for it, getting return values can be a real pain
as well. I didn't test this code, just tried to copy and paste the
important parts from what I have, so I may have forgotten something.
class Process :
def __init__( self ):
self.stdoutstderr = System.IO.StringWriter()
self.cmdLine = "rd /s /q ."
def Run( self ):
processStartInfo = System.Diagnostics.ProcessStartInfo( "cmd",
"/c " + self.cmdLine )
processStartInfo.UseShellExecute = False
processStartInfo.RedirectStandardInput = True
processStartInfo.RedirectStandardOutput = True
processStartInfo.RedirectStandardError = True
processStartInfo.CreateNoWindow = True
processStartInfo.EnvironmentVariables.Clear()
for envVariable in os.environ.keys() :
processStartInfo.EnvironmentVariables.Add( envVariable,
os.environ[ envVariable ] )
processToRun = System.Diagnostics.Process()
processToRun.StartInfo = processStartInfo
processToRun.OutputDataReceived += self.__WriteStdoutData
processToRun.ErrorDataReceived += self.__WriteStderrData
processToRun.Start()
processToRun.BeginOutputReadLine()
processToRun.BeginErrorReadLine()
processToRun.WaitForExit()
stdoutstderrReader = System.IO.StringReader(
self.stdoutstderr.ToString() )
return ( processToRun.StandardInput, stdoutstderrReader )
def __WriteStdoutData ( self, sendingProcess, stdoutData ) :
if stdoutData.Data != None:
self.stdoutstderr.Write( stdoutData.Data + '\n' )
def __WriteStderrData ( self, sendingProcess, stderrData ) :
if stderrData.Data != None:
self.stdoutstderr.Write( stderrData.Data + '\n' )
Hopefully that helps.
Eric
-----Original Message-----
From: users-bounces at lists.ironpython.com
[mailto:users-bounces at lists.ironpython.com] On Behalf Of Matthew S
Bourdua
Sent: Thursday, July 05, 2007 3:50 PM
To: users at lists.ironpython.com
Subject: [IronPython] import subprocess
Hello,
I have a question about the "subprocess" module. I am unable to "import
subprocess" in my scripts. Will IronPython will support this in the
future,
or, perhaps is it intentionally not supported? (Or perhaps I am doing
something stupid, and it should work?)
I recently began using IronPython because I had both Python scripts and
.Net
classes that I needed to use together. So far I have been pretty
pleased
with how well everything is working.
My biggest problem has been that the os.system() call does not work in
IronPython. I notice that this was raised as an issue in Sep '06
(http://www.codeplex.com/IronPython/WorkItem/View.aspx?WorkItemId=2982).
While hunting around for an alternative, I found the following text in
the
library reference (http://docs.python.org/lib/module-subprocess.html):
"The subprocess module ... intends to replace several other, older
modules
and functions, such as:
os.system
os.spawn*
os.popen*
popen2.*
commands.*"
For this reason, I thought that perhaps "os.system" was going to become
deprecated in favor of "subprocess", and that might explain why this
issue
has not been addressed.
However, I am unable to "import subprocess" in IronPython. When I do
so, an
exception is raised because, "No module named fcntl". Digging into this
a
bit, I notice the following variable defined within subprocess.py (this
is
from ActivePython 2.5):
mswindows = (sys.platform == "win32")
And then many conditional statements of the form:
if mswindows:
...#Windows specific code
else
...#Non-windows code, presumably
IronPython fails this check because sys.platform = 'cli', and thus is
lead
into using modules that are (I would guess) meant for Unix-based
systems.
Further, this behavior seems to be known - I noticed that this seemed to
be
accounted for in (what appears to be) the automatic test-suite code
located
here:
http://www.codeplex.com/IronPython/SourceControl/FileView.aspx?itemId=29
6023
&changeSetId=21805
What's strange to me is that I couldn't find a single reference to using
subprocess in IronPython anywhere. So perhaps this issue is widely
understood by everybody but me (I'm relatively new to Python), or simply
not
an issue for anybody but me?
Anyway, I was hoping somebody on this mailing list might have some
insight
to share.
Thanks,
Matt
_______________________________________________
users mailing list
users at lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20070705/53a1cb96/attachment.html>
More information about the Ironpython-users
mailing list