1

I have a weird behaviour while managing a third party executable in my python code. Conceptually I have the following code in python:

import subprocess
p = subprocess.Popen([r'c:\path\to\programme.exe', '-d'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
print p.returncode, out, err

And the tool crashes showing in out its traceback, and returning an error code that means "unhandled exception". I have tried with a simple os.system(...) with the same results.

But, here comes the fun part, when I just paste the command in the windows shell, it works perfectly...

C:\> c:\path\to\programme.exe -d

The python interpreter is a 32bit 2.7.2 version.

So... what can be the difference between these two calls that leads to the crash? thanks in advance.

Extra info

I am not quite sure if this helps, but this external tool connects to a database and performs some operations. With some RDBMS it works when called from python code, but when it connects to an Oracle DB, it crashes. So the python code seems to be right, there is just a factor or difference that I don't know.

8
  • I would appreciate an explanation for the closing votes. Commented Nov 4, 2014 at 13:49
  • Typical things to test for: IO (stdin being closed), evnironment. Easiest to test is to start Python (interactively) inside cmd and run same code. Commented Nov 4, 2014 at 14:07
  • I have already tried to run the command from the REPL, and it does not work either. Could it be because of any x64/x86 difference? I'm not very good at that. Thanks a lot. Commented Nov 4, 2014 at 14:14
  • It works well from the 32bit cmd.exe... Commented Nov 4, 2014 at 15:57
  • 1
    @ikaros45 it actually helps a lot, at least you know where to look for. Google this error and see what cause it. And see if running the app from Python (like you're doing) migth trigger it. Commented Nov 4, 2014 at 16:46

2 Answers 2

2

Well, you really don´t provide much info. I will make a guess based on my own experience dealing with situations like this.

  1. Make sure you´re running the Python app as admin if the third party app require priveleges.

  2. Check there is no problem with the working dir. Meaning, if the program opens some file or in any way it references to some relative path, you must change your working directory when executing from python. See code below for how to do this.

  3. If the programm you're executing is a builtin windows shell app (dir, copy, etc...) consider using shell=True when creating the Popen object. See Popen constructor reference.

  4. Python sets or modifies some environment variable needed/used by your third party application.

Code for changing working directory within the running Python app.

import os
os.chdir('/path_you_need/python/work_from')
Sign up to request clarification or add additional context in comments.

4 Comments

1) It does not need privileges, since it works from the cmd without problems. 2) Paths are absolute, that should not be a problem. 3) It is not a shell programme, just a given executable in a given place. However, I tried the shell=True and still does not work. The question at the end boils down to: what's the difference between running a command directly or as a subprocess from python?
@ikaros45 I think you must think deeper and ask what's the difference between running this app directly or as a subprocess from python?
I cannot ask what is the difference about a certain propietary app that probably nobody in SO knows. Therefore I am asking what can be different in general between running directly and as subprocess... which now seems to me that it could be an environment variable. Thanks for the help, I appreciate it.
Ok, found it! it was indeed an environment variable, namely ORACLE_HOME, which is only set inside of this python process (it's a huge framework). In the system this variable does not exist, and therefore the driver search falls back to path. If I use env with previous cleaning of the ORACLE_HOME, it works. Thank you a lot sir. If you adapt the question to include this environment issue, I will give you the love (otherwise it would be confusing).
0

You are supposed to use raw string.

p = subprocess.Popen([r'c:\path\to\programme.exe', '-d'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

else you can use \\ instead of using raw string like this:-

p = subprocess.Popen(['c:\\path\\to\\programme.exe', '-d'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

2 Comments

Thanks, but that is not the issue. The string is created programmatically... the literal is just an example.
What the 3rd party tool returns is not the matter. The question is: where is the difference between the calls?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.