嵌入式 Python 指向 Python35 不起作用.zip 与 NumPy - 如何修复
Embedded Python does not work pointing to Python35.zip with NumPy - how to fix?
好的,这是来自Python网站的基本示例,用于运行下面的Python脚本的简单runpy.exe
。 在引用Python包含并链接到基本功能python35.lib
后,在x64 Windows上使用Visual Studio 2015可以正常工作(文档没有提到pyvenv.cfg
必须在EXE目录中)。 但是,调用导入NumPy
的脚本只会导致此错误,ImportError: No module named 'numpy' Failed to load "eig"
使用嵌入式python35.zip
,那么如何在嵌入式Python EXE中包含NumPy
呢? 即我还想"嵌入"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 argumentn");
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: %ldn", PyLong_AsLong(pValue));
Py_DECREF(pValue);
}
else {
Py_DECREF(pFunc);
Py_DECREF(pModule);
PyErr_Print();
fprintf(stderr, "Call failedn");
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))
有人知道如何解决这个问题吗? 非常感谢。
更新:这是编译版本x64 Python 3.5 Windows NumPy SciPy和Pandas,包括Intel MKL:https://www.dropbox.com/sh/2smbgen2i9ilf2e/AADI8A3pCAFU-EqNLTbOiUwJa?dl=0
不起作用,因为 numpy 不在 zipfile python35.zip
中。runpy-program将路径设置为python35.zip
:因此,它是Pythonpath中此程序例外的唯一路径...您必须将本地numpy文件夹的父文件夹也添加到Pythonpath中才能使其正常工作。
- 静态数据成员的问题-修复链接错误会导致编译器错误
- Boost Graph Library,修复节点大小
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 如何修复函数样式强制转换或类型构造的预期"("?
- 如何针对特定情况调试和修复此双自由内存损坏问题
- 当使用带有VS2019或VSCode的虚幻引擎4.24.2时,我如何修复这些错误的Intellisense错误
- 如何修复sfml c++代码编译错误
- Python中的for循环与C++有何不同
- 如何修复此iFile以将.txt输出到控制台
- 我收到同义重复编译器错误。我应该如何修复"类型"X"的参数与类型"X"的参数不兼容?
- 如何修复链表类实现的未处理异常0xDDDDDDDD
- 如何修复此错误:className::className的无效使用
- 叮叮当当在修复时插入多个"覆盖"说明符
- 如何修复带有 clang 的参数'args'缺少默认参数的问题?
- 如何修复 Onnx运行时会话>运行问题?
- C++返回 Numpy 数组的 Python 扩展模块
- 当我尝试加载内核模块时,如何修复C++中的这个 malloc() 错误?
- 如何修复编译器警告 C6386 和 C6385?
- 嵌入式 Python 指向 Python35 不起作用.zip 与 NumPy - 如何修复