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

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

Example project

Project generator

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

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.3

(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 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-5.6.0.0.dev3-pp311-pypy311_pp73-manylinux_2_24_x86_64.whl (2.6 MB view details)

Uploaded PyPymanylinux: glibc 2.24+ x86-64

raylib_drm-5.6.0.0.dev3-cp314-cp314-manylinux_2_35_aarch64.whl (2.4 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.35+ ARM64

raylib_drm-5.6.0.0.dev3-cp314-cp314-manylinux_2_24_x86_64.whl (3.3 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.24+ x86-64

raylib_drm-5.6.0.0.dev3-cp313-cp313-manylinux_2_35_aarch64.whl (2.4 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.35+ ARM64

raylib_drm-5.6.0.0.dev3-cp313-cp313-manylinux_2_24_x86_64.whl (3.3 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.24+ x86-64

raylib_drm-5.6.0.0.dev3-cp312-cp312-manylinux_2_35_aarch64.whl (2.4 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.35+ ARM64

raylib_drm-5.6.0.0.dev3-cp312-cp312-manylinux_2_24_x86_64.whl (3.3 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.24+ x86-64

raylib_drm-5.6.0.0.dev3-cp311-cp311-manylinux_2_35_aarch64.whl (2.3 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.35+ ARM64

raylib_drm-5.6.0.0.dev3-cp311-cp311-manylinux_2_24_x86_64.whl (3.3 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.24+ x86-64

raylib_drm-5.6.0.0.dev3-cp310-cp310-manylinux_2_35_aarch64.whl (2.3 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.35+ ARM64

raylib_drm-5.6.0.0.dev3-cp310-cp310-manylinux_2_24_x86_64.whl (3.3 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.24+ x86-64

raylib_drm-5.6.0.0.dev3-cp39-cp39-manylinux_2_24_x86_64.whl (3.3 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.24+ x86-64

raylib_drm-5.6.0.0.dev3-cp38-cp38-manylinux_2_24_x86_64.whl (3.3 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.24+ x86-64

raylib_drm-5.6.0.0.dev3-cp37-cp37m-manylinux_2_24_x86_64.whl (3.3 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.24+ x86-64

File details

Details for the file raylib_drm-5.6.0.0.dev3-pp311-pypy311_pp73-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.6.0.0.dev3-pp311-pypy311_pp73-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 e9a39080c7449339e580cf55c62f649e140fc37c5a8948b7547d5053bff01efd
MD5 cfff10fa89bc869ac63d1d4763d9833f
BLAKE2b-256 ff6134f7875a279d5c3487531fbf0897f9013b540a506766953ebe1f47599760

See more details on using hashes here.

File details

Details for the file raylib_drm-5.6.0.0.dev3-cp314-cp314-manylinux_2_35_aarch64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.6.0.0.dev3-cp314-cp314-manylinux_2_35_aarch64.whl
Algorithm Hash digest
SHA256 1c59e064b37f2a15c1c04a6479f3b7e01466b7ab54712b994998c4cd42e79346
MD5 bfabdf4caf0eeab1896410cd2c61ab4e
BLAKE2b-256 af39718a55f30d191cd8d1bea19b0d324ddd29f80eedf4ae41f4372b65e834f0

See more details on using hashes here.

File details

Details for the file raylib_drm-5.6.0.0.dev3-cp314-cp314-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.6.0.0.dev3-cp314-cp314-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 736f7b8a7217ab7f496b1343434b613b804572a4a5c9576703da68d8a896489f
MD5 be3b580aa01126aa31af58e069a8d601
BLAKE2b-256 d99ab829f4b0dc1828e6ba47fa84b5f6ded5962c50fceb24fe4e0e20f899205c

See more details on using hashes here.

File details

Details for the file raylib_drm-5.6.0.0.dev3-cp313-cp313-manylinux_2_35_aarch64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.6.0.0.dev3-cp313-cp313-manylinux_2_35_aarch64.whl
Algorithm Hash digest
SHA256 0ac0fd3c31623d0bf8558313c55e75b08d51381d652b6c8b1c9c1c4d059beebc
MD5 adca53f4f4ca02771a2a2f1339f30e88
BLAKE2b-256 ec9dfb443bd41bc5159307d31d466df629f043011f88dac6fd89d069e32b982f

See more details on using hashes here.

File details

Details for the file raylib_drm-5.6.0.0.dev3-cp313-cp313-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.6.0.0.dev3-cp313-cp313-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 390f36bfe99ca7988f8bfdad706994eb456b42ae13e4fdbc5a822076015507bb
MD5 e6929396f0774fb95c718c2c216eb180
BLAKE2b-256 9af67c4df913f8b5c27685c332ab7aaf63e68976107c777b2a0cc47c6f187ec8

See more details on using hashes here.

File details

Details for the file raylib_drm-5.6.0.0.dev3-cp312-cp312-manylinux_2_35_aarch64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.6.0.0.dev3-cp312-cp312-manylinux_2_35_aarch64.whl
Algorithm Hash digest
SHA256 89b0841791abc4fdd8384d307e026df97d5dfc61445db45e7d41cf963d339052
MD5 1151171ced87913daa4af096dcce6ead
BLAKE2b-256 22728c661b9158922f2cdefad663b12088b331ef6d35c62bf5d26e7d7834f48d

See more details on using hashes here.

File details

Details for the file raylib_drm-5.6.0.0.dev3-cp312-cp312-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.6.0.0.dev3-cp312-cp312-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 eacf07ab8d6e21445125b72b22ebb618340731fb180c5f71841b2434c2bfb9cf
MD5 ca349cb8bbcd5223bf618091e2eabaca
BLAKE2b-256 b13c047bed91491142822bf897ec2fffbe17869e87c0b91300d8173830c37e4a

See more details on using hashes here.

File details

Details for the file raylib_drm-5.6.0.0.dev3-cp311-cp311-manylinux_2_35_aarch64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.6.0.0.dev3-cp311-cp311-manylinux_2_35_aarch64.whl
Algorithm Hash digest
SHA256 92a4bc8ef8b88fc111a89f03d614063c799a2d67c8eaf1efb4fa53192e5d33d1
MD5 5fdc512e0e3058133e1696be687d8793
BLAKE2b-256 badd31104d91cc54a5b20d4d8452d1d66d5a964331f5dc4df150c22342e7537c

See more details on using hashes here.

File details

Details for the file raylib_drm-5.6.0.0.dev3-cp311-cp311-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.6.0.0.dev3-cp311-cp311-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 de6b9e4ccb6d1d70ca81b03774b11d18001f77d88ab6258f25a2823bf6d89c09
MD5 3b3c69751440a98290c307e5983a9751
BLAKE2b-256 bf8e57109f7d7907aa4714e6f421ad12eb61d5d1e5c5c9c481e06e324ed09d0b

See more details on using hashes here.

File details

Details for the file raylib_drm-5.6.0.0.dev3-cp310-cp310-manylinux_2_35_aarch64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.6.0.0.dev3-cp310-cp310-manylinux_2_35_aarch64.whl
Algorithm Hash digest
SHA256 d3a89b254b2f1da02828510f57e08541d5f563886b5f9d9eb5cf00683546d4fa
MD5 3794d5ad3f0b517842d7317e8198b3a6
BLAKE2b-256 769bfd43bd5d91fcb39204d12d36f37ce49da943df37df346d166fae6c7f77b3

See more details on using hashes here.

File details

Details for the file raylib_drm-5.6.0.0.dev3-cp310-cp310-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.6.0.0.dev3-cp310-cp310-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 31291a3ef697a1b2ab6482bc31ac0690ca863766a54574a1a555ff4953f89f9d
MD5 9f00146f617dd8d5b3bdb356a0e7f5c4
BLAKE2b-256 65a12b54e3401c6e4d90e9f1b8add899322efab0ddff00cada28e17586f30db1

See more details on using hashes here.

File details

Details for the file raylib_drm-5.6.0.0.dev3-cp39-cp39-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.6.0.0.dev3-cp39-cp39-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 ca45b0a6a5bca7c9c7cf6c1220f93178648cbfa58c84c31ce7fe74ce980af8c8
MD5 eb4360995bb8ec4e56f2a2845e0e3ec7
BLAKE2b-256 c633d420904d851180217c8f054be3e25aed46f83c24f6604efadf42df81399a

See more details on using hashes here.

File details

Details for the file raylib_drm-5.6.0.0.dev3-cp38-cp38-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.6.0.0.dev3-cp38-cp38-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 550e2dbeadcb7680b16ad449a0fcbf50de6e3a46fd65e2d0e7cd26b3577ab9ab
MD5 531cde7160973c1134790a833ad07d3d
BLAKE2b-256 01088c94bb78ea335f2a498d2658431301b33a09d01754f9128244e1a6bd6c1d

See more details on using hashes here.

File details

Details for the file raylib_drm-5.6.0.0.dev3-cp37-cp37m-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.6.0.0.dev3-cp37-cp37m-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 e0e98abbd1a9decc938905aa321ef09fc459f7aa309d5b0bcd7ed4a193a95ca2
MD5 36fed2fab3bb8c347f315729f921679a
BLAKE2b-256 7c68241788e6ef305712651faf0e41a1dc69cc6b241501f226ce35bbf8899dd1

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