1

I have create a C++ application that use python using the python C++ library and I have temporarily change my PATH variable into C++ using this code

GetEnvironmentVariable( _T("PATH"), szSysPath.GetBuffer(32000), 32000) ;
szSysPath.ReleaseBuffer() ;

szSysPath = _T("C:\\Path1;") +
            _T("C:\\Path2;") +
            szSysPath ;

SetEnvironmentVariable( _T("PATH"), szSysPath );

Next, I have initialized Python using

Py_Initialize() ;

This code is OK because I can execute many script Python using my application but when I execute a script that show the PATH, my new paths added are not present.

This is my Python script

import os

print(os.environ['PATH'])

Do you have any ideas about this problem? Maybe i must update library os with new data? Thanks

belmat
  • 11
  • 1
  • 1
    That `_T` is a really bad idea here. It's hiding some Windows95/Windows NT differences in character set, but that is critically important here. Practically speaking, there are two environments: ANSI and Unicode, and they're kept roughly in sync. So we don't know what the code above is really doing. – MSalters Oct 10 '18 at 09:20
  • ANSI and Unicode are not the problem. The problem is that I set the PATH variable in C++ but into Python this change is not reflected and i see old status of PATH variable – belmat Oct 10 '18 at 10:44
  • the fact that you talk about **the** `PATH` variable is already indicative. Effectively, there are two. – MSalters Oct 10 '18 at 11:47
  • have you found a solution to this? – mhn_namak Oct 11 '20 at 05:39
  • I solved using a direct call of Python functions. pFunc = PyObject_GetAttrString( pEnviron, "_ _ setitem _ _" ); GetEnvironmentVariable( _T("PATH"), szSysPath.GetBuffer(32000), 32000) ; szSysPath.ReleaseBuffer() ; pArgs = Py_BuildValue( "(uu)", _T("PATH"), szSysPath ); PyObject_CallObject( pFunc, pArgs ); Py_DECREF( pArgs ); – belmat Oct 12 '20 at 06:12

0 Answers0