This guide shows you how to use the Pythonic C++ library in your own projects.
- Prerequisites
- Step 1: Clone the Pythonic Library
- Step 2: Build and Install the Library
- Step 3: Create Your Project
- Step 4: Write Your Code
- Step 5: Build Your Project
- Step 6: Run Your Application
- Alternative: Using Without Installing
Before you begin, make sure you have:
- C++20 compiler (GCC 10+, Clang 10+, MSVC 2019+)
- CMake 3.10 or later
- Git
To use terminal image/video rendering with print(), install:
- ImageMagick - For image format conversion (PNG, JPG, etc.)
- FFmpeg - For video decoding and playback
sudo apt update && sudo apt install -y imagemagick ffmpegsudo dnf install imagemagick ffmpegsudo pacman -S imagemagick ffmpegbrew install imagemagick ffmpegDownload and install:
- ImageMagick - Add to PATH during installation
- FFmpeg - Add to PATH manually or use
choco install ffmpeg
convert --version # Should show ImageMagick version
ffmpeg -version # Should show FFmpeg versionTo enable synchronized audio playback with videos, you need SDL2 or PortAudio:
# SDL2 (recommended)
sudo apt-get install libsdl2-dev
# Or PortAudio
sudo apt-get install portaudio19-dev libportaudio2# SDL2 (recommended)
sudo dnf install SDL2-devel
# Or PortAudio
sudo dnf install portaudio-devel# SDL2 (recommended)
sudo pacman -S sdl2
# Or PortAudio
sudo pacman -S portaudio# SDL2 (recommended)
brew install sdl2
# Or PortAudio
brew install portaudio# SDL2 (recommended)
vcpkg install sdl2:x64-windows
# Or PortAudio
vcpkg install portaudio:x64-windows# With SDL2
cmake -B build -DPYTHONIC_ENABLE_SDL2_AUDIO=ON
cmake --build build
# Or with PortAudio
cmake -B build -DPYTHONIC_ENABLE_PORTAUDIO=ON
cmake --build build#include <pythonic/pythonic.hpp>
using namespace Pythonic;
// Play video with audio (Audio::on enables synchronized audio)
print("video.mp4", Type::video, Render::BW, Audio::on);
// Play video with audio in true color
print("video.mp4", Type::video, Render::colored, Audio::on);
// Default: video without audio
print("video.mp4", Type::video); // Audio::off is defaultNote: If audio libraries are not available,
Audio::onwill automatically fall back to silent video playback.
For smoother colored video playback (especially for high-resolution videos), you can enable OpenCL GPU acceleration:
sudo apt-get install ocl-icd-opencl-dev opencl-clhpp-headerssudo dnf install ocl-icd-devel opencl-headerssudo pacman -S ocl-icd opencl-headersOpenCL is included with macOS by default - no additional installation needed.
vcpkg install opencl:x64-windows# Enable OpenCL when configuring
cmake -B build -DPYTHONIC_ENABLE_OPENCL=ON
cmake --build build
# Or combine with other options
cmake -B build -DPYTHONIC_ENABLE_SDL2_AUDIO=ON -DPYTHONIC_ENABLE_OPENCL=ON
cmake --build build- When OpenCL is enabled and a GPU is detected, colored video rendering uses GPU acceleration
- The GPU processes frame data in parallel, reducing flickering and improving frame rates
- If no GPU is available, it automatically falls back to optimized CPU rendering
Note: OpenCL support is completely optional. Video playback works without it, just with potentially more flickering on high-resolution colored videos.
For games and interactive applications that require detecting multiple keys pressed simultaneously, the TerminalGraphics keyboard system provides enhanced input handling.
- Windows: Automatic via
GetAsyncKeyState()- no setup required - macOS/Linux (default): Uses a 200ms "sticky window" for simulated multi-key detection
- Linux (evdev): Optional direct hardware access for true simultaneous detection
For true multi-key detection on Linux, you need:
- Read access to
/dev/input/event*devices - Define
PYTHONIC_USE_EVDEVwhen compiling
# Add your user to the input group (log out and back in after)
sudo usermod -aG input $USER
# Compile with evdev support
g++ -std=c++20 -DPYTHONIC_USE_EVDEV -Iinclude -o game game.cpp -pthread# Enable evdev when configuring (Linux only, auto-detected)
cmake -B build -DENABLE_EVDEV=ON
cmake --build build#include <pythonic/TerminalGraphics.hpp>
using namespace Pythonic::TG;
int main() {
Keyboard::init();
while (true) {
// On Windows and Linux with evdev: true simultaneous detection
// On other systems: simulated via sticky window
if (Keyboard::isKeyPressed(Key::W) && Keyboard::isKeyPressed(Key::D)) {
// Move forward-right diagonally
}
}
Keyboard::shutdown();
}Note: The terminal mode fallback (without evdev) still provides good multi-key simulation using a 200ms sticky window - most games will work fine without evdev.
First, choose a location to install the Pythonic library (this can be anywhere on your system). For this guide, we'll use a folder called pythonic_cpp_lib:
mkdir -p ~/pythonic_cpp_lib
cd ~/pythonic_cpp_lib
git clone https://github.com/Creamy-pie-96/Pythonic.git
cd Pythonicmkdir %USERPROFILE%\pythonic_cpp_lib
cd %USERPROFILE%\pythonic_cpp_lib
git clone https://github.com/Creamy-pie-96/Pythonic.git
cd PythonicNew-Item -ItemType Directory -Force -Path ~\pythonic_cpp_lib
cd ~\pythonic_cpp_lib
git clone https://github.com/Creamy-pie-96/Pythonic.git
cd PythonicNote: You can use any directory name and location you prefer - just remember where you put it!
Now build and install the Pythonic library. The install will be placed in a subfolder called install within the Pythonic directory.
mkdir -p build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=../install -DPYTHONIC_ENABLE_GRAPH_VIEWER=ON
cmake --build . --target install -j4
cd ../.. # Go back to pythonic_cpp_lib directory
pwd # Remember this path - you'll need it later!
If you want to use the interactive Graph Viewer (`var::show()`), build
the library with graph viewer support enabled. This will compile the
GLFW/OpenGL/ImGui-backed viewer and expose the CMake target
`pythonic::pythonic_graph_viewer` for downstream projects.
Enable the viewer during configuration:
```bash
cmake .. -DCMAKE_INSTALL_PREFIX=../install -DPYTHONIC_ENABLE_GRAPH_VIEWER=ON
cmake --build . --target install -j4Required system dependencies (example on Debian/Ubuntu):
sudo apt update && sudo apt install -y build-essential cmake libglfw3-dev libx11-dev \
libxrandr-dev libxinerama-dev libxcursor-dev libxi-dev libglu1-mesa-dev libgl1-mesa-devImGui is included in the Pythonic repository under external/imgui.
If you vendor or replace it, ensure the viewer build picks up a compatible
ImGui + OpenGL loader.
macOS (Homebrew)
# Install dependencies via Homebrew
brew update
brew install cmake glfw
# OpenGL is provided by the system (via frameworks). Build as usual with CMake.Windows (Visual Studio + vcpkg)
Option A — vcpkg (recommended):
git clone https://github.com/microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.bat # or bootstrap-vcpkg.sh on WSL
# Install GLFW and other deps for x64
./vcpkg install glfw3:x64-windows
# Configure CMake to use vcpkg toolchain when building your project
cmake .. -DCMAKE_TOOLCHAIN_FILE=/path/to/vcpkg/scripts/buildsystems/vcpkg.cmake -DCMAKE_PREFIX_PATH=<path-to-pythonic-install>Option B — Visual Studio without vcpkg:
- Install the "Desktop development with C++" workload in Visual Studio.
- Download prebuilt GLFW binaries or build GLFW from source and point CMake
to the GLFW install location via
CMAKE_PREFIX_PATHor-DGLFW_ROOT.
On Windows, OpenGL is available via opengl32.lib (FindOpenGL handles it).
### Windows (Command Prompt):
```cmd
mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=..\install -DPYTHONIC_ENABLE_GRAPH_VIEWER=ON
cmake --build . --target install --config Release
cd ..\..
cd
New-Item -ItemType Directory -Force -Path build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX="..\install"
cmake --build . --target install --config Release
cd ..\..
Get-Location # Remember this path - you'll need it later!After this step:
- The library is installed at:
<your-pythonic_cpp_lib-path>/Pythonic/install/ - Save or remember this full path - you'll use it when building your own projects
Now create your own project in a separate location (anywhere you want - doesn't need to be near the Pythonic library):
mkdir -p ~/myproject
cd ~/myprojectmkdir %USERPROFILE%\myproject
cd %USERPROFILE%\myprojectNew-Item -ItemType Directory -Force -Path ~\myproject
cd ~\myprojectYour directory structure now looks like:
(anywhere on your system)
└── pythonic_cpp_lib/
└── Pythonic/
└── install/ ← Pythonic is installed here
(can be anywhere else on your system)
└── myproject/ ← Your project (we are here now)
Create two files in your project directory:
#include <pythonic/pythonic.hpp>
using namespace pythonic::vars;
using namespace pythonic::print;
int main() {
var x = 42;
var name = "Pythonic C++";
print("Hello from", name);
print("The answer is:", x);
// Create a list
var nums = list(1, 2, 3, 4, 5);
print("Numbers:", nums);
return 0;
}Or you can also copy the demo.cpp from example dir
cmake_minimum_required(VERSION 3.10)
project(MyApp)
# Require C++20
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# If you plan to link the optional Graph Viewer (to enable `var::show()`),
# CMake needs to find OpenGL before importing the installed `pythonic`
# package because the exported viewer target references `OpenGL::GL`.
find_package(OpenGL REQUIRED)
# Find the Pythonic library
find_package(pythonic REQUIRED)
# Find ImageMagick and FFmpeg for media rendering support (images and videos)
find_package(ImageMagick COMPONENTS Magick++ REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(FFMPEG REQUIRED libavcodec libavformat libswscale libavutil)
# Optional: SDL2 for audio playback
pkg_check_modules(SDL2 QUIET sdl2)
if(SDL2_FOUND)
message(STATUS "SDL2 found - audio playback enabled")
endif()
# Optional: OpenCL for GPU-accelerated video rendering
find_package(OpenCL QUIET)
if(OpenCL_FOUND)
message(STATUS "OpenCL found - GPU acceleration enabled")
endif()
# Add your executable (replace main.cpp with your source files)
add_executable(myapp main.cpp)
# Link the Pythonic library and media dependencies
target_link_libraries(myapp PRIVATE pythonic::pythonic ImageMagick::Magick++ ${FFMPEG_LIBRARIES})
# Add FFmpeg include directories
target_include_directories(myapp PRIVATE ${FFMPEG_INCLUDE_DIRS})
# Link optional SDL2 for audio
if(SDL2_FOUND)
target_compile_definitions(myapp PRIVATE PYTHONIC_ENABLE_SDL2_AUDIO)
target_include_directories(myapp PRIVATE ${SDL2_INCLUDE_DIRS})
target_link_libraries(myapp PRIVATE ${SDL2_LIBRARIES})
endif()
# Link optional OpenCL for GPU acceleration
if(OpenCL_FOUND)
target_compile_definitions(myapp PRIVATE PYTHONIC_ENABLE_OPENCL)
target_include_directories(myapp PRIVATE ${OpenCL_INCLUDE_DIRS})
target_link_libraries(myapp PRIVATE ${OpenCL_LIBRARIES})
endif()
# If you built and installed Pythonic with the Graph Viewer enabled,
# link the viewer target to enable `var::show()` in your app:
target_link_libraries(myapp PRIVATE pythonic::pythonic_graph_viewer)Copy paste it or we have a CMakeLists.txt file in example directory. You can directly use that too.
Now configure and build your project. You need to tell CMake where you installed the Pythonic library.
IMPORTANT - You Must Specify the Path:
Replace <path-to-pythonic-install> below with the actual full path to where you installed Pythonic in Step 2.
For example:
- Linux/macOS:
/home/username/pythonic_cpp_lib/Pythonic/install - Windows:
C:\Users\username\pythonic_cpp_lib\Pythonic\install
mkdir build
cd build
# Replace <path-to-pythonic-install> with your actual path!
# Example: cmake .. -DCMAKE_PREFIX_PATH=/home/john/pythonic_cpp_lib/Pythonic/install
cmake .. -DCMAKE_PREFIX_PATH=<path-to-pythonic-install>
cmake --build . -j4Example with actual path:
cmake .. -DCMAKE_PREFIX_PATH=$HOME/pythonic_cpp_lib/Pythonic/installmkdir build
cd build
REM Replace <path-to-pythonic-install> with your actual path!
REM Example: cmake .. -DCMAKE_PREFIX_PATH=C:\Users\John\pythonic_cpp_lib\Pythonic\install
cmake .. -DCMAKE_PREFIX_PATH=<path-to-pythonic-install>
cmake --build . --config ReleaseExample with actual path:
cmake .. -DCMAKE_PREFIX_PATH=%USERPROFILE%\pythonic_cpp_lib\Pythonic\installNew-Item -ItemType Directory -Force -Path build
cd build
# Replace <path-to-pythonic-install> with your actual path!
# Example: cmake .. -DCMAKE_PREFIX_PATH="C:\Users\John\pythonic_cpp_lib\Pythonic\install"
cmake .. -DCMAKE_PREFIX_PATH="<path-to-pythonic-install>"
cmake --build . --config ReleaseExample with actual path:
cmake .. -DCMAKE_PREFIX_PATH="$HOME\pythonic_cpp_lib\Pythonic\install"Finding Your Path:
If you forgot where you installed Pythonic, you can find it:
Linux/macOS:
find ~ -name "pythonicConfig.cmake" 2>/dev/null
# This will show the full path - use everything before "/lib/cmake/pythonic/"Windows (PowerShell):
Get-ChildItem -Path $HOME -Filter "pythonicConfig.cmake" -Recurse -ErrorAction SilentlyContinue
# Look for the path, use everything before "\lib\cmake\pythonic\"If you built and installed Pythonic with -DPYTHONIC_ENABLE_GRAPH_VIEWER=ON,
your installed package exports the target pythonic::pythonic_graph_viewer.
Link that target in your project's CMakeLists.txt to enable the
interactive viewer API. Example CMake configuration:
find_package(pythonic REQUIRED)
add_executable(myapp main.cpp)
target_link_libraries(myapp PRIVATE pythonic::pythonic pythonic::pythonic_graph_viewer)Minimal example that uses the viewer:
#include <pythonic/pythonic.hpp>
using namespace pythonic::vars;
int main() {
var g = graph(3);
g.add_edge(0, 1, 1.0, 0.0, true);
g.add_edge(1, 2, 1.0, 0.0, true);
// Show opens an interactive window (may block until closed)
g.show();
return 0;
}Runtime notes:
- The viewer opens a desktop window: ensure a graphical session (DISPLAY) is available on Linux. Headless servers without an X/Wayland display cannot show the GUI unless you use a virtual framebuffer (Xvfb).
- If you see missing link symbols when building your application, confirm system GLFW/OpenGL development packages were available when Pythonic was built and that Pythonic was installed with the viewer enabled.
Finally, run your compiled program:
./myappRelease\myapp.exe.\Release\myapp.exeYou should see output like:
Hello from Pythonic C++
The answer is: 42
Numbers: [1, 2, 3, 4, 5]
If you don't want to install the library, you can include the headers directly:
cmake_minimum_required(VERSION 3.10)
project(MyApp)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Find optional dependencies
find_package(PkgConfig QUIET)
if(PKG_CONFIG_FOUND)
pkg_check_modules(SDL2 QUIET sdl2)
endif()
find_package(OpenCL QUIET)
add_executable(myapp main.cpp)
# Point directly to the Pythonic include directory
target_include_directories(myapp PRIVATE
${CMAKE_SOURCE_DIR}/../Pythonic/include
)
target_compile_features(myapp PRIVATE cxx_std_20)
# Optional: SDL2 for audio playback
if(SDL2_FOUND)
target_compile_definitions(myapp PRIVATE PYTHONIC_ENABLE_SDL2_AUDIO)
target_include_directories(myapp PRIVATE ${SDL2_INCLUDE_DIRS})
target_link_libraries(myapp PRIVATE ${SDL2_LIBRARIES})
message(STATUS "SDL2 audio enabled")
endif()
# Optional: OpenCL for GPU acceleration
if(OpenCL_FOUND)
target_compile_definitions(myapp PRIVATE PYTHONIC_ENABLE_OPENCL)
target_include_directories(myapp PRIVATE ${OpenCL_INCLUDE_DIRS})
target_link_libraries(myapp PRIVATE ${OpenCL_LIBRARIES})
message(STATUS "OpenCL GPU acceleration enabled")
endif()Then build without CMAKE_PREFIX_PATH:
mkdir build && cd build
cmake ..
cmake --build . -j4
./myappmkdir build && cd build
cmake ..
cmake --build . --config Release
Release\myapp.exeThis is the most common error. It means CMake cannot find the installed package config files.
Solution:
-
Find where you installed Pythonic:
Linux/macOS:
find ~ -name "pythonicConfig.cmake" 2>/dev/null
This will show something like:
/home/user/pythonic_cpp_lib/Pythonic/install/lib/cmake/pythonic/pythonicConfig.cmakeThe path you need is everything before
/lib/cmake/pythonic/:
→/home/user/pythonic_cpp_lib/Pythonic/installWindows (PowerShell):
Get-ChildItem -Path $HOME -Filter "pythonicConfig.cmake" -Recurse -ErrorAction SilentlyContinue
This will show something like:
C:\Users\John\pythonic_cpp_lib\Pythonic\install\lib\cmake\pythonic\pythonicConfig.cmakeThe path you need is everything before
\lib\cmake\pythonic\:
→C:\Users\John\pythonic_cpp_lib\Pythonic\install -
Use that full path in your cmake command:
cmake .. -DCMAKE_PREFIX_PATH=/home/user/pythonic_cpp_lib/Pythonic/install
note you can add -DCMAKE_BUILD_TYPE=Release if you want to build in release mode
-
Verify the path is correct before running cmake:
Linux/macOS:
ls /path/to/pythonic_cpp_lib/Pythonic/install/lib/cmake/pythonic/ # Should show: pythonicConfig.cmake pythonicConfigVersion.cmake pythonicTargets.cmakeWindows:
dir C:\path\to\pythonic_cpp_lib\Pythonic\install\lib\cmake\pythonic\
- Check that you're using
pythonic::pythonic(with::) intarget_link_libraries - Verify headers were installed to
install/include/pythonic/
- Ensure your compiler supports C++20
- On older systems, you may need to update GCC/Clang
The Graph class includes a to_dot() method that exports the graph structure to a DOT file (for visualization with Graphviz). By default, it only creates the .dot file.
If you have Graphviz installed on your system, you can enable automatic SVG generation when to_dot() is called.
The CMakeLists.txt provided in this examples folder automatically detects Graphviz. When you run cmake, you'll see one of these messages:
-- Graphviz found: /usr/bin/dot
-- Graph::to_dot() will automatically generate SVG files
or:
-- Graphviz not found (optional)
-- Graph::to_dot() will only create .dot files
-- Install Graphviz to enable automatic SVG generation
To disable the auto-detection, configure with:
cmake .. -DCMAKE_PREFIX_PATH=<path> -DENABLE_GRAPHVIZ=OFFIf you're creating your own CMakeLists.txt from scratch, add this line after add_executable():
target_compile_definitions(myapp PRIVATE GRAPHVIZ_AVAILABLE)Or define it in your code before including the library:
#define GRAPHVIZ_AVAILABLE
#include <pythonic/pythonic.hpp>#include <pythonic/pythonic.hpp>
using namespace pythonic::vars;
int main() {
var g = graph(4);
g.add_edge(0, 1, 1.0);
g.add_edge(1, 2, 2.0);
g.add_edge(2, 3, 3.0);
g.add_edge(3, 0, 4.0);
// If GRAPHVIZ_AVAILABLE: Creates my_graph.dot AND my_graph.svg
// Otherwise: Creates only my_graph.dot
g.to_dot("my_graph.dot");
return 0;
}Without Graphviz installed, you can still manually convert DOT files:
# Install Graphviz first (one-time):
# Ubuntu/Debian: sudo apt install graphviz
# macOS: brew install graphviz
# Windows: Download from https://graphviz.org/download/
# Then convert manually:
dot -Tsvg my_graph.dot -o my_graph.svg
dot -Tpng my_graph.dot -o my_graph.pngCheck out the full documentation in the Documentation for all the features:
- Dynamic typing with
var - Python-style containers (lists, dicts, sets)
- Slicing, comprehensions, ranges
- Graph algorithms
- Math library
- And much more!
Enjoy coding with Pythonic C++!