VS调用Python函数的方法
    
   
    
     Yutai Sun
    
   
    VS修改为Release x64
    
    VS项目属性→
    
    VC++目录→
    
    包含目录:
    
     
      
       P 
A
T
H
        PATH
      
      
       
        
        
        
         P
        
        
         A
        
        
         T
        
        
         H
        
       
      
     
    
    \include
    
    引用目录:
    
     
      
       P 
A
T
H
        PATH
      
      
       
        
        
        
         P
        
        
         A
        
        
         T
        
        
         H
        
       
      
     
    
    \libs
    
    链接器→
    
    输入/附加依赖项:
    
     
      
       P 
A
T
H
        PATH
      
      
       
        
        
        
         P
        
        
         A
        
        
         T
        
        
         H
        
       
      
     
    
    \libs\python37.lib(请根据自身实际选择Python版本)
   
    附言:
    
     
      
       P 
A
T
H
        PATH
      
      
       
        
        
        
         P
        
        
         A
        
        
         T
        
        
         H
        
       
      
     
    
    为Anaconda或其虚拟环境的路径,请将上述的所有
    
     
      
       P 
A
T
H
        PATH
      
      
       
        
        
        
         P
        
        
         A
        
        
         T
        
        
         H
        
       
      
     
    
    改为形如下列的形式F:\Anaconda\Anaconda3\envs\tensorflow(虚拟环境)
   
将自己要运行的神经网络写进Python脚本Sample.py,并将脚本放入VS项目文件夹下x64/Release文件夹下。
将下列代码根据注释含义放入自己的VS项目代码中:
#include "Python.h"
#include <iostream>
int main()
{
	// Initialize the Python interpreter.
	Py_SetPythonHome(L"E:/Anaconda3");
	Py_Initialize();
 
	// Create some Python objects that will later be assigned values.
	PyObject  *pModule, *pDict, *pFunc_add, *pFunc_hello, *pArgs, *pValue;
	
	// Import the file as a Python module.
	pModule = PyImport_ImportModule("Sample");
	
	// Create a dictionary for the contents of the module.
	pDict = PyModule_GetDict(pModule);
 
	// Get the add method from the dictionary.
	pFunc_add = PyDict_GetItemString(pDict, "add");
	pFunc_hello = PyDict_GetItemString(pDict, "hello");
 
	// Create a Python tuple to hold the arguments to the method.
	pArgs = PyTuple_New(2);
 
	// Convert 2 to a Python integer.
	pValue = PyLong_FromLong(2);
 
	// Set the Python int as the first and second arguments to the method.
	PyTuple_SetItem(pArgs, 0, pValue);
	PyTuple_SetItem(pArgs, 1, pValue);
 
	// Call the function with the arguments.
	PyObject_CallObject(pFunc_hello, NULL);
	PyObject* pResult = PyObject_CallObject(pFunc_add, pArgs);
 
	// Print a message if calling the method failed.
	if (pResult == NULL)
		printf("Calling the add method failed.\n");
 
	// Convert the result to a long from a Python object.
	long result = PyLong_AsLong(pResult);
 
	// Destroy the Python interpreter.
	Py_Finalize();
 
	// Print the result.
	printf("Calling Python to find the sum of 2 and 2.\n");
	printf("The result is %d.\n", result);
	std::cin.ignore();
	return 0;
}
    
     文件输入输出:
    
    
    这时的文件输入输出,请使用
   
freopen(“in.txt”,”r”,stdin);freopen(“out.txt”,”w”,stdout);
    结束使用后用
    
    fclose(stdin);fclose(stdout);
    
    来关闭输入输出流。
    
    C++的文件输出并非实时输出,因此在关闭输出流之前,您还应该添加这句话来清空缓冲区:fflush(stdout);
   
此后,如果您还需要在命令行中输出,请在fclose语句之后加上
freopen(“CON”,”r”,stdin);freopen(“CON”,”w”,stdout);以恢复输出在命令行上。
    
     参考资料:
    
    
    https://blog.csdn.net/shanwenkang/article/details/88954652?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2
    
     all
    
    first_rank_v2~rank_v25-1-88954652.nonecase&utm_term=c++%E8%B0%83%E7%94%A8anaconda
   
https://blog.csdn.net/weixin_30588675/article/details/97496254?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param
 
