Creating a subprocess in python
-
02-03-2021 - |
Pergunta
I am trying to use th subprocess
module along with the Popen
class to create and run a process. Up until now, I always succeeded calling any program I wanted.
Now I am trying to call paraview
(version 3.8.1) with a python script attached to it as a parameter like so:
paraview --script=script.py
If I run this command from console it works just fine. If I try to run it with my Popen
class it fails with the error message:
/home/woltan/local/lib/python2.7/site.py:157: Warning: 'with' will become a reserved keyword in Python 2.6
'import site' failed; use -v for traceback
/home/woltan/local/lib/python2.7/linecache.py:127: Warning: 'with' will become a reserved keyword in Python 2.6
/home/woltan/local/lib/python2.7/site.py:157: Warning: 'with' will become a reserved keyword in Python 2.6
'import site' failed; use -v for traceback
ERROR: In /home/kitware/Kitware/ParaView-3.8.1/source/Utilities/VTKPythonWrapping/Executable/vtkPVPythonInteractiveInterpretor.cxx, line 75
vtkPVPythonInteractiveInterpretor (0x124e9d0): Failed to locate the InteractiveConsole object.
Paraview itself is starting up. But is the error message due to the fact, that paraview itself has a python interpreter which it is using? Or can I tweak my Popen
calling routine which looks like this:
p = subprocess.Popen("paraview --script=script.py", bufsize = -1, shell = True)
How can it be, that a process can be created from console but not with the Popen
-call from above?
Edit
I downloaded and installed a paraview version where python 2.7 is running inside (and not python 2.5 of the version I used above) and the call of Popen
works. So this must have something to do with the python version of paraview. The only question remaining is: Why does it even matter when I start a process with Popen
?
Solução
There are 2 things which are strage:
Normally, this call shouldn't work at all. As you have
shell=False
, you would need to use a sequence as 1st parameter:p = subprocess.Popen(("paraview", "--script=script.py"), bufsize=-1)
(
shell=False
is the default.)You seem to mix several Python versions:
/home/woltan/local/lib/python2.7/site.py:157: Warning: 'with' will become a reserved keyword in Python 2.6
The said site.py
uses the with
keyword. That's ok, because it is intended for Python 2.7. The python version you are running seems to come from Python 2.5. There was no with
back in 2.4, and in 2.6 it was already present.
Outras dicas
The error message you're getting doesn't seem to be related to subprocess.Popen
. I'd say that what you need is either shell=True
:
p = subprocess.Popen("paraview --script=script.py", shell = True)
or separate args
in a sequence:
p = subprocess.Popen(['paraview', '--script=script.py'])
I've never used bufsize
, but I don't think it makes any difference here.