Skip to main content

Python CFFI bindings for Raylib

Project description

Python Bindings for Raylib 6.0

Libraries: raymath, raygui, rlgl, physac and GLFW

Backends: Desktop, SDL, DRM, Web, Software rendering

Platforms: Windows, Mac, Linux, Raspberry Pi, Web

PyPI - Downloads

PyPI Downloads

HELP WANTED: writing examples

Features:

  • CFFI API static bindings.
  • Automatically generated to be as close as possible to original Raylib.
  • Faster, fewer bugs and easier to maintain than ctypes.
  • Commercial-friendly license.
  • Docstrings and auto-completion.
  • Type checking with Mypy

Quickstart

pip3 install raylib==6.0.1.0 --break-system-packages

from pyray import *
init_window(800, 450, "Hello")
while not window_should_close():
    begin_drawing()
    clear_background(WHITE)
    draw_text("Hello world", 190, 200, 20, VIOLET)
    end_drawing()
close_window()

Use the project generator to generate a complete project. Example of project

Videos

FinFET - Making a simple 3D game in Python (for real) with Raylib
video

more on FinFET

Clear Code - The ultimate introduction to Raylib
video

Unconventional Coding - I Made My Own Video Editor Because Kdenlive Is Too Slow
video

WOBLO GAMES - The Druid's Downfall
video

Links

Installation

If you are on a modern Linux you will probably want to create a venv:

python3 -m venv venv
source venv/bin/activate

Then make sure you have the latest pip installed:

python3 -m pip install --upgrade pip

Then install

python3 -m pip install setuptools
python3 -m pip install raylib==6.0.1.0

On most platforms it should install a binary wheel. If yours isn't available then pip will attempt to build from source, in which case you will need to have Raylib development libs installed, e.g. using homebrew, apt, etc.

Windows

Binaries require x64 or x86 Windows 10 or newer.

Use an official Windows Python release rather than WSL, MSYS, etc.

MacOS

Binaries require:

  • arm64 MacOS 14
  • x64 MacOS 10.13, or newer.

Older MacOS requires building from source but this is usually simple:

brew install pkg-config
brew install raylib
python3 -m pip install raylib==6.0.1.0

Linux

Binaries require OS newer than Ubuntu 2016, x64/x86 or Ubuntu 2022 arm64. Otherwise build from source. (Pip should attempt automatically but will need Raylib itself installed and also pkg-config.)

The arm64 binaries are built on Raspberry Pi arm64 Bullseye with OpenGL 2.0 so may not work on other boards.

Raspberry Pi

Using on Rasperry Pi

Backends

Dynamic binding version

There is now a separate dynamic version of this binding:

python3 -m pip uninstall raylib
python3 -m pip install raylib_dynamic

It works on some systems where the static version doesn't, but be sure to read these caveats before using it

You can't have multiple raylib packages installed at once.

SDL backend

This is not well tested but has better support for controllers:

python3 -m pip uninstall raylib
python3 -m pip install raylib_sdl

You can't have multiple raylib packages installed at once.

DRM backend

This uses the Linux framebuffer for devices that don't run X11/Wayland:

python3 -m pip uninstall raylib
python3 -m pip install raylib_drm

You can't have multiple raylib packages installed at once.

Problems?

If it doesn't work, try to build manually.. If that works then submit an issue to let us know what you did.

If you need help you can try asking on our discord. There is also a large Raylib discord for issues that are not Python-specific.

If it still doesn't work, submit an issue.

How to use

There are two modules in the raylib package, raylib and pyray. (There is no separate package for pyray. Do not pip install pyray). You can use either or both:

If you are familiar with C coding and the Raylib C library and you want to use an exact copy of the C API

Use the raylib module.

If you prefer a more Pythonistic API

Use the pyray module.

Running in a web browser

Pygbag >=0.8.7 supports running in a web browser. Usually the latest git version is recommended.

Make a folder my_project with a file main.py:

