How to run external executable using Python? - arcpy

I have an external executable file which i am trying to run from a Python script. CMD executable runs but without generating output. Probably it exit before output can be generated. Any suggestion about how to delay exit until outputs are generated?
import subprocess, sys
from subprocess import Popen, PIPE
exe_str = r"C:/Windows/System32/cmd C:/temp/calc.exe"
parent = subprocess.Popen(exe_str, stderr=subprocess.PIPE)

use, more info here:
import subprocess["C:\\temp\\calc.exe"])
import os
i hope it helps you...

The os.system method is depreciated and should not be used in new applications. The subprocess module is the pythonic way to do what you require.
Here is an example of some code I wrote a few weeks ago using subprocess to load files, the command you need to use to delay exit until data has been received and the launched program completes is wait():
import subprocess
cmd = "c:\\file.exe"
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, creationflags=0x08000000)
creationflags=0x08000000 is an optional parameter which suppresses the launch of a window, which can be useful if the program you are calling does not need to be directly seen.

Option 1
import subprocess'C:\Windows\System32\calc.exe')
Option 2
Option 3
import os

This worked for me after trying everything else:
Change the location of your python program to be the same as where the .exe is located.
And then the simple:"calc.exe")
would work.


Generate data file at install time

My python package depends on a static data file which is automatically generated from a smaller seed file using a function that is part of the package.
It makes sense to me to do this generation at the time of running install, is there a standard way in setup() to describe “run this function before installing this package's additional files” (the options in the docs are all static)? If not, where should I place the call to that function?
Best done in two steps using the cmdclass mechanism:
add a custom command to generate the data file
override build_py to call that before proceeding
from distutils.cmd import Command
from setuptools import setup
from setuptools.command.install import install
class GenerateDataFileCommand(Command):
description = 'generate data file'
user_options = []
def run(self):
pass # Do something here...
class InstallCommand(install):
def run(self):
return super().run()
'generate_data_file': GenerateDataFileCommand,
'install': InstallCommand,
# ...
This way you can call python generate_data_file to generate the data file as a stand-alone step, but the usual setup procedure (python install) will also ensure it's called.
(However, I'd recommend including the built file in the distribution archive, so end users don't have to build it themselves – that is, override build_py (class setuptools.command.build_py.build_py) instead of install.)

Is it possible to read Doc Strings of a Python file w/o using python shell?

I was wondering if there is any way we can read doc strings present in a python file from command line, prior to importing it, or w/o using any Ipython or Python kernel, just like 'man' command does.
You can execute some python code from the command line, so given a module named, and a function named my_function, the following:
python -c "from my_module import my_function;print my_function.__doc__"
will print out the docstring

Passing control of console window to subprocess

I'm writing several Python scripts to manage building my C++ project. In one of these scripts, i will generate some header files, call GNU make with a makefile to build the project, then if successful, call gdb with my executable. My question is: How can i pass control of the console window to gdb, so i can interact with gdb directly without interference from Python? (Or perhaps pipe input and output through Python in real time.) And how would the Python script regain control of the console window after gdb has stopped?
Using subprocess, you can easily do that.
from subprocess import Popen
Popen('gdb a.out', shell=True).communicate()
print 'test'
once you quit GDB, script will continue and 'test' will be printed.

How to call specific EXE function on Linux/Mac

I'm sure if it's possible, but I'd like to call the function which is defined in the exe file on Linux/Mac:
0x421ff0 ##my_func_doing_stuff#Initialize 4
Basically I'd like to add command line support which is not implemented and the UI is kind of drag&drop which is useless.
Note: I don't have access to the source of the file.
I've was playing with wine, objdump, uncc (trying to covert it into C again) and Python using pefile, SWIG and ctypes:
from ctypes import *
import pefile, sys
pe = pefile.PE('my_file.exe')
print pe.dump_info()
my_exe = cdll.LoadLibrary('./my_file.exe')
but without success.
The error:
OSError: ./my_file.exe: invalid ELF header
reminded me that I can't call any of Windows functions under Linux without emulation?
So I'm looking for some other solutions. Probably it can be done somehow by emulating or debugging it under wine. But I'm not sure if there is any API for calling the specific functions.
Are there any existing solutions?
You can use winelib to load and link with the binary (since you need wine to provide a usable ABI), but keep in mind that this effectively turns your application into a Wine application.

'PyCObject_Import(“cairo”, “CAPI”)' C++ call yields segfault, 'import cairo' on python works

I am running OSX Lion and trying to import the python module for goocanvas, using python2.7.
I managed to successfully compile pygoocanvas-0.14.1, but when I try to import goocanvas through the python2.7 console, I get a segfault. After some debugging, I'm led to this code:
DL_EXPORT (void)
initgoocanvas (void)
PyObject *m, *d;
fprintf(stderr,"init<< \n");
// Pycairo_IMPORT; // XXX removed, it expands to the line below, anyways
Pycairo_CAPI = (Pycairo_CAPI_t*) PyCObject_Import("cairo", "CAPI"); // ADDED XXX
fprintf(stderr,"after import<< \n");
if (Pycairo_CAPI == NULL) {
I discovered that the segfault happens when the C++ code of the goocanvas python module tries to import the "cairo" library through PyCObject_Import("cairo", "CAPI"). However, if I try to import the cairo module directly through the python2.7 console via import cairo, it works.
How should I proceed? I have a blind seg fault and no idea on why it happens. Remembering that I managed to compile the python goocanvas module, but it segfaults upon trying to import it on python.
What kind of console are you using? I think gtk/glib has some hooks to enable running the main loop concurrently with the REPL. This means that threads are in use, which may cause crashes if glib.threads_init() was not called.
IMHO this is broken by design, because by just importing glib or any g* module a sane Python program that uses any threads will suddenly start to segfault. Supporting threading should be the default.
In our case, the crash was caused by the logging system of glib which was forwarded to Python without holding the GIL.