尝试在 Visual Studio 2013-15 中从 C++ 执行 python 脚本时出现导入错误

Posted

技术标签:

【中文标题】尝试在 Visual Studio 2013-15 中从 C++ 执行 python 脚本时出现导入错误【英文标题】:Import error when trying to execute a python script from C++ in Visual Studio 2013-15 【发布时间】:2016-06-01 06:26:21 【问题描述】:

我目前正在与 Keras 合作进行手势识别项目。 所以我安装了 Anaconda 和 Python 2.7,并制作了一些与 Spyder 配合良好的脚本。

在此之后,我想在我的主要软件(即 Visual Studio 2015 C++ 项目)中使用它们。 所以我为此使用“python.h”,但我有导入问题

例如,如果我尝试执行此代码:

PyObject *pName, *pModule, *pDict, *pFunc, *pValue;         // Initialize the Python Interpreter

Py_Initialize(); 

FILE *fd =  fopen("test.py","r");

PyFile_FromString("test.py", "r");

PyRun_SimpleFileEx(PyFile_AsFile(PyFileObject),"test.py", 1);   //file before returning.    

Py_Finalize();

cout << "Press any key to exit" << endl;    
_getch();

这是我将得到的输出:

Traceback (most recent call last):  
File "test.py", line 4, in <module>  
from numpy import numpy 

ImportError: cannot import name numpy 

Press any key to exit

这里是“test.py”:

from time import time,ctime 

from numpy import np 

np.random.seed(1337) 

//No need to add more code for this example 

print ('Today is',ctime(time()))

我仍然是 Python 的初学者,所以我看到缺少一些库,但我不明白为什么它在 Spyder 中可以工作,但在这里却不行。

感谢您的关注!

【问题讨论】:

【参考方案1】:

好的,我修好了。事实上,我的电脑上有两个 python 版本,而 Visual Studio 拿错了一个。

【讨论】:

以上是关于尝试在 Visual Studio 2013-15 中从 C++ 执行 python 脚本时出现导入错误的主要内容,如果未能解决你的问题,请参考以下文章

Visual Studio 2012 未能检测到 Visual Studio 2008 构建工具

为啥我在尝试使用 Visual Studio Analyzer 时遇到错误?

Swift for Visual Studio Code,尝试在 VS Code 上写 Swift

尝试在 Visual Studio 2012 中添加对项目的引用,“未找到项目”

尝试在 Visual Studio 2005 上运行使用 Oracle 库的应用程序

尝试linux下的debug工具: Visual studio