[英]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.