All Projects → matusnovak → python-embedded-example-project

matusnovak / python-embedded-example-project

Licence: Unlicense license
This is an example project using mebedded python in C++ console application using CMake

Programming Languages

CMake
9771 projects
C++
36643 projects - #6 most used programming language
python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to python-embedded-example-project

awesome-cpp-python-binding-generator
😎 A curated list of awesome automatic Python binding generators for C++ projects
Stars: ✭ 16 (-50%)
Mutual labels:  pybind11
pybind11-stubgen
Generates stubs for python modules (targeted to C++ extensions compiled with pybind11)
Stars: ✭ 103 (+221.88%)
Mutual labels:  pybind11
spdlog-python
python wrapper around C++ spdlog ([email protected]:gabime/spdlog.git)
Stars: ✭ 46 (+43.75%)
Mutual labels:  pybind11
Chaiscript
Embedded Scripting Language Designed for C++
Stars: ✭ 2,443 (+7534.38%)
Mutual labels:  embedded-scripting-language
wdte
WDTE is a simple, functional-ish, embedded scripting language.
Stars: ✭ 20 (-37.5%)
Mutual labels:  embedded-scripting-language
oak
A small embedded scripting language.
Stars: ✭ 14 (-56.25%)
Mutual labels:  embedded-scripting-language
pysdsl
Python bindings to Succinct Data Structure Library 2.0
Stars: ✭ 23 (-28.12%)
Mutual labels:  pybind11
pymonero
Python Monero, a fork of monero-project/monero to deliver python c++ bindings
Stars: ✭ 12 (-62.5%)
Mutual labels:  pybind11
Electronic-Cheat-Sheet-and-Schematics-MegaCollection
A lot of Files of various Electronic Shit that I have collected over the years. Cheatsheet, Schematics, Pinouts, Pdf, and More... Enjoy it ;)
Stars: ✭ 43 (+34.38%)
Mutual labels:  pybind11
neworder
A dynamic microsimulation framework for python
Stars: ✭ 15 (-53.12%)
Mutual labels:  pybind11
SymSpellCppPy
Fast SymSpell written in c++ and exposes to python via pybind11
Stars: ✭ 28 (-12.5%)
Mutual labels:  pybind11
chimera
🐍 A CLI tool for generating Boost.Python/pybind11 bindings from C/C++
Stars: ✭ 12 (-62.5%)
Mutual labels:  pybind11
python-poppler
Python binding to Poppler-cpp pdf library
Stars: ✭ 51 (+59.38%)
Mutual labels:  pybind11
pywrap
C++ binding generator based on libclang and pybind11
Stars: ✭ 17 (-46.87%)
Mutual labels:  pybind11
h264decoder
h264 decoding module for python based on libav
Stars: ✭ 76 (+137.5%)
Mutual labels:  pybind11
pynanoflann
Unofficial python wrapper to the nanoflann k-d tree
Stars: ✭ 24 (-25%)
Mutual labels:  pybind11

Python Embedded Example Project

This is an example project using mebedded python 3.8 in C++ console application using CMake. This example project also contains pybind11 library for easy binding between C++ and python.

Tested on Windows 10 with Visual Studio 2013, 2015, and 2017 (both x86 and x64). Also tested on Ubuntu with GCC (x64).

Note this example has nothing to do with embedding C++ in python! This is the other way around! Embeding python in C++.

About

Normally, you have a python installed in your system and then you can launch python scripts from command line. What if you want to create C++ application, but want to use python scripts (for example as modding for games)? One option is to let the user install python on their system and then install your app. No problem there, except, you are forcing the user to modify their system. Additonally, you can never guarantee that the user will install the specific python version (2.7 or 3.8) you require. Even if all of that is sorted out, the python that will get run will probably need third party packages (or you need to explicitly disable some packages), this can't be done easily.

What if we can embed entire python in C++ executable? This way, the user won't have to install python on their system nor additional dependencies. Everything will be bundled up in the executable.

This project comes with cpython 3.8 (as a git submodule) and pybind11. Once you build the project, your build folder will look like this:

python-embedded-example-project/
  build/
    Release/
      PythonEmbeddedExample.exe
      app/
        example.py
      lib/
        a lot of Python standard library files

The script files (such as example.py) are copied to the build directory from src/app and the python standard libraries are copied over from git submodule located in python-embedded-example-project/libs/cpython/Lib. You can't get rid of those standard libraries! Python needs them to initialise. However, you could limit the standard libraries by only selectively copying over the ones you need.

When the executable runs, it will load an example module and creates an instance of Example class from example.py file. That's all it does.

The python PATH is limited only to the app and lib folder located next to the executable. The embedded python won't have any access to installed packages in your operating system. You will have to include them in any of those two folders.

Requirements

Visual Studio 2013 (or newer) or Linux with GCC. MinGW is sadly not supported, and clang has not been tested.

Download

Don't forget to initialise and update the submodules! The cpython is +200MB so it may take some time to during git submodule update.

git clone https://github.com/matusnovak/python-embedded-example-project 
cd python-embedded-example-project 
git submodule init
git submodule update --progress

Build using Visual Studio on Windows

Please note: that you have to explicitly specify CMAKE_BUILD_TYPE. If you specify Debug, then you must use Debug in your Visual Studio. You won't be able to change to Release using the dropdown list in the main menu. Python will be build using the CMAKE_BUILD_TYPE flag regarding the chosen configuration in Visual Studio. To change from Debug to Release, re-run the cmake and set the CMAKE_BUILD_TYPE to Release.

cd python-embedded-example-project 
mkdir build
cd build
cmake .. -G "Visual Studio 15 2017" -DCMAKE_BUILD_TYPE=Debug

Then open the generated solution file "PythonEmbeddedExample.sln" in the build folder. You should see the following projects:

  • ALL_BUILD - Building this will build all projects
  • CPYTHON - The embedded python project
  • PYBIND - The pybind11 library for binding
  • PythonEmbeddedExample - This is the example project

Build the PythonEmbeddedExample and then run the PythonEmbeddedExample.exe from command line. That's all.

Build using GCC on Linux

cd python-embedded-example-project 
mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Debug

Then build the example by running:

make

The PythonEmbeddedExample executable will be generated.

Changing python version

At the time of the creation of this example, Python 3.8 was just released. The cpython submodule is frozen to commit 491bbedc209fea314a04cb3015da68fb0aa63238. If you want to change python version, change the branch in .gitmodules to branch = 2.7 or any other version. See available branches at: https://github.com/python/cpython

Example output

Example output of the PythonEmbeddedExample executable.

Python PATH set to: C:\Users\matus\Documents\cpp\python-embedded-example-project\build\Debug\lib;C:\Users\matus\Documents\cpp\python-embedded-example-project\build\Debug\app;
Importing module...Initializing class...
Example constructor with msg: Hello World
Got msg back on C++ side: Hello World

fatal error LNK1104: cannot open file 'python38_d.lib'

This happens when you run cmake with -DCMAKE_BUILD_TYPE=MinSizeRel and you are compiling the solution in Visual Studio as Debug. Simply, in Visual Studio, change the configuration to the one used in CMAKE_BUILD_TYPE.

This happens because the cpython has been built via CMAKE_BUILD_TYPE but your Visual Studio is looking for a debug version of the python library (or the other way around).

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].