简体   繁体   English

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

[英]Import error when trying to execute a python script from C++ in Visual Studio 2013-15

I am currently working with Keras for a gesture recognition project. 我目前正在与Keras合作进行手势识别项目。 So I installed Anaconda and Python 2.7 and I made some script that work well with Spyder. 所以我安装了Anaconda和Python 2.7,我制作了一些与Spyder配合得很好的脚本。

After this, I wanted to use them from my main software that is a Visual Studio 2015 C++ project. 在此之后,我想从我的主要软件即Visual Studio 2015 C ++项目中使用它们。 So I use "python.h" for this but I have import problem . 所以我使用“python.h”,但我有导入问题

For example, if I try to execute this code : 例如,如果我尝试执行此代码:

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();

This is what I will get as an output : 这是我将作为输出得到的:

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

And here is "test.py" : 这是“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()))

I am still a beginner in Python so I see that there is some lib missing but I don't understand why it work in Spyder but not here. 我仍然是Python的初学者,所以我看到有一些lib缺失,但我不明白为什么它在Spyder中工作但不在这里。

Thanks for your attention ! 感谢您的关注 !

Ok I fixed it. 好的,我修好了。 In fact I had two pythons versions on my computer and Visual Studio took the wrong one. 事实上,我的计算机上有两个pythons版本,而Visual Studio则使用了错误的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Visual Studio C ++解决方案调用Python脚本 - Calling Python Script from Visual Studio C++ Solution 尝试从命令行执行脚本时出现Python语法错误 - Python syntax error when trying to execute a script from command line Python 3 导入错误 AttributeError: '_ModuleLock_' object 从 c++ 调用脚本时没有属性 'name' - Python 3 Import error AttributeError: '_ModuleLock_' object has no attribute 'name' when script invoked from c++ python 来自 Visual Studio 中的 c++ - python from c++ in visual studio ImportError:从 C++ 运行 python 文件时,无法在 Visual Studio 调试控制台中导入名称“etree” - ImportError: cannot import name 'etree' in Visual Studio Debug console while running python file from C++ 从C ++中的PATH导入Python脚本 - Python script import from PATH in C++ C++ DLL 使用 SWIG 和 Visual Studio 2017 导入 python - C++ DLL import in python with SWIG and Visual Studio 2017 Visual Studio 2013 中的 Python 无模块错误 - Python No module Error with Visual Studio 2013 如何在Visual Studio Code上执行Python脚本 - How to execute a Python script on Visual Studio Code 当dll具有嵌入式Python解释器时,将函数从dll(用C ++编程)导入Python脚本 - Import functions from dll (programmed in C++) into Python script when the dll has an embedded Python interpreter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM