首页 > 解决方案 > PyINIT 没有被调用

问题描述

在 Visual Studio 2019 中,尝试运行本教程 https://docs.microsoft.com/en-us/visualstudio/python/working-with-c-cpp-python-in-visual-studio?view=vs-2019 除外我称我的模块为 twitc。

初始化模块的代码永远不会被调用。

import twitc

工作正常。
但是当我尝试调用 twitc.generate_twit_list(0.1) 它说

module 'twitc' has no attribute 'generate_twit_list'

我的代码喜欢

#include <Python.h>
#include <Windows.h>
#include <cmath>

const double e = 2.7182818284590452353602874713527;

double sinh_impl(double x) {
    return (1 - pow(e, (-2 * x))) / (2 * pow(e, -x));
}

double cosh_impl(double x) {
    return (1 + pow(e, (-2 * x))) / (2 * pow(e, -x));
}

PyObject* generate_twit_list_impl(PyObject*, PyObject* o) {
    double x = PyFloat_AsDouble(o);
    double tanh_x = sinh_impl(x) / cosh_impl(x);
    return PyFloat_FromDouble(tanh_x);
}

static PyMethodDef twitc_methods[] = {

    { "generate_twit_list", (PyCFunction)generate_twit_list_impl, METH_VARARGS, "Generate the twit list of inter tensor links as a cache." },
    { nullptr, nullptr, 0, nullptr }
};

static PyModuleDef twitc_module = {
    PyModuleDef_HEAD_INIT,
    "twitc",
    "Provides some functions, but faster",
    0,
    twitc_methods
};

PyMODINIT_FUNC PyInit_twitc() {
    printf("Init TWITC\n"); // THIS NEVER GETS CALLED
    return PyModule_Create(&twitc_module);
}

任何想法为什么这会失败?

标签: pythonc++

解决方案


已解决 - 我没有在 python37.lib 中链接现在它可以工作了。


推荐阅读