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

标签 c++ python-3.x embed python-embedding

好的,这是来自 Python 网站的基本示例,用于简单的 runpy.exe运行下面的 Python 脚本。在引用 Python 包含并链接到 python35.lib 后,它在 x64 Windows 上使用 Visual Studio 2015 工作正常对于基本功能(文档没有提到 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

最佳答案

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

关于c++ - 嵌入式 Python 无法使用 NumPy 指向 Python35.zip - 如何修复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38333320/

相关文章:

c++ - 我需要帮助在 C++ 中创建优先级队列

c++ - 从 Truetype 字体中提取 SplineSet/其他信息

python - 相对于文本而不是边界框对齐任意旋转的文本注释

youtube - 嵌入YouTube channel 中的热门视频

php - 嵌入式 iframe - 验证 GET 请求的来源/来源

c++ - 带有 async_read 的 boost.asio 多线程 I/O 服务器

c++ - 是否可以使用 C++/WinRT 创建 Windows 服务应用程序?

python-3.x - Locust - 如何为同一用户定义多个任务集?

python-3.x - 将对象转换为 Int Pandas

powershell - 如何通过嵌入网页或powershell运行Gist来解决ssl证书错误?