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

video

video

more videos

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.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 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.0.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.0.0rc4-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.0.0rc4-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.0.0rc4-cp314-cp314-manylinux_2_24_i686.whl (3.3 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.24+ i686

raylib_drm-6.0.0.0rc4-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.0.0rc4-cp313-cp313-manylinux_2_24_i686.whl (3.3 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.24+ i686

raylib_drm-6.0.0.0rc4-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.0.0rc4-cp312-cp312-manylinux_2_24_i686.whl (3.3 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.24+ i686

raylib_drm-6.0.0.0rc4-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.0.0rc4-cp311-cp311-manylinux_2_24_i686.whl (3.3 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.24+ i686

raylib_drm-6.0.0.0rc4-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.0.0rc4-cp310-cp310-manylinux_2_24_i686.whl (3.3 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.24+ i686

raylib_drm-6.0.0.0rc4-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.0.0rc4-cp39-cp39-manylinux_2_24_i686.whl (3.3 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.24+ i686

raylib_drm-6.0.0.0rc4-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.0.0rc4-cp38-cp38-manylinux_2_24_i686.whl (3.3 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.24+ i686

raylib_drm-6.0.0.0rc4-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.0.0rc4-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.0.0rc4-pp311-pypy311_pp73-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc4-pp311-pypy311_pp73-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 8a6b9336fddef1d987f27ab51fea1c6b0ff0b469f1d03115c922c7c7b42fa712
MD5 fb0e5bca3f9665889c5534bed77f2f1f
BLAKE2b-256 a80afef271524f80aac250a90cd8126c6ce999ab578de76be4874079e2d01dde

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.0.0rc4-cp314-cp314-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc4-cp314-cp314-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 8bf55623b386e2ce1a9c0a71066deecd3e9c9918401e55c2b3d6c94008a40e06
MD5 0e9e6d66de47b00ea4626103d0318e52
BLAKE2b-256 c0efe8750f64b98588914ad4422d3669024e6b5af88395244008300e949cad04

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.0.0rc4-cp314-cp314-manylinux_2_24_i686.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc4-cp314-cp314-manylinux_2_24_i686.whl
Algorithm Hash digest
SHA256 397c9e8435371cd998471af535e753e4b37f640aeeb81022362d9a2b11eb93a4
MD5 863865b7c9e700726da211181195a2e7
BLAKE2b-256 00b8146d5edab170906402c516f82585c22266e2e1f060ae549923c65b699182

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.0.0rc4-cp314-cp314-manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc4-cp314-cp314-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 8b26d0d2d16a63c9aa33a0d27047712c7f2b30b6193f8786435a9588fca4dbcd
MD5 1c6a911a6e9182af45c529a820c80fe2
BLAKE2b-256 186213651a36f21306d65c9021d5380af4e317c1a5d6ab96977f3c5c7879652f

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.0.0rc4-cp313-cp313-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc4-cp313-cp313-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 feb594658f230ecdb683370da46de538494296035dde4e3e5c67d332378427b0
MD5 8a74c742c8fbc5e6279ce05a14ca38f3
BLAKE2b-256 d7a957a9317a8510582d99254b841428e076d5ad4a947ef3a564f3989b88462d

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.0.0rc4-cp313-cp313-manylinux_2_24_i686.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc4-cp313-cp313-manylinux_2_24_i686.whl
Algorithm Hash digest
SHA256 263c3d254002ffce2590546aa7beb4725330e6353ea40b1722aa9c124111cbf7
MD5 ed534fd0850e8e70df0c08ca79457b58
BLAKE2b-256 e55d7e31ed427466d3ce2245c6d432f73e51c5b8b892826e9c344b35327990a4

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.0.0rc4-cp313-cp313-manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc4-cp313-cp313-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 de6bd08b7c101ef656052a75073cc30531997002ab784b081ff47700a09ed82a
MD5 f9fd089be7b3589ffb530c5651d0b6f5
BLAKE2b-256 79c98c2da9cb4053835b198df16a75b133f3f7bbe833581bf179f824f67f8dc6

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.0.0rc4-cp312-cp312-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc4-cp312-cp312-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 e10b11b6553ba8c53d5442e823765e9e7eff35a4633b2b375568caefc9d5dee1
MD5 1e54fbefd8e21eaf70da871542820726
BLAKE2b-256 ac6f7c7312efb1d0e5edc2297347243b276254b15d571dad8b033d6ecc7b4c49

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.0.0rc4-cp312-cp312-manylinux_2_24_i686.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc4-cp312-cp312-manylinux_2_24_i686.whl
Algorithm Hash digest
SHA256 30c7ec8cb9433eb7333fcff33221f66ba28d3b217ba3bc8a5ec27688683f35d6
MD5 2c3476a0a646bcb8420e698c5ae32f8a
BLAKE2b-256 214bf8a710cb8dcfd547c91553d459810b0b663798e408d8a96516d1e203be6d

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.0.0rc4-cp312-cp312-manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc4-cp312-cp312-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 75f7b9a143224e95065308960ab22afa4c01c885aba552262d43aba7f7489a62
MD5 cca501b7c8f1c5129d8f0a54a6248e81
BLAKE2b-256 4d83e1a41f21599c400a0d33d0c6ab80909412a683b58f96887ead1b6c2e2722

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.0.0rc4-cp311-cp311-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc4-cp311-cp311-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 2162b31fb65690f76dae9b8e781c1f30bbf56fac636980e22c7704adceeebfcc
MD5 2f4acc6e1fd2ba6804e33972ddf48cf1
BLAKE2b-256 d2c74d784a3ba973b18dc162955a32dba588afed44dd34a5cf8e1f9fe7fabf0e

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.0.0rc4-cp311-cp311-manylinux_2_24_i686.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc4-cp311-cp311-manylinux_2_24_i686.whl
Algorithm Hash digest
SHA256 3f45eb06c84ae345c154270690593eaf9eb8db8e837877ec2d3bd975448e7a35
MD5 2987f62478ee908ec0b0919799120a7e
BLAKE2b-256 62da8342dfafcc8775cca1f01ba8f62fe86f5b6f50540e01d3a84105793b3cf9

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.0.0rc4-cp311-cp311-manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc4-cp311-cp311-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 10529aff825857633c64d6105e647e06cb29ba37eb0c9bdf4724148e6ea30439
MD5 8e7ca0c12c94c99ac4e6e446565ddfd4
BLAKE2b-256 eee013dc250c3750e02ff1e83b697f80fd45108883ced7f053d0095345c15bce

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.0.0rc4-cp310-cp310-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc4-cp310-cp310-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 0a70070a902892c7edf48435c26a444f27b7b8d81ed42d81d90cc476715c3b66
MD5 f9730a78872d6c7e10e65cbc73d7e211
BLAKE2b-256 ca395bf024315f0aef6df003a0b3bb3b71adef51495b7bb8d51d0f948415337e

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.0.0rc4-cp310-cp310-manylinux_2_24_i686.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc4-cp310-cp310-manylinux_2_24_i686.whl
Algorithm Hash digest
SHA256 d259d8f4d6b12711a64a5e316c63544fb23bb4c272d657dad4d9a8e6f9cb202f
MD5 06e530bce674dd917758d917daf4062e
BLAKE2b-256 75ab3d39b10ab996c289042568c441d47ccf102a2547635d09829d29c22b8551

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.0.0rc4-cp310-cp310-manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc4-cp310-cp310-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 9d4535e81a26863120bbd712d1f62d0d3cb8626e4929d51821b15efe47263889
MD5 a713dbafad7438bd722f925ad799ea41
BLAKE2b-256 c9adfd8c2d4cbd3869d26e61a8e30d171ac60e4d2c89a9c969ff2eee63697516

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.0.0rc4-cp39-cp39-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc4-cp39-cp39-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 c23bab2928acb667051fe3ad250892b0b4e9933ecf01d17ae9f45bcea19cd001
MD5 6d3d16f52253f00c840602822b7ea32f
BLAKE2b-256 cd96bc59bddb8f59a64ae5ccac30b60f47935bcafacdc3be5e1268441c9eb327

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.0.0rc4-cp39-cp39-manylinux_2_24_i686.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc4-cp39-cp39-manylinux_2_24_i686.whl
Algorithm Hash digest
SHA256 ae69d7d50d0d39e1dd3804798998f39f12f8fbfdccb2cea4bb58e7c6823f5fd0
MD5 b97b3b0031b883d1fe94c975001016af
BLAKE2b-256 0e96e03f85d2d7ee6c6c6dc95850878abee1ec0116f4d9a979d751caba8c4e95

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.0.0rc4-cp38-cp38-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc4-cp38-cp38-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 0e1beacea72bd2658d1132bbecd86d32dccce516c331ea900756a451914165c8
MD5 379251c9b7c3acc0cb158b182301a592
BLAKE2b-256 379ca5aadbc9fd16028dac000b6f509225f54244e8d1abbbe0fd7704ab091ddb

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.0.0rc4-cp38-cp38-manylinux_2_24_i686.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc4-cp38-cp38-manylinux_2_24_i686.whl
Algorithm Hash digest
SHA256 8f7e53de4b54ae35e69aabbf196b7f942b42a2c9c3b7dea7f34ebcd2b9ee7f11
MD5 5340ec7e50a0e2479b413a66600aed42
BLAKE2b-256 59ab5144f0ba335885773051436fdc1a9cd75ecf1d8bb2a5ca70873c6471a4d9

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.0.0rc4-cp37-cp37m-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc4-cp37-cp37m-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 63b88b2a1602ef5d0af5dc28b5e0392a91b40d2ca8bc6b41df37f168262d07db
MD5 fb3b3e2b999c5c28c819ba1549798599
BLAKE2b-256 ce25a671f4bace7595eb85085e77d44afe17b233e983e11f70f2cd7c5bc2ca64

See more details on using hashes here.

File details

Details for the file raylib_drm-6.0.0.0rc4-cp37-cp37m-manylinux_2_24_i686.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc4-cp37-cp37m-manylinux_2_24_i686.whl
Algorithm Hash digest
SHA256 137bec9827d0deff768ce4bfbe54837683060adb00009e9d041a2987d779a739
MD5 e3b29dbd8bae6b54f0ccc2cb5347c5a7
BLAKE2b-256 9a56fc7f053e47274bcd6da5f7ae70991c633d1b3a5f2b2a7e13fead5705f169

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