嵌入式 Python 指向 Python35 不起作用.zip 与 NumPy - 如何修复

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

本文关键字:NumPy 何修复 zip Python 指向 Python35 不起作用 嵌入式      更新时间:2023-10-16

好的,这是来自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中才能使其正常工作。