0

I have a program, written in C++. I would like to get full path to python executable from it. For example, if I open Windows command prompt (cmd.exe) and type python, it use python executable from PATH. So, i would like to have a function get_exec_path("python") whick returns something like C:\Python27\python.exe. C:\Python27\ is in my PATH. I need this for calling python scripts from C++ code. Embedding python in C++ is a bad idea for my purposes. I used to call it like this:

std::system("start \"\" /WAIT python myscript.py --arg1 arg1 --arg2 arg2")

but this method shows command prompt window, I would like some kind of background work. For this purpose I used CreateProcess with second argument "C:\Python27\python.exe myscript.py --arg1 arg1 --arg2 arg2". So, I need full path to python executable from PATH variable.

sashadereh
  • 223
  • 1
  • 15

3 Answers3

2

You're asking the wrong question.

Instead of trying to bypass the shell (and reinventing the PATH variable while doing so), use it to your advantage by passing the proper flags to start for hiding the command prompt window.

According to the documentation, that's /b:

Starts an application without opening a new Command Prompt window.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Always **read the documentation** for the tools you use, to find out how you can get the most out of them. Programming by guessing does not work. – Lightness Races in Orbit Oct 04 '16 at 11:58
  • Unfortunately, it is hide python interpreter window, but not windows command prompt, which is called by std::system, so your solution is not good for me. – sashadereh Oct 04 '16 at 13:23
  • @sasha You can also read [the documentation for `CreateProcess`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx) to discover that it _does_ look in search paths. Don't stop researching. – Lightness Races in Orbit Oct 04 '16 at 13:46
0

There are some solutions, that could help you.

  • Get from windows registry using C++ tools. Replace {ver} with actual version. "3.5" was in my case.

    HKCU\SOFTWARE\Python\PythonCore\{ver}\InstallPath\ExecutablePath

  • Use where.exe utility to perform PATH search. It works like linux "which".

    C:\Users\admin>where python
    C:\Users\admin\AppData\Local\Programs\Python\Python35\python.exe

Artem Gromov
  • 124
  • 4
0

As you a showing a Windows Python path, this answer will focus on Windows and is not portable.

A function from the shwlapi does exactly what you want:

BOOL PathFindOnPath(
  _Inout_  LPTSTR  pszFile,
  _In_opt_ LPCTSTR *ppszOtherDirs
);

Its documentation says:

PathFindOnPath searches for the file specified by pszFile. If no directories are specified in ppszOtherDirs, it attempts to find the file by searching standard directories such as System32 and the directories specified in the PATH environment variable.

To find for python.exe you could do:

char path[MAX_PATH] = "python.exe";
BOOL cr = ::PathFindOnPathA(path, NULL);
if (! cr) {
    //process error ...
}
// path now contains the full path

BEWARE: you must include shlwapi.h and link shlwapi.lib ...

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252