Click here to Skip to main content
15,887,344 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good Morning Sir,

I have referred this article, Embedding Python program in a C/C++ code[^] I have followed up the tutorial and successfully embedded Python in my code.

Python
PyRun_SimpleString("print('Hello World from Embedded Python!!!')");


s used to execute a single line of Python code but how do I run a multiple lines of code in Python. I also tried running a Python from a file but the file also needed to be there for running the executable. Cry | :(( I dont want a file along with my executable, I searched in the google sir but I find no results.

I am confused here, I dont know what to doConfused | :confused:

Thank you sir for your time

What I have tried:

1. tried to contact the author of the article, waited for a month and he is unresponsive.

2. Tried some Googling but I don't find anything relevant, and I am still waiting for the author to respond.

Any help is much appreciated

Thank you sir for your time!
Posted
Updated 10-Jan-17 7:03am
Comments
Richard MacCutchan 10-Jan-17 12:04pm    
You can only do what the article provides. If you want something extra then you will need to modify the provided code.
[no name] 10-Jan-17 12:53pm    
Sir, I dont understand your comment, I the code is working without any problem but the python file should be present where the executable is placed, the executable will not work if the python file is not present there, Is there any way to make the executable working without actually revealing the code? Thank you sir for your time excuse my English
Richard MacCutchan 10-Jan-17 12:59pm    
No the python file must be accessible to the executable program. Alternatively you could store it in encrypted form in a resource in your program. Then when required, you decrypt it to a temporary file, and run it from the decrypted copy.
[no name] 11-Jan-17 0:10am    
Thank you sir for your kind solution

1 solution

Well, you could simply turn the python file into binary and compile/link it into your program binary. At run time, take the compiled binary text (which should be some const char* at this point) and feed it to Python as a string.

This approach however, makes your application have to compile every time you need any Python changes... which loses a big part of the desirability of using Python in the first place. In another words... at that point why use Python at all?
 
Share this answer
 
v2
Comments
[no name] 11-Jan-17 0:10am    
Thank you sir for your kind solution, Python has good and easy networking libraries so why I asked. Thank you for your solution
Albert Holguin 11-Jan-17 10:58am    
You know, C++ has pretty good/easy networking libraries as well. Heck, there are a handful of libraries that have interfaces/bindings for both C++ AND Python (i.e. ZeroMQ[^]).
[no name] 14-Jan-17 3:48am    
Oh, Thank you sir for your kind help

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