Python Call External Program Windows

Perl Lists Python Lists PHP Lists Ruby Lists Tcl Lists ActiveState Lists. Lists » python-tutor. (in windows) > > Any hints? What about os.system('your_command_here')? Calling an external program. Max Noel: Feb 14, 2005 05:05 am [Tutor] calling an external program. Bernard Lebel.

Active3 months ago

I just started working on Python, and I have been trying to run an outside executable from Python.

I have an executable for a program written in Fortran. Let’s say the name for the executable is flow.exe. And my executable is located in C:Documents and Settingsflow_model. I tried both os.system and popen commands, but so far I couldn't make it work. The following code seems like it opens the command window, but it wouldn't execute the model.

How can I fix this?

Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
MesutMesut

15 Answers

Those whitespaces can really be a bother:-(. Try os.chdir('C:/Documents and Settings/') followed by relative paths for os.system, subprocess methods, or whatever...

If best-effort attempts to bypass the whitespaces-in-path hurdle keep failing, then my next best suggestion is to avoid having blanks in your crucial paths. Couldn't you make a blanks-less directory, copy the crucial .exe file there, and try that? Are those havoc-wrecking space absolutely essential to your well-being...?

Alex MartelliAlex Martelli
660k135 gold badges1059 silver badges1292 bronze badges

If using Python 2.7 or higher (especially prior to Python 3.5) you can use the following:

  • subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)Runs the command described by args. Waits for command to complete, then returns the returncode attribute.
  • subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False)Runs command with arguments. Waits for command to complete. If the return code was zero then returns, otherwise raises CalledProcessError. The CalledProcessError object will have the return code in the returncode attribute

Example: subprocess.check_call([r'C:pathToYourProgramyourProgram.exe', 'your', 'arguments', 'comma', 'separated'])

Call

In regular Python strings, the U character combination signals a extended Unicode code point escape.

Here is the link to the documentation: http://docs.python.org/3.2/library/subprocess.html

For Python 3.5+ you can now use run() in many cases: https://docs.python.org/3.5/library/subprocess.html#subprocess.run

Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
Ida NIda N
Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges

Python Call External Program Windows

SomeoneSomeone

I'd try inserting an 'r' in front of your path if I were you, to indicate that it's a raw string - and then you won't have to use forward slashes. For example:

Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
Jon MillsJon Mills
1,0572 gold badges10 silver badges25 bronze badges

Your usage is correct. I bet that your external program, flow.exe, needs to be executed in its directory, because it accesses some external files stored there.

So you might try:

(Beware of the double quotes inside the single quotes...)

Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
Adrien PlissonAdrien Plisson
17.1k4 gold badges36 silver badges69 bronze badges

Use subprocess, it is a smaller module so it runs the .exe quicker.

Robert
4,17711 gold badges45 silver badges91 bronze badges
Frodo BagginsFrodo Baggins
Celeo
4,4898 gold badges29 silver badges37 bronze badges
romyromy
int3int3
9,5795 gold badges46 silver badges77 bronze badges

Is that trying to execute C:Documents with arguments of 'and', 'Settings/flow_model/flow.exe'?

Python run external program

Also, you might consider subprocess.call().

Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
ThanatosThanatos
26.7k11 gold badges69 silver badges121 bronze badges

If it were me, I'd put the EXE file in the root directory (C:) and see if it works like that. If so, it's probably the (already mentioned) spaces in the directory name. If not, it may be some environment variables.

Also, try to check you stderr (using an earlier answer by int3):

The code might not be entirely correct as I usually don't use Popen or Windows, but should give the idea. It might well be that the error message is on the error stream.

Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
extraneonextraneon
20.1k2 gold badges38 silver badges49 bronze badges
JohnJohn

That's the correct usage, but perhaps the spaces in the path name are messing things up for some reason.

You may want to run the program under cmd.exe as well so you can see any output from flow.exe that might be indicating an error.

Dan OlsonDan Olson
19.3k4 gold badges35 silver badges50 bronze badges

for the above question this solution works.

just change the path to where your executable file is located.

Here test1.pdf rootimage is for my code .

Suraj Rao
25.3k8 gold badges65 silver badges76 bronze badges
spurthispurthi

There are loads of different solutions, and the results will strongly depend on:

  • the OS you are using: Windows, Cygwin, Linux, MacOS
  • the python version you are using: Python2 or Python3x

As I have discovered some things that are claimed to work only in Windows, doesn't, probably because I happen to use Cygwin which is outsmarting the OS way to deal with Windows paths. Other things only work in pure *nix based OS's or in Python2 or 3.

Here are my findings:

  • Generally speaking, os.system() is the most forgiving method.
  • os.startfile() is the least forgiving. (Windows only && if you're lucky)
  • subprocess.Popen([...])not recommended
  • subprocess.run(winView, shell=True) the recommended way!
  • Remembering that using subprocess for anything may pose a security risk.

Try these:

Q: Why would you want to use explorer in Windows?

A: Because if you just want to look at the results of some new file, explorer will automatically open the file with whatever default windows program you have set for that file type. So no need to re-specify the default program to use.

not2qubitnot2qubit
5,2602 gold badges45 silver badges74 bronze badges

in python 2.6 use string enclosed inside quotation ' and apostrophe ' marks. Also a change single / to double //. Your working example will look like this:

Also You can use any parameters if Your program ingest them.

finally You can use string variable, as an example is plotting using gnuplot directly from python:

Bomba PsBomba Ps

protected by CommunityDec 5 '18 at 13:42

Python Call External Program With Arguments Windows

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged pythonexecutable or ask your own question.

Active6 years, 10 months ago

Possible Duplicate:
Running a process in pythonw with Popen without a console
How do I eliminate Windows consoles from spawned processes in Python (2.7)?

I am using Python 2.7 and running the python scripts from within IDLE.

The commands I am executing are simple exe's that perform quick tasks. The issue I am having is every time the external commands are called from within Python a console is created and it flashes on my screen and takes focus, thus preventing me from using my PC while executing various scripts.

Examples of how I am calling them from within Python are as follows:

Searching for a solution I came across adding the following

to make the following command

However I still get the console appear every time an external command is called

Community
smashtasticsmashtastic

marked as duplicate by Piotr Dobrogost, Kate Gregory, ЯegDwight, David, Tim PostOct 23 '12 at 3:04

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers

There might be two issues here. First off, if your python scripts have the .pyw extension then they will be associated with pythonw which does not use a console*. However, you have shell=True, which generates a console*. You need to run the program and hide the console:

*Pedantically, it's not a dos prompt, it is a console window. DOS - Disk Operating System - was an IBM mainframe OS. MS-DOS or PC-DOS command-line features were mirrored (with a lot of extra features) by cmd.exe (a Windows shell), which is a console program and so uses a console window. It's that console window you need to hide.

cdarkecdarke
32k4 gold badges51 silver badges65 bronze badges

You need to use startupinfo parameter of subprocess.Popen() class' constructor.

You do not needshell=True if all you want is to hide console window; see this answer.

Community
Piotr DobrogostPiotr Dobrogost
29.9k28 gold badges187 silver badges317 bronze badges

Not the answer you're looking for? Browse other questions tagged pythonwindowssubprocess or ask your own question.