title: call python function from c++ (visual studio 2019)
body: <p>I want to call the function from a python file from the visual studio 2019.
  By googling, it looks like I need to use the following function.
  I added a &quot;Sample.py&quot; in the same place where the exe is stored for the c++ .</p>
  <pre><code>int main()
  {
      Py_Initialize();
      // Create some Python objects that will later be assigned values.
  
      PyObject* pName, * pModule, * pFunc, * pArgs = nullptr, * pValue; 
      pName = PyUnicode_FromString((char*)&quot;Sample&quot;); 
      pModule = PyImport_Import(pName); 
      pFunc = PyObject_GetAttrString(pModule, (char*)&quot;fun&quot;); 
      pValue = PyObject_CallObject(pFunc, pArgs);
  }
  </code></pre>
  <p>In the &quot;Sample.py&quot; which is placed in the same path as the exe of the c++</p>
  <pre><code>import matplotlib.pyplot as plt
  
  def fun(): x = [1,2,3]
  y = [2,4,1]
  plt.plot(1, 2)
  </code></pre>
  <p>And when i run the c++ code, it throws an exception at PyObject_CallObject(pFunc, pArgs); saying Exception thrown at 0x00007FFC54B6F3D2 (python37.dll) in ConsoleApplication9.exe: 0xC0000005: Access violation reading location 0x0000000000000008.
  Unhandled exception thrown: read access violation.
  <strong>v</strong> was nullptr.</p>
  <p>I just want to know if im missing any thing in my visual studio setup</p>
  <p>Because when i change the sample.py to just following without the #import, it works fine</p>
  <pre><code>def fun():  print(&quot;Hello from a function&quot;)
  </code></pre>