title: Embedding Python with C++
body: <p><strong>Problem:</strong> Strange exception is thrown when embedding Python with C++.</p>
  
  <p><strong>Program:</strong></p>
  
  <pre><code>bool embedd::execute_python(std::string location)
  {
      if (std::ifstream(location))
      {
              const char* file_location = location.c_str();
              FILE* file_pointer;
              // Initialize the Python interpreter
              Py_Initialize();
              file_pointer = _Py_fopen(file_location, "r");
              // Run the Python file
              PyRun_SimpleFile(file_pointer, file_location);
              // Finalize the Python interpreter
              Py_Finalize();
          return true;
      }
      return false;
  }
  </code></pre>
  
  <p><strong>What does the above code snippet should do:</strong>  The function should first check whether the passed argument is a valid location of the python file. If the file is present then it should execute the Python file.</p>
  
  <p><strong>Am I getting expected results:</strong>  yes and no.</p>
  
  <p><strong>what is going wrong:</strong> </p>
  
  <p>Test file 1:</p>
  
  <pre><code>print("Hello world")
  </code></pre>
  
  <p><strong>Result :</strong> Successful execution and getting proper output</p>
  
  <p>Test file 2:</p>
  
  <pre><code>from tkinter import *
  root = Tk()
  root.mainloop()
  </code></pre>
  
  <blockquote>
    <p>Result : Exception  root = Tk() File
    "C:\Users\User\AppData\Local\Programs\Python\Python35-32\Lib\tkinter__init__.py",
    line 1863, in <strong>init</strong> baseName = os.path.basename(sys.argv[0])
    AttributeError: module 'sys' has no attribute 'argv'</p>
  </blockquote>
  
  <p>Tested with some other file and found that when-ever we import modules(any) like tkinter, uuid, os etc similar exception gets thrown. on digging briefly about this the process monitor of my IDE told "Symbol file is not loaded" e.g no symbol file loaded for tk86t.dll </p>
  
  <p><strong>Python Version</strong> : 3.5.2  </p>
  
  <p>Links which I do have referred:
  <a href="https://stackoverflow.com/questions/12230210/attributeerror-module-object-has-no-attribute-argv-when-using-python-h">SO - 1</a> found the bug has been fixed from Python 2.3 here <a href="http://bugs.python.org/issue839151" rel="nofollow noreferrer">BUGS</a></p>