Good morning sir,
I am willing to embed Python in my C++ application, for that I have referred several articles on this site. Finally, I had some success in embedding python in my application.
const char* file_location = location.c_str();
FILE* file_pointer;
Py_Initialize();
file_pointer = _Py_fopen(file_location, "r");
PyRun_SimpleFile(file_pointer, file_location);
Py_Finalize();
This is the code I have in my application. this code is working fine when I use some common programs like this,
print("Testing the woking from Python file")
print("works good...")
print(1+2)
But when I try to import any modules for example tkinter even basic program throws exception,
This program throws,
from tkinter import *
root = Tk()
root.mainloop()
AttributeError: module 'sys' has no attribute 'argv'
I am using Python 3.5.2 and VS2015
Actual Process Happening:
This is the method which executes the Python file,
bool pycompile::execute_python(std::string location)
{
if (std::ifstream(location))
{
try {
const char* file_location = location.c_str();
FILE* file_pointer;
Py_Initialize();
file_pointer = _Py_fopen(file_location, "r");
PyRun_SimpleFile(file_pointer, file_location);
Py_Finalize();
}
catch (...)
{
std::cout << "exception";
}
return true;
}
return false;
}
What the above code does
Once the location of the file is passed the it executes
Contents of Python file 1:
print("Python from C++")
Result:
works well
test 2 with different file
from tkinter import *
root = Tk()
root.mainloop()
I get this error,
Quote:
root = Tk()
File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\Lib\tkinter\__init__.py", line 1863, in __init__
baseName = os.path.basename(sys.argv[0])
AttributeError: module 'sys' has no attribute 'argv'
What I have tried:
What I have tried is :
1. Referred Google and found some kind of information like making sure Python is in Path., etc.,
2. Copied and pasted the dll's like tk86t.dll and tcl86t.dll for tkinter near the executable but nothing works.
I dont know sir what to do kindly help me Thank you