title: Calling Python function with parametrs from C++ project (Visual Studio)
body: <p>I need to call a pipeline realized as a Python (3.6) function from my C++ project under the Windows platform. Function “<strong>function_name</strong>” from file  "<strong>experiment_test.py</strong>" takes text string as input parameter and return another text string as the result. I try the code below but it doesn’t work properly – python functions from libraries <strong>shutil</strong>, <strong>codecs</strong>, <strong>makedirs</strong>, etc doesn’t work.</p>
  
  <p>C++ code (reduced):</p>
  
  <pre><code>std::string Text,Result;
  PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue;
  Py_Initialize();
  
  pName = PyUnicode_FromString("experiment_test");
  pModule = PyImport_Import(pName);    
  pDict = PyModule_GetDict(pModule);
  
  pFunc = PyDict_GetItemString(pDict, "function_name");
  
  pArgs = PyTuple_New(1);
  pValue = PyUnicode_FromString(Text.c_str());
  PyTuple_SetItem(pArgs, 0, pValue);
  
  if (PyCallable_Check(pFunc))
  {
      pValue = PyObject_CallObject(pFunc, pArgs);
      if (pValue != NULL)
      {
          Result = PyUnicode_AsUTF8(pValue);    
          Py_DECREF(pValue);
      }
      else return false;    
  }
  // ...
  
  Py_Finalize();
  </code></pre>
  
  <p>Python code (reduced):</p>
  
  <pre><code>#!/usr/local/bin/python3
  import shutil
  import codecs
  from os import makedirs
  from os import path
  from os import unlink
  from subprocess import call
  
  def function_name():
  
      name = 'working_files/current_text'
  
      if not path.exists('working_files'):
          makedirs('working_files')
      if path.exists('result.txt'):
          unlink('result.txt')
      with codecs.open(name + '.txt', 'w', encoding='utf-8') as f:
          f.write(text)
      # ...
      return result
  </code></pre>
  
  <p>So no new files will be generated by Python. I tried to import Python modules in C++ by calling <strong>PyRun_SimpleString("import shutil");</strong> etc after <strong>Py_Initialize();</strong> but it doesn’t help.</p>
  
  <p>What do I do wrong?</p>