Click here to Skip to main content
15,888,313 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.


C++
const char* file_location = location.c_str();
FILE* file_pointer;
// Initialize the Python interpreter
Py_Initialize();
file_pointer = _Py_fopen(file_location, "r");
// Run the Python file
PyRun_SimpleFile(file_pointer, file_location);
// Finalize the Python interpreter
Py_Finalize();


This is the code I have in my application. this code is working fine when I use some common programs like this,

Python
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,

Python
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,

C++
bool pycompile::execute_python(std::string location)
{
	if (std::ifstream(location))
	{
		try {
			const char* file_location = location.c_str();
			FILE* file_pointer;
			// Initialize the Python interpreter
			Py_Initialize();
			file_pointer = _Py_fopen(file_location, "r");
			// Run the Python file
			PyRun_SimpleFile(file_pointer, file_location);
			// Finalize the Python interpreter
			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:

Python
print("Python from C++")


Result:

works well

test 2 with different file



Python
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
Posted
Updated 1-Mar-17 11:14am
v3

 
Share this answer
 
Comments
[no name] 1-Mar-17 7:56am    
sir, I have tried Py_SetProgramName but that too is not helpful
Richard MacCutchan 1-Mar-17 8:29am    
You still have not shown the code where your error occurs. It is no use just guessing that adding some random line of code will fix your problem. You need to diagnose exactly what is failing.
[no name] 1-Mar-17 8:52am    
seriously the code posted in my question is what I actually tried this is the error baseName = os.path.basename(sys.argv[0])
AttributeError: module 'sys' has no attribute 'argv'
Richard MacCutchan 1-Mar-17 8:57am    
That code does not appear anywhere in your question, hence my comments.

So what is it about the error message that you do not understand? It quite clearly states that sys does not have an attribute named argv. What is sys and where is it defined?

Please edit your question and show all the details connected with your error, rather than a few random lines of code that are not connected with it.
[no name] 1-Mar-17 9:09am    
sir
from tkinter import *
root = Tk()
root.mainloop()

That three lines of code is actually contained in the file. Nothing more only three lines when I try to execute it the error shows. I have not defined anything more. I ran only three lines of code in testing
It appears that the arguments list is undefined, not sure if this nuance is common to all "embedded" Python versions (i.e. you're calling Python via an API instead of running natively).... but you may be able to get around the issue by setting argv list via the PySys_SetArgv() call:
c++ - "AttributeError: 'module' object has no attribute 'argv'" when using Python.h - Stack Overflow[^]
 
Share this answer
 
Comments
[no name] 2-Mar-17 8:47am    
Thank you sir for your solution, PySys_SetArgv() is really working and I am running the python scripts. Thank you so much.
Member 14169974 4-Mar-19 1:38am    
Hi, would you please upload your code which are used for sovle this problem? I tried :
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
PySys_SetArgv(argc, argv);
}
and I got an error:
/home/stefan/QtObjects/Vispy_based_MeshLab/main.cpp:47: error: cannot convert 'char**' to 'wchar_t**' for argument '2' to 'void PySys_SetArgv(int, wchar_t**)'
PySys_SetArgv(argc, argv);
I don't know how to solve this, Please help me,sir.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900