How to insert Python3 with a standard library

I am trying to implement Python in a (ultimately multi-platform) C ++ application.

It is important that my application contains its own implementation of Python (just like blender does), so that it is completely self-contained. (Otherwise, it becomes the configuration minefield).

I have two options:

  • Trying to embed Python3 without the standard library (which I asked here )

  • Trying to embed Python3 with a standard library.

What is required for (2)?

With this information, I can balance the merits of each approach with respect to the effort required to configure it.

My built-in Python will be for my own use (and not any custom script) - basically controls the flow / game logic. I need very little from the standard library - maybe I can undo this to 0 by tunneling back to C ++ when necessary, for example, if I need a random number, I can create a C ++ routine and access this from Python . Everything is covered with me.

However, it seems that even a minimal installation should contain some stdlib components that ask the question: "If I have to include some, it might be better to include everything!"

+9
python standard-library embedding
Jan 11 '16 at 15:15
source share
3 answers

I believe that you already double-check how to embed Python in another application ( here you will see something that will cover the python2 attachment, but will be true for python3 as well, in my opinion)

There are various types of attachments:

  • Very high level implementation
  • Outside of a very high level investment
  • Net investment
  • Python embedding in C ++

Since your question is about "Embedding Python in C ++", you can read the following:

It is also possible to embed Python in a C ++ program; exactly how it will depend on the details of the C ++ system used ; in you will need to write a basic C ++ program and use C ++ to compile and link your program. There is no need to recompile Python itself using C ++.

As one hand you said “(ultimately, multi-platform) C ++ application” and on the other hand you have “exactly how this is done will depend on the details of the C ++ system used,” so you can explain more information about Used C ++ system?

You can also find some tips here regarding using the pybind11 module or another old page that talks about how to implement Python and import modules in C / C ++ (python2.6, but I hope you can find inspiration)

Finally:

You obviously need Python development packages to have Python include a directory

0
Apr 04 '17 at 23:09 on
source share

You are looking for Boost.Python !

This is a C ++ library that provides seamless interoperability between the C ++ and Python programming language and, in my opinion, this should satisfy your needs if you are not trying to achieve something else.

It also has a mechanism for embedding the python interpreter in C ++ code, and you can link to this link (the URL is not release specific) to delve into the possibilities.

PS I believe less in reinventing the wheel and much more in reuse.

-one
Feb 22 '17 at 22:24
source share

Since this does not really have an answer, I will offer it for posterity. I also do not have access to a Mac, so for you this might be a little different than Linux. In addition, for this it is necessary to establish the required dependencies so that this can be done, but it is usually quite easy to understand.

Create working directory

mkdir ~/embeddedpython cd ~/embeddedpython 

Download Python Source

 wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz 

Create an installation directory for Python

 mkdir ./installation 

Extract downloaded source files

 tar xvf Python-3.6.1.tgz 

Enter the newly created source directory

 cd Python-3.6.1 

Configure Python to install in our installation directory

 ./configure --prefix="/home/<username>/embeddedpython/installation" 

Make and Install Python

 make && make install 

Return to the working directory

 cd .. 

Create a new PYTHONHOME directory where the library will be located

 mkdir home && mkdir home/lib 

Copy the Python library to our new home directory

 cp -r ./installation/lib/python3.6 ./home/lib/ 

Create a new C ++ source file (embeddedpython.cpp) with the following code taken from the python documentation , except for the setenv function call.

 #include <Python.h> #include <cstdlib> int main(int argc, char *argv[]) { setenv("PYTHONHOME", "./home", 1); wchar_t *program = Py_DecodeLocale(argv[0], NULL); if (program == NULL) { fprintf(stderr, "Fatal error: cannot decode argv[0]\n"); exit(1); } Py_SetProgramName(program); /* optional but recommended */ Py_Initialize(); PyRun_SimpleString("from time import time,ctime\n" "print('Today is', ctime(time()))\n"); if (Py_FinalizeEx() < 0) { exit(120); } PyMem_RawFree(program); return 0; } 

Compile and run

 g++ embeddedpython.cpp -I ./installation/include/python3.6m/ ./installation/lib/libpython3.6m.a -lpthread -ldl -lutil ./a.out > Today is Fri Apr 14 16:06:54 2017 

This is now the standard built-in python, as usual. Using this method, the "home" directory must be included in your deployment, and the PYTHONHOME environment PYTHONHOME must be set to point to it before any python-related code is executed.

-one
Apr 14 '17 at 22:12
source share



All Articles