the system command


I am trying to use the system command to launch a python program.

example.cc
====================
main() {

(...)
i=system(program.py);
(...)

}
======================

the compilation of example.cc is ok, but when I execute it the program is not able to find program.py (even if I did set the path correctly).

any clue ?

Barb

Despite the fact that I dont know the python, I think you should use the system command in the fallowing way:

i = system("c:\pathToPython\python c:\pathToYourProgram.py");

http://www.cplusplus.com/reference/clibrary/cstdlib/system/

http://www.python.org/doc/faq/windows/#how-do-i-run-a-python-program-under-windows

Bye
If you are using Windows and you installed Python using the Win32 installer, then .py files should have a Python association. Hence
1
2
3
4
5
6
7
8
#include <cstdlib>
using namespace std;

int main()
  {
  system( "hello.py" );
  return 0;
  }

should, indeed, launch the hello.py Python script.

It will also work on *nix if your script starts the proper way:
1
2
3
4
#! /usr/bin/python

print "Hello, world!"
exit

Hope this helps.
I'm not sure it will work, Duoas.
system("python myPythonFile.py");
will work provided you have set the PATH to include the python26 or python31 folder or whatever.

For example, on Vista you will need to go to control panel -> system & maintenance -> system -> advanced system settings -> Environment Variables -> Prepend (or append) C:\python26; to the current PATH variable. Replace C:\python26 with your python directory.

Or navigate to the following registry key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
for system-wide settings or
HKEY_CURRENT_USER\Environment
for a local setting.

The keys you'll want to edit are, obviously, called PATH.

On Linux distributions and some other Unixes you won't have to do any of this, and as Duoas said you'll just need to edit the script itself.
Never use system() on Windows.
You Shell or Krnl apis.
I disagree. You can use system, but it's bad practice. It's considered bad practice to declare using namespace std aswell, but you can still do it in cases where you're just messing around.

As Duoas has said before, it's only for more serious programs that you should never use system in.
chrisname
It will work if you have properly installed Python on your Windows system.
By using "python foo.py" you are assuming that the python executable/startup script is in the user's PATH -- which is far less guaranteed than having a proper file association that ShellExecute[Ex]() can handle -- which is what will happen when using system("foo.py");.

george135
There is nothing wrong with using system() responsibly. It is "always/never" rules that are bunk. The OP did not specify that her program should only work on Windows... so the simplest/most correct cross-platform answer is simply to use system(). Keep It Simple.

chrisname
There is also nothing wrong with using namespace std; so long as you keep it out of header files. (You can import whatever you like into your own namespace[s], but you should almost never import stuff into someone else's namespace. That is, don't dink with the namespaces of people using your library code.)


The system() call does what it is supposed to -- launch a system shell command. Since the purpose in this case is to have the shell launch another, specific executable, then it fits.

Hope this helps.
Ah, fair enough Duoas :P
Is the code to execute a program using system() universal, or should i say the same, on both windows and *nix
Simple answer: yes.

That is, only so long as the system shell runs named programs the same on win and nix. (For sh and cmd.exe there exists enough in common that you can execute other programs in either the system path or the current working directory with the same command. Shell commands, however, differ significantly.)

Glad to be of help.
Well they share the cd command; and the only difference there is between the paths is that Unix uses forward slashes and Windows uses backward slashes, but forward work.
So C:\Windows\System32
is the same as
C:/Windows/System32
but
/home/usr
Isn't the same as
\home\usr
which wouldn't work.
That's only true on NT and later. (IIRC)
Last edited on
actually I am working on linux
Topic archived. No new replies allowed.