Visual Studio cant find my python file.

Bratcher, Howard 1 Reputation point
2021-04-11T19:43:14.503+00:00

I am trying to link python and C++ in visual studio. I am hopefully in the right place, maybe I am not using the right words because I cant find an article on this exact subject anywhere. It seems like I can't get my compiler to find my file (in C++) that contains the python code it is supposed to call. does anyone have any ideas?

C++ code:

#include <Python.h>
#include <iostream>
#include <string>

using namespace std;

void main()
{
    cout << "Start 1 \n";
Py_Initialize();
cout << "2\n";
PyObject* my_module = PyImport_ImportModule("mypy");
cerr << my_module << "\n";
PyErr_Print();
cout << "3\n";
PyObject* my_function = PyObject_GetAttrString(my_module, "printsomething");
cout << "4\n";
PyObject* my_result = PyObject_CallObject(my_function, NULL);
Py_Finalize();
}

python code:

import re
import string

def printsomething():
    print( 'Hello from Python!' )

Error Message:
ModuleNotFoundError: No module named 'mypy'

output:

enter image description here

The program its self runs, but I can't understand why this happens, it can't find the file with the python code.

Developer technologies C++
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.