# /// script
# dependencies = [
#     "cffi",
#     "raylib"
# ]
# ///
import asyncio
import platform
from pyray import *

async def main():   # You MUST have an async main function
    init_window(500, 500, "Hello")
    platform.window.window_resize()  # You MAY want to add this line
    while not window_should_close():
        begin_drawing()
        clear_background(WHITE)
        draw_text("Hello world", 190, 200, 20, VIOLET)
        end_drawing()
        await asyncio.sleep(0) # You MUST call this in your main loop
    close_window()

asyncio.run(main())

Then to create the web files and launch a web server:

python3.12 -m pip install --user --upgrade pygbag
python3.12 -m pygbag --PYBUILD 3.12 --ume_block 0 --template noctx.tmpl --git my_project

Point your browser to http://localhost:8000

Some features may not work, so you can disable them like this:

if platform.system() != "Emscripten":  # audio may not work on current version of emscripten
    init_audio_device()

This is all done by Pygbag rather than by me, so you should probably contact them with any issues. Carefully read all their documentation.

It does work for most of these examples

App showcase

Tempest-raylib

KarabinerKeyboard

PyTaiko

DOOM-Clone

Tanki

Alloy Bloxel Editor

Eidolon

Add your app here!

RLZero

A related library (that is a work in progress!):

A simplified API for Raylib for use in education and to enable beginners to create 3d games

Help wanted

  • Converting more examples from C to Python
  • Testing on more platforms

License

Eclipse Public License, so you are free to statically link and use in non-free / proprietary / commercial projects!

Performance

If you need more performance, do in this order:

  1. Use Pypy rather than standard CPython. It is much, much faster and will make more difference than any other optimisations you might do.

  2. Every call to C is costly, so it's slightly faster if you use Python data structures and functions when calculating in your update loop and then only convert them to C data structures when you have to call the C functions for drawing.

  3. The raylib.* functions are potentially slightly faster than the pyray.* equivalents, so if you need a tiny bit more performance you can switch your inner loop functions to these.

  4. There is a version of Python that is faster than Pypy: GraalPy. However it's not fully compatible with all Python packages. It doesn't work with CFFI and so doesn't work with this binding. But it is compatible with the Java binding, Jaylib! There is an example of this here: https://github.com/electronstudio/megabunny/tree/master/raylib-python-jaylib

Bunnymark

Library Implementation Bunnies (60 FPS) Percentage
Raylib 5.0 C 180000 100%
Raylib Python CFFI 5.0.0.2 Python 3.12 10500 5.8%
Raylib Python CFFI 5.0.0.2 Pypy 3.10 95000 53%
Raylib 3.7 C 168100 100%
Raylib Python CFFI 3.7 Pypy 3.7 33800 20%
Raylib Python CFFI 3.7 Python 3.9 7700 4.5%
Raylib Python CFFI 3.7 Python 3.9 Nuitka 8600 5.1%
Raylib Python CFFI 3.7 Dynamic Python 3.9 6300 3.7%

See also https://github.com/electronstudio/megabunny/

Packaging your app

You can create a standalone binary using the Nuitka compiler. For example, here is how to package Bunnymark:

pip3 install nuitka
cd examples/textures
python3 -m nuitka --onefile --linux-onefile-icon resources/wabbit_alpha.png textures_bunnymark.py

Advert

RetroWar: 8-bit Party Battle is out now. Defeat up to 15 of your friends in a tournament of 80s-inspired retro mini games.

Coding Games With Pygame Zero & Python is a book for Python beginners.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

raylib_drm-6.0.1.0rc6-pp311-pypy311_pp73-manylinux_2_24_x86_64.whl (2.7 MB view details)

Uploaded PyPymanylinux: glibc 2.24+ x86-64

