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.
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 usingnamespace 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.
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.)
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.