i compile and run your "call_thread" example under Visual Studio 2013 and windows 10 64 bits, but i have crash at the end just after the screen displays the last "...Printed from my thread."
i don't have the the two last lines (My thread is finishing... and Main thread finished gracefully)
i tried to replace WIN32 by _WIN64 in the code but no more help.
i put at :
// Windows code
printf("Waiting for My Thread to complete");
printf("My Thread completed.");
and see :
...Printed from my thread.
Main Thread waiting for My Thread to complete...
Waiting for My Thread to complete.
...Printed from my thread.
so crash at WaitForSingleObject()? Have you any idea to solve it?
This page looks like you cut and pasted right from the Python documentation. it's completely useless. First off, people who embed python into c++ don't want to run python scripts from the command line - that's what py.exe and python.exe are for. No, instead, people want to run logic scripts at runtime that are dynamic. To that end, perhaps you could forgo the argc nonsense and use a real world example. Nothing too fancy, just something to show how it's done. For example, does the script name have to be a fully qualified path? and if not, where does the script file need to be in relation to the location of the c++ exe? I have put my scripts right in the same folder as the exe and your example still doesn't work, so obviously you need to provide more explanation.
I taught myself how to embed Python in C++ by self studying Jun Du's code and the docs on python.org. But none of that has left me with the feeling that I know how to tell whether particular C/Python API functions return borrowed references --- other than digging thru the source code of the functions. For now, I am blindly going by what Jun Du did which is to not Py_DECREF pCls, pDict, and pFunc.
It seems to me so obvious that the API reference documentation should explicitly state whether or not each API function returns a borrowed reference. Is there some cogent reason why this is not being done?
First of all this is a nice and very helpful article. I love it. Thanks to authors.
I wrote a small code following the code presented here. But I am getting this error: This is my error log
g++ -c -o main.o main.cpp
g++ -c -o c_test.o c_test.cpp
c_test.cpp: In constructor ‘pythonTest::pythonTest(char*)’:
c_test.cpp:10:41: error: ‘PyString_FromString’ was not declared in this scope
c_test.cpp: In member function ‘long int pythonTest::callMethod(char*, int)’:
c_test.cpp:28:29: error: ‘PyInt_FromLong’ was not declared in this scope
c_test.cpp:40:38: error: ‘PyInt_AsLong’ was not declared in this scope
make: *** [c_test.o] Error 1
I am using python3.2mu/Python.h on Ubuntu 14.04LTS.
Interesting thing is PyModule_GetDict and PyImport_Import are being detected but not the others. I am not sure if I am missing any detail. Any help ... ?
Need some help embedding Python in C++, My system:
Windows 7 64bit
I've already included Python27/lib and Python27/include in my VC++ additional include directories. In addition, I was using "release" mode. However, I still have the fatal error:
LINK : fatal error LNK1104: cannot open file 'python27.lib';
I found out this article is very useful for my master project and everything is working perfect.But I have a question how can I pass an array from c++ to python function?besides, how can I retrieve the python returned numpy array to c++?hope you can help me and give me some hints.
The code is very useful it helps me to understand how to use python in C++
I run into a problem when I want to pass a tuple declared in C++ to a method of a class declared in python
it doesn't work;(get a run failed!message)
If anyone has an idea it will be helpful
self.value = msg
#don't use vecData for instance
result = 12.
I've being trying these last few days to use the Python.h in a Windows Forms Application with no success.
From searching the web I think the problem is because Windows Forms are managed c++ and there for can not be used with the native c header - Python.h
Is this true?
Is there a way to get it to work?
Please read article "Embedding Python in Multi-Threaded C/C Applications" (Linux Journal)to which is referred.
The information in this article (which is correct) is not copied right.
The multi-threaded solution is wrong. It only works when running it in 1 thread as is done.
I've compiled your code with VS2005 and got the following errors:
1>call_function.obj : error LNK2019: unresolved external symbol _PyInt_AsLong referenced in function _main
1>call_function.obj : error LNK2019: unresolved external symbol _PyInt_FromLong referenced in function _main
1>call_function.obj : error LNK2019: unresolved external symbol _PyString_FromString referenced in function _main
1>Executable.exe : fatal error LNK1120: 3 unresolved externals
I am using this code of yours in my project of calling a python function from my C file.
I used your code along with the 2 lines to modify the python path and your example worked.
Now, if I just add this line: "import time" in the python file, then again I get segmentation fault.
How to solve this?
Thanks and a really good article..
Hi and thanks for this great article!
When I load a python file which is for example in "Script" directory, I get an error by
"pModule = PyImport_ImportModule(pName);" which says "ImportError: Import by filename is not supported."
How can I fix this and import a python file from somewhere other than where the exe is?