嵌入式 Python 无法使用 NumPy 指向 Python35.zip - 如何修复?

Embedded Python does not work pointing to Python35.zip with NumPy - how to fix?

好的,下面是来自 Python 网站的基本示例,用于简单的 runpy.exe 到 运行 Python 脚本。在引用 Python 包含并链接到 python35.lib 以获得基本功能后,在 x64 Windows 上使用 Visual Studio 2015 可以正常工作(文档没有提到 pyvenv.cfg 必须在 EXE 目录中)。但是,仅在使用嵌入式 python35.zip 时调用导入 NumPy 的脚本会导致此错误 ImportError: No module named 'numpy' Failed to load "eig",那么如何在嵌入式 Python EXE 中包含 NumPy ? IE。我还想 "embed" 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.zippython35.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

这不起作用,因为 numpy 不在压缩文件中 python35.zip。 runpy-program 将路径设置为 python35.zip:因此它是该程序异常的 Pythonpath 中的唯一路径... 您还必须将本地 numpy 文件夹的父文件夹添加到 Pythonpath 才能使其正常工作。