raylib_drm-6.0.1.0rc6-cp314-cp314-manylinux_2_24_x86_64.whl (3.4 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.24+ x86-64

raylib_drm-6.0.1.0rc6-cp314-cp314-manylinux_2_24_i686.whl (3.3 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.24+ i686

raylib_drm-6.0.1.0rc6-cp313-cp313-manylinux_2_24_x86_64.whl (3.4 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.24+ x86-64

raylib_drm-6.0.1.0rc6-cp313-cp313-manylinux_2_24_i686.whl (3.3 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.24+ i686

raylib_drm-6.0.1.0rc6-cp312-cp312-manylinux_2_24_x86_64.whl (3.4 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.24+ x86-64

raylib_drm-6.0.1.0rc6-cp312-cp312-manylinux_2_24_i686.whl (3.3 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.24+ i686

raylib_drm-6.0.1.0rc6-cp311-cp311-manylinux_2_24_x86_64.whl (3.4 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.24+ x86-64

raylib_drm-6.0.1.0rc6-cp311-cp311-manylinux_2_24_i686.whl (3.3 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.24+ i686

raylib_drm-6.0.1.0rc6-cp310-cp310-manylinux_2_24_x86_64.whl (3.4 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.24+ x86-64

raylib_drm-6.0.1.0rc6-cp310-cp310-manylinux_2_24_i686.whl (3.3 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.24+ i686

raylib_drm-6.0.1.0rc6-cp39-cp39-manylinux_2_24_x86_64.whl (3.4 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.24+ x86-64

raylib_drm-6.0.1.0rc6-cp39-cp39-manylinux_2_24_i686.whl (3.3 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.24+ i686

raylib_drm-6.0.1.0rc6-cp38-cp38-manylinux_2_24_x86_64.whl (3.4 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.24+ x86-64

raylib_drm-6.0.1.0rc6-cp38-cp38-manylinux_2_24_i686.whl (3.3 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.24+ i686

raylib_drm-6.0.1.0rc6-cp37-cp37m-manylinux_2_24_x86_64.whl (3.4 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.24+ x86-64

raylib_drm-6.0.1.0rc6-cp37-cp37m-manylinux_2_24_i686.whl (3.3 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.24+ i686

File details

Details for the file raylib_drm-6.0.1.0rc6-pp311-pypy311_pp73-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.1.0rc6-pp311-pypy311_pp73-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 28ad6eff7c21e4fc550c77411acd7c45c14b7ff69ca628d2cf512869220eaaa1
MD5 b2609c1e7d7ba47799b1e7999dee65ba
BLAKE2b-256 45a8651befed7e9cdccc9f186b9d6f93588dfdd20a0b97768e6f25bfcea81df5

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.1.0rc6-cp314-cp314-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.1.0rc6-cp314-cp314-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 48af6fb537cfbc33f4f18e740156ebde364b94a175fd78854560335529c9982d
MD5 42b6f9a9c00eff435b7c746eaa86d1b1
BLAKE2b-256 93d0f0ead158606e95e8822fc61c8bf8af971d91fa5dae201640b85bd2a137f3

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.1.0rc6-cp314-cp314-manylinux_2_24_i686.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.1.0rc6-cp314-cp314-manylinux_2_24_i686.whl
Algorithm Hash digest
SHA256 f351b6bb60b4d7b2b7971d4d780d6d957c901ca4bc5efb74324781da30c17fee
MD5 29b29be2976c6aa367cc7b31b45f87dd
BLAKE2b-256 47ef2fcdff69d64794ceabd72410c926ebf0095e691f84e6d7fbdecab5aab333

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.1.0rc6-cp314-cp314-manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.1.0rc6-cp314-cp314-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 2b92c9db773ec70a66c1e69755fded2f89238bc98ba4e4d92e6ee6f993e446cb
MD5 b99ab7d20d0cf2ff9d54ce8aee69aabc
BLAKE2b-256 d07d11a450f5d5ef73b36f03bc820da538f89616ea9909910945c9c357d4eb8c

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.1.0rc6-cp313-cp313-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.1.0rc6-cp313-cp313-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 8b1179d81795d2266f2eaf0ee652915de6ec6471ef9e15c366355e9b7ef2c133
MD5 280dfdab3f89a5c0b53cbd9f5f3c9ce6
BLAKE2b-256 8fc6e833df897938609eb402366c5e39cb384d51615db7b0d0164e6f4cff6f54

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.1.0rc6-cp313-cp313-manylinux_2_24_i686.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.1.0rc6-cp313-cp313-manylinux_2_24_i686.whl
Algorithm Hash digest
SHA256 228beeea112ff4128aab5579760b3ea484603f46d5bca8305262057b2d62d413
MD5 1e8209a66281c5059d7d2ec63fa33f42
BLAKE2b-256 b444ade5fb3c74080309c0a8ad8f2a83735fc62d131dcd2751dda6721b5f589b

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.1.0rc6-cp313-cp313-manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.1.0rc6-cp313-cp313-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 08a247bf6d49b80e27e1358625c9a1d113c930a914ca59c282ccb894dc61ebfa
MD5 97c1b0adb1009e021cc38b5cf939c5a0
BLAKE2b-256 8839cc1d3d5913584f2addd0dd02c443c61fb36be4be491c41444624db24df79

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.1.0rc6-cp312-cp312-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.1.0rc6-cp312-cp312-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 b66a04865a56492ae5dab28e74f33cf58e6c4429675c675b3d58f7ad41e2593f
MD5 0408f1602fb75a4da64770dbdb42c0cc
BLAKE2b-256 d3caa18a2610a6fe7f3a60192e9e1d2e8c4d89580a17b6976c00889c615fd0e8

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.1.0rc6-cp312-cp312-manylinux_2_24_i686.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.1.0rc6-cp312-cp312-manylinux_2_24_i686.whl
Algorithm Hash digest
SHA256 9aaa95ead81c1ce06c254ebe79d6cfe03a987112db0c21c237dac37c0c06f764
MD5 1a668131e64f8abf97b859b3cba4adcf
BLAKE2b-256 32830eec6bb66aa2778b5c61315bd52e6b6164180c6937041c27bde28277dd39

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.1.0rc6-cp312-cp312-manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.1.0rc6-cp312-cp312-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 0ef9c23825eb9c32d03c36c4edd8f7f46e900fb825ed33bd4e4614144fe86e51
MD5 645623fd8209f94fe4d04b896d78d6c5
BLAKE2b-256 514470db21b94686d63ba66c4e927d9fdc88eef0128376b8372bfcff5c643891

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.1.0rc6-cp311-cp311-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.1.0rc6-cp311-cp311-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 b9496d603ab2b8e141850b5ebf9ecda1e083c543fa757469dbb5da2dd7520df8
MD5 bc566cdbb36d3ae1a9f2e980fe50c998
BLAKE2b-256 51375b5e3bf3ba9c230c041146e87b9d7c1742066830e76b58ef25a39ef47d79

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.1.0rc6-cp311-cp311-manylinux_2_24_i686.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.1.0rc6-cp311-cp311-manylinux_2_24_i686.whl
Algorithm Hash digest
SHA256 a6923a2887c025ffdcf0284dbf3f83ecbb63f77b14cebcde9d97514aaa19d0dc
MD5 edb6f04157555b5c99324642fc51525a
BLAKE2b-256 f1b7b984ec0993ff262d7e93ba2098dd9e72cc8d1021803427bb216c6dc888c6

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.1.0rc6-cp311-cp311-manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.1.0rc6-cp311-cp311-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 e225b2372585a21f6eec986a4aafc8788ed7e4e980fd370c6a60c71ec45c5fcd
MD5 b4aaa08687a6eea9ca7418657acc7b0c
BLAKE2b-256 665c3fdfcfee076a7888e21e6f625ec6f199e01be2024b77f4a22d01771a59cd

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.1.0rc6-cp310-cp310-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.1.0rc6-cp310-cp310-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 1053f474a3fc790b286bbd28a5611a8c094843a2721ecf4aa99c0a5453f4f3b3
MD5 a9f68cda6a8a6006618f2eb1eda9e626
BLAKE2b-256 6df176028c4b6ff767a6ada4ae5a2db941424dd8d620c2ecd5f23b9fe5450a3d

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.1.0rc6-cp310-cp310-manylinux_2_24_i686.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.1.0rc6-cp310-cp310-manylinux_2_24_i686.whl
Algorithm Hash digest
SHA256 8630beec2ea5ddaca3620b189e6e48de9a3e3190a3c27ebf6ec4317d0b006cce
MD5 b0b7ee64dc1e25f7d1c8a096de92cf8e
BLAKE2b-256 da6faff52c039e7046d289a302e5989f3484f5f74d7e57a7e0016f08c502c641

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.1.0rc6-cp310-cp310-manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.1.0rc6-cp310-cp310-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 1de1afdeabd0a46c3dbf71ccf218a5511089f7cd8ba3b50187d262fb888cb605
MD5 d09e54d4f314b1171b78b7c17b625061
BLAKE2b-256 6a55e498923a22fe5d2d9aaf1b051b344a87ff4a1b3bc44373f5f3bf9f095c58

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.1.0rc6-cp39-cp39-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.1.0rc6-cp39-cp39-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 5261d7370e027445a1a054161adf1e501429daf59c86a0f499fb8a89e9bfd40f
MD5 693109b504f46a899121995aab5edb4e
BLAKE2b-256 0957f3a11cc95d44bcd7e43619fd377e23bf45671d401e8064d1ddc8b8ac4b5b

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.1.0rc6-cp39-cp39-manylinux_2_24_i686.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.1.0rc6-cp39-cp39-manylinux_2_24_i686.whl
Algorithm Hash digest
SHA256 129cf2fe5229ee650dafa7b3f81211a1b0c8072e60d25b1abaf8e62c4d7cb093
MD5 6614ea064c7d3aa93b2aff43ebd464a3
BLAKE2b-256 9f4085bfe4a59cee0711d722b9c48c7a31d40c93156372f16fe1d05b0aca7557

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.1.0rc6-cp38-cp38-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.1.0rc6-cp38-cp38-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 2effc8030f395b2b636590713f622f60a98ac4d06e3097a8f9166c60a89c9070
MD5 22c714f7f53dbaf99d0ceba70e5c4449
BLAKE2b-256 46b9c6c283465b50b7116a5a38e945ead40f0786e9eae6d1966e0e2f05eed4e7

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.1.0rc6-cp38-cp38-manylinux_2_24_i686.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.1.0rc6-cp38-cp38-manylinux_2_24_i686.whl
Algorithm Hash digest
SHA256 df39f80cace9d7d6ce6f164fe964cfc11900e71d40c119c447cd1a572dab0eb4
MD5 1c0b0713f578ce67cd3fe780b4b89ce4
BLAKE2b-256 34c99643ca7a15a7aab485f38e89156639c900a85c7347891655d2d0155e1ed1

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.1.0rc6-cp37-cp37m-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.1.0rc6-cp37-cp37m-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 4ae160db4cd054a121b6663a6be0819501fd1537b399e555b391f8f4bde3577d
MD5 8d4d877c5164e5e52b70dee228071c37
BLAKE2b-256 a6ac46b1c4138656a90481125f28d9dc3ce565be04c9b9db8551930a2c1c7301

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.1.0rc6-cp37-cp37m-manylinux_2_24_i686.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.1.0rc6-cp37-cp37m-manylinux_2_24_i686.whl
Algorithm Hash digest
SHA256 e13595c85a4f323267c49767e36f3278108042c0a156b411d9b7dc89a44bd7f2
MD5 e186746b905ba6b638ac79b14ab635de
BLAKE2b-256 6c5f71b841825609887b3c8502d5f33ef20a10349b896d4c1da28c54a6de8514

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page