在C ++上嵌入Python的问题 [英] Problem wih embeding Python on C++

查看:110
本文介绍了在C ++上嵌入Python的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,先生,

我愿意在我的C ++应用程序中嵌入Python,因为我在这个网站上引用了几篇文章。最后,我在我的应用程序中嵌入python方面取得了一些成功。





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;
// 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,

print("Testing the woking from Python file")
print("works good...")
print(1+2)





但是当我尝试导入任何模块时,例如tkinter甚至基本程序抛出异常,



此程序抛出,





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:模块'sys'没有属性'argv'



我使用的是Python 3.5.2和VS2015



实际流程发生:



这是执行Python文件的方法,





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;
			// 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;
}





以上代码的作用



一旦文件的位置传递它执行



Python文件内容1:





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++")





结果:

运作良好



测试2使用不同的文件







Result:

works well

test 2 with different file



from tkinter import *
root = Tk()
root.mainloop()





我收到此错误,



I get this error,

引用:

root = Tk()

文件C:\ Users \User \ AppData \ Local \ Program•\\ Python \Python35-32 \Lib \ tkinter \\ \\ ___ ___。py,第1863行,在__init__

baseName = os.path.basename(s ys.argv [0])

AttributeError:模块'sys'没有属性'argv'

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'





我尝试了什么:



我试过的是:



1.推荐谷歌发现了一些信息,比如确保Python在Path等等,



2.复制并粘贴dll就像tk86t.dll和tcl86t.dll对于可执行文件附近的tkinter但没有任何作用。



我不知道先生该怎么做才能帮助我谢谢



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

推荐答案

请参阅 1。在另一个应用程序中嵌入Python - Python 3.4.5文档 [ ^ ]。


看来参数列表是未定义的,不确定这个细微差别是否对所有嵌入式Python版本都是通用的(即您通过以下方式调用Python一个API而不是本地运行)....但你可以通过PySys_SetArgv()调用设置argv列表来解决这个问题:

c ++ - AttributeError:'module'对象在使用Python时没有属性'argv'。 h - 堆栈溢出 [ ^ ]
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[^]


这篇关于在C ++上嵌入Python的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