Skip to main content

Python CFFI bindings for Raylib

Project description

Python Bindings for Raylib 5.5

Libraries: raymath, raygui, rlgl, physac and GLFW

Backends: Desktop, SDL, DRM, Web

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

PyPI - Downloads

Chatroom: Discord

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

Full documentation

Quickstart

pip3 install raylib==5.5.0.0

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()

Installation

First 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==5.5.0.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 Windows 10 or newer. (For x86 or older Windows you will have to build from source.)

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==5.5.0.0

(I do have binaries for arm64 MacOS 11, 12 and 13 but I have no way of testing they work, so post an issue if you want to test them.)

Linux

Binaries require OS newer than Ubuntu 2020, x64 or 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 does 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-5.5.0.2-cp313-cp313-manylinux2014_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.13

raylib_drm-5.5.0.2-cp312-cp312-manylinux2014_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.12

raylib_drm-5.5.0.2-cp312-cp312-manylinux2014_aarch64.whl (2.0 MB view details)

Uploaded CPython 3.12

raylib_drm-5.5.0.2-cp311-cp311-manylinux2014_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.11

raylib_drm-5.5.0.2-cp311-cp311-manylinux2014_aarch64.whl (1.9 MB view details)

Uploaded CPython 3.11

raylib_drm-5.5.0.2-cp310-cp310-manylinux2014_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.10

raylib_drm-5.5.0.2-cp310-cp310-manylinux2014_aarch64.whl (1.9 MB view details)

Uploaded CPython 3.10

raylib_drm-5.5.0.2-cp39-cp39-manylinux2014_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.9

raylib_drm-5.5.0.2-cp39-cp39-manylinux2014_aarch64.whl (1.9 MB view details)

Uploaded CPython 3.9

File details

Details for the file raylib_drm-5.5.0.2-pp311-pypy311_pp73-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.5.0.2-pp311-pypy311_pp73-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ab40c5adfb35838b6df6f66f1b25c4d7d6e55f8a312e6630ef2061ae60f6e479
MD5 018f5d443ef3a7da877b7539e173f2c5
BLAKE2b-256 ba57a67f43e73300eb688762172b512e6c39ff0d27cfc8441c2d8887e6961155

See more details on using hashes here.

File details

Details for the file raylib_drm-5.5.0.2-pp310-pypy310_pp73-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.5.0.2-pp310-pypy310_pp73-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a310698c160be20e33e3f2455e7f3273f3e824ffeca3c0fcb643dec7481af59c
MD5 8fdf3e8c65b5c742a81512b52e5f65d2
BLAKE2b-256 dc1ec59b9c89c903b25aea3dfc8da0cd5376419b43c80bfb1480bf499aa047c3

See more details on using hashes here.

File details

Details for the file raylib_drm-5.5.0.2-pp39-pypy39_pp73-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.5.0.2-pp39-pypy39_pp73-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2e345842c0842f4d8349ddf15987201153e1c084442f22bd9ee45c8ea79f0c44
MD5 b88eb84db1b2153d89a3c85568aa9c2b
BLAKE2b-256 2665f8c7a525ebe4fc496d97876ce0cfc322a97ef2b58f4e05f87826660ab8ee

See more details on using hashes here.

File details

Details for the file raylib_drm-5.5.0.2-cp313-cp313-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.5.0.2-cp313-cp313-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 88e0b225fb72f066793d01a5a160aecf0a9a19388f3868d15a0c07a676ff32ac
MD5 06f0cd7879bbe205491cbb7d12377502
BLAKE2b-256 e44a055a8317b05b60e8954e36ac371fedf0eb571c103d7bfa1036c70d7f7e8d

See more details on using hashes here.

File details

Details for the file raylib_drm-5.5.0.2-cp312-cp312-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.5.0.2-cp312-cp312-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6c40dbb2379f4c4f471790bed785a54386447ed5e98612e6656d1682fac0c93b
MD5 02ccbeb7043558100cbc8ac83e434568
BLAKE2b-256 18fe70b7fe3d91c4f95c61ff5e3f9c1c3b5995b35e5ccb56aa10b130d623824f

See more details on using hashes here.

File details

Details for the file raylib_drm-5.5.0.2-cp312-cp312-manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.5.0.2-cp312-cp312-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 6f9791ea450912b0dc06aa8e03e210a2ed6e13bc826ca7e50e7e9bad18c328cd
MD5 d053bfe0ddf55aaec2b396553172a339
BLAKE2b-256 6a8fcc697a739a2c8b501bc8b215de2c0873fb8e024f8c7a4572b1a83fc64688

See more details on using hashes here.

File details

Details for the file raylib_drm-5.5.0.2-cp311-cp311-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.5.0.2-cp311-cp311-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cc92b754c7cf2205f3ea5011315ebabd69918276e03ff2bdd9892095418c32d0
MD5 9910c41c6d163b8fa6862f070b9e49d6
BLAKE2b-256 988f447c7388928a7062ad8eaa481836ce476b3048aa6a01329477035a0566b7

See more details on using hashes here.

File details

Details for the file raylib_drm-5.5.0.2-cp311-cp311-manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.5.0.2-cp311-cp311-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 886f3e3926a5ac78587d1e4b3293c67bb43982f2eb88fd1f3a4b3310cd29aa98
MD5 6e39ccb2da3ad9f5709ee2401d9d7413
BLAKE2b-256 f841c75dd398bc4a70e73fc758f580da1e976ac4ea516237686191cdd3ea3ee7

See more details on using hashes here.

File details

Details for the file raylib_drm-5.5.0.2-cp310-cp310-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.5.0.2-cp310-cp310-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1aac691cb2e5675aae0a7f29c57706f421a0b7dc3d4a8b133136ec2d1461d99b
MD5 3abefe885abafe45380405a412619b5e
BLAKE2b-256 b1a98db6635709eecc6fae5c7f2c49ab69819ac1f72c16473b99f93c718bb2bd

See more details on using hashes here.

File details

Details for the file raylib_drm-5.5.0.2-cp310-cp310-manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.5.0.2-cp310-cp310-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 4d788a31fd78c259208097c69de3c59880cf6085552e1c61ae321a480fc7530b
MD5 6e7d9d1cb34ce0ae9e00f8acd00d21d4
BLAKE2b-256 0bd00ce429ad95cf1e33db79f638aa6055316516fa43793a65a8306a0f66dfea

See more details on using hashes here.

File details

Details for the file raylib_drm-5.5.0.2-cp39-cp39-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.5.0.2-cp39-cp39-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1b22c6f4b4e76ea316482fd14b002d5681c94d12e2c12bc5f4ab9b915c0a5090
MD5 38374563e3c9461a4c48b53571bc15e7
BLAKE2b-256 1ec9b41db989c1f0410ddc3485fea1a0a53a4ac1f7dc5ab51b821ec465341af7

See more details on using hashes here.

File details

Details for the file raylib_drm-5.5.0.2-cp39-cp39-manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.5.0.2-cp39-cp39-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 49b26103344b4ab944ff154199eb9c74faa927bec2d7288c45ea1bc7223f7a13
MD5 45d1961ccdefce14aa5ee581c56b6144
BLAKE2b-256 a2d1f4670c5c024af7e4bab96c59dd2a6c9ce8229306e9fae2e9c1fffec27314

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