why i get "no attirubute error" when i try to call python function from c++

badcode

Hi' i follow this tutorial to call the python function from a c++ code.

This is embeddedPython.cpp

#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <iostream>

int
main(int argc, char *argv[])
{
    PyObject *pName, *pModule, *pFunc;
    PyObject *pArgs, *pValue;
    int i;

    if (argc < 3) {
        fprintf(stderr,"Usage: call pythonfile funcname [args]\n");
        return 1;
    }

    Py_Initialize();
    pName = PyUnicode_DecodeFSDefault(argv[1]);
    /* Error checking of pName left out */
    std::cout << pName << std::endl;
    pModule = PyImport_Import(pName);
    Py_DECREF(pName);

    if (pModule != NULL) {
        pFunc = PyObject_GetAttrString(pModule, argv[2]);
        /* pFunc is a new reference */
        // didn't add the all code 
    }
    return 0;
}

And test.py

def multiply(a,b):
    print("Will compute", a, "times", b)
    c = 0
    for i in range(0, a):
        c = c + b
    return c

And finally CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
project(demo)
find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
add_executable(demo embeddedPython.cpp)
target_link_libraries(demo ${PYTHON_LIBRARIES})

when I try to run the following command:

./demo test multiply 3 2

I get the following error.

AttributeError: module 'test' has no attribute 'multiply'
Cannot find function "multiply"

What could be the problem? I think; I can access the file but the code can't find the function.

Mathias Schmid

The name of your Python file is test.py but there is already a Python module test for regression tests (see Python documentation) which doesn't contain a multiply() function. Rename your python file to avoid collision with existing modules. The example in the tutorial uses multiply.py which works. I used test1.py which works too.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

python: why it says undefined variable when I call function?

Why am I getting a NameError when I try to call my function?

Slumber Python library fails when I try to make get call when URL had dashes(-)

Why I get "ECONNRESET" when I try to get an element from a remote html page

I get a syntax error when I try to use array map function in Google Scripts. Why?

Why does the IDE give a warning when I try to call the constructor from a member function?

Why do I get a segfault when calling my C++ function with .Call rather than .C?

why is it when I try to create a child class from a parent class(Downcasting), I get a null object

Why I get exception when I try to fetch data from tables that has many to many relationship

Data from API call are stored in a Array but when i try to use that array in a function for further use it shows that array is empty . why?

Why is my python function not working properly when I call it recursively?

Undefined reference error when I try to call compiled NASM function from C program

Why do I get a message saying "Object is not a function" when I try to do an apply?

Why do I get error when I try to alert the length of the passed array to the function?

Why do I get an error (Notice: Undefined index) when I try to echo a value from my database?

Why am I getting an error when I try to get a string from my table in a Jframe?

Why do I get 'undefined' error when I try to read session atrribute from Controller

Why do I get an error message of NoneType object is not callable when I try to call this function?

Why is involving a special character from another language creating a crash when I try to call api in iOS SWIFT?

Why don't I get an input when I call the function?

Why do I get 'Next is not a function' when I try to export a Middleware function to module.exports object?

Why am I getting an 'undefined' when I try to call my function?

Error occurs when I try to call an inherited function in C++

Why is an AccessViolationException thrown when I call an imported C++ function from C# code?

Why do I get an Error when I try to rename a table after FROM?

Node js: Why do I get ```TypeError: Cannot read property 'json' of undefined``` when I call an async function from an express route?

Can anyone tell me why I get a error when i try to call this function?

Why is this function return empty result when I try to get column name from a table in PostgreSQL?

Why my state is empty when I try to access it from a function?