好的,这是来自 Python 网站的基本示例,用于简单说明runpy.exe
运行下面的 Python 脚本。引用 Python 包含并链接到后,在 x64 Windows 上使用 Visual Studio 2015 可以正常工作python35.lib
对于基本功能(文档没有提到pyvenv.cfg
必须位于 EXE 目录中)。但是,调用导入的脚本NumPy
导致这个错误ImportError: No module named 'numpy' Failed to load "eig"
仅当使用嵌入式时python35.zip
,那么如何包含NumPy
在嵌入式 Python EXE 中? IE。我还想“嵌入”NumPy(作为 .zip、目录、.dll 或 .pyd 等)。我尝试添加 NumPy 包含并链接到npymath.lib
但我得到同样的导入错误。我还研究了一些 Cython 包装器代码,但没有找到解决方案。下面是Python嵌入的示例代码:
#include <Python.h>
#include <iostream>
int main(int argc, char *argv[])
{
PyObject *pName, *pModule, *pDict, *pFunc;
PyObject *pArgs, *pValue;
int i;
if (argc < 3) {
fprintf(stderr, "Usage: runpy pythonfile funcname [args]\n");
return 1;
}
Py_SetPath(L"python35.zip"); //this is in the current directory
Py_Initialize();
pName = PyUnicode_DecodeFSDefault(argv[1]);
/* Error checking of pName left out */
pModule = PyImport_Import(pName);
Py_DECREF(pName);
if (pModule != NULL) {
pFunc = PyObject_GetAttrString(pModule, argv[2]);
/* pFunc is a new reference */
if (pFunc && PyCallable_Check(pFunc)) {
pArgs = PyTuple_New(argc - 3);
for (i = 0; i < argc - 3; ++i) {
pValue = PyLong_FromLong(atoi(argv[i + 3]));
if (!pValue) {
Py_DECREF(pArgs);
Py_DECREF(pModule);
fprintf(stderr, "Cannot convert argument\n");
return 1;
}
/* pValue reference stolen here: */
PyTuple_SetItem(pArgs, i, pValue);
}
pValue = PyObject_CallObject(pFunc, pArgs);
Py_DECREF(pArgs);
if (pValue != NULL) {
printf("Result of call: %ld\n", PyLong_AsLong(pValue));
Py_DECREF(pValue);
}
else {
Py_DECREF(pFunc);
Py_DECREF(pModule);
PyErr_Print();
fprintf(stderr, "Call failed\n");
return 1;
}
}
else {
if (PyErr_Occurred())
PyErr_Print();
fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
}
Py_XDECREF(pFunc);
Py_DECREF(pModule);
}
else {
PyErr_Print();
fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);
return 1;
}
Py_Finalize();
return 0;
}
嵌入文件在这里:https://www.python.org/ftp/python/3.5.2/python-3.5.2-embed-amd64.zip, python35.zip
档案内。这是简单的测试脚本(runpy eig eig 10
测试 - 如果您不嵌入,请注意Python35.zip
并有NumPy
/ SciPy
安装后它将运行):
eig.py
import numpy as np
from scipy import linalg
def eig(a):
c = np.random.rand(a,a)*100
c = np.corrcoef(c)
print('You are taking the eigsh of a ', a, '^2 matrix')
e, f = linalg.eig(c)
return print('Eigvals are: ',np.diag(f))
有人知道如何解决这个问题吗?非常感激。
更新:这是包含 Intel MKL 的编译版本 x64 Python 3.5 Windows NumPy SciPy 和 Pandas:https://www.dropbox.com/sh/2smbgen2i9ilf2e/AADI8A3pCAFU-EqNLTbOiUwJa?dl=0