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

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.0rc5-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.0rc5-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.0rc5-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.0rc5-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.0rc5-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.0rc5-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.0rc5-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.0rc5-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.0rc5-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.0rc5-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.0rc5-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.0rc5-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.0rc5-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.0rc5-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.0rc5-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.0rc5-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.0rc5-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.0rc5-pp311-pypy311_pp73-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc5-pp311-pypy311_pp73-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 84dfcbcc61c0f78365c7b9b71cc1acd5e9440935d6d952e8a607866045fbd59a
MD5 f1275ef4293e73348507bbb9e57ac0d5
BLAKE2b-256 df4e1ca0b9e04e41477b56cc508b461f6055d092ea9bd048292371c66be7b685

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc5-cp314-cp314-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 878b9d59f14ab62a889c9f54d357047805b809b682a1df293576ec624080b939
MD5 0721a9e72439c0c4064551a5beb4db73
BLAKE2b-256 9e3520658a5f2cf2aad31a65c0b41032a121ba9c1c94b2fd41f4aac373f660f2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc5-cp314-cp314-manylinux_2_24_i686.whl
Algorithm Hash digest
SHA256 2fc6ebbfb5fd858ce0b23e93d84263a21083f04936065a03e18aaf471ec392d0
MD5 a4d39a00714c1571ffe60ae599e54cca
BLAKE2b-256 f08625686060b42aa81e200e8fb5d218bded2d394c9bfa767193f0708c3d0219

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc5-cp314-cp314-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 aa2154127330ad64c44fac1a5de28ba4c29cf4b43c97a699367eda7c5d3155ea
MD5 8cf342f849f31aa6e386e622f186b3b2
BLAKE2b-256 02c406459cec3d9d4bd89da57a4dcc70ef20bb95043ae557d04b2e5a3c3ef2dc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc5-cp313-cp313-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 3eb8d300de03ed32d6076850d8fee9af7d456a40f573b584aa464e2742d33e48
MD5 a18a2ed1925fc79725e24a55ff33c646
BLAKE2b-256 3c40b74693d0ba1d853b9133c39466f71e91f2a6f369159959fa333398d34f71

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc5-cp313-cp313-manylinux_2_24_i686.whl
Algorithm Hash digest
SHA256 7705310c47c9885845bb90dd9ef8dbffa452f611d58b8d5650074d1b73334930
MD5 bd8ee992403fc0c87d8af9d07b583a0d
BLAKE2b-256 bc0cf8a9bc6e1e6315713b78be158df366ab66d0de6846bdc744b4d346c8eb9b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc5-cp313-cp313-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 9e5b3c87029b1969137179fdf6f035d2f3dc7f0b09d29d26bdf2e747d09c28c3
MD5 820b821c8e8942fd28ea90da6360823f
BLAKE2b-256 5a38995b3314423b6d38e9c04dd3747abd93402a62e1be8f033f85f50f7587c6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc5-cp312-cp312-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 c9b85eeac70d41ff37e9b6cbd1abaeeb40317c55d63ed8410f02a0129183abb6
MD5 60f4bea90d85a953241d68be5d0b260f
BLAKE2b-256 589422fe4ab121c4027f4322778c165bf2529a61bb64c51ebd7ebd1d9db88bad

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc5-cp312-cp312-manylinux_2_24_i686.whl
Algorithm Hash digest
SHA256 bfb87a72d06ba46f1ae429b361c7560ebee5c0084757882d461c54da943e22e0
MD5 7ea48f8a989a28c85e296ebc6fccd391
BLAKE2b-256 a4afb6ceaba976f90741e9b86f63d0e123a936f743dc8e928e8d09b08d1020b3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc5-cp312-cp312-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 a8fe0504acdb3c46f4f4e9761689c47228a04bc7b15782a90fbccad6a5ac3af2
MD5 e2ada49856b2c836e4a902e889e46bce
BLAKE2b-256 dab0dee0dd3d021b6cbd398316f169af040681ee5a03a2a7e00197d066e6aeb9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc5-cp311-cp311-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 6890341c915d51dd2a5755912c272a965e5a3a5f6b0e26d99cd95fc26cb418e8
MD5 baf4444c278eb6017b42b36145fb90f1
BLAKE2b-256 7fcb37c58b13c6cfbfaa9b6923aaffc4265d69406b412d28dd6f2a0b1a0905f5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc5-cp311-cp311-manylinux_2_24_i686.whl
Algorithm Hash digest
SHA256 be469c2f734ea7433e0865c291796f5c3c10b451430a435e4535b518e3c90665
MD5 6f1b8d844b69a91a6e3a4fc9d31f02e1
BLAKE2b-256 af2693f6c6f6e70f22e422a4ec44669e3d8e5b97fb0170ea83bc9a1d57b53ae1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc5-cp311-cp311-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 7dd27f07f30915d1b39c1b0a997b88f3ae52ff94109b5424f11930f8e4cd4263
MD5 bcc2c43a8a6f8df3ad6baa5aafb16e5a
BLAKE2b-256 94de30aea77d9ea5534e85f3efdc60cd5c807e82d15a6558aa9b24afcef08bbf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc5-cp310-cp310-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 11c9d00f00c543c621b21b90eeffc3e8401ab7228589ad0c8ccd67e5d67f17c8
MD5 8e86ed98c6aaa17173e7bd19c06dda1e
BLAKE2b-256 e53b034e73be7b7834350dac20ee13ff3ee0bb58ff59597d056d7befe804461b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc5-cp310-cp310-manylinux_2_24_i686.whl
Algorithm Hash digest
SHA256 3b304badae2ff95b00206e748dbdc5bcccaa7e2a089b90ee7043e5385c1f64ca
MD5 d651df75fad68486865a22ff8c9f3991
BLAKE2b-256 e96b577041a248e38fff4c20d640321dc7964fee34bd7e8e22060fafd0b44847

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc5-cp310-cp310-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 4088f4771ee543efdb51e2958400d13fb2061c2428e32512263056f59ffab529
MD5 eec989a09b55ab6f0529e2f890759ec2
BLAKE2b-256 1925f088e690a8b79edd660aea4ad60827383415ae5b054696ecabf81e726900

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc5-cp39-cp39-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 630ad1eff5075dbc3ebfcc1d2789c5609a095acf3510f6e7fd54150fcb08c4c3
MD5 f52b8e102d8dae8330dea48d875a64db
BLAKE2b-256 c079cd912f1b3da87ae69403985bdffe86da57757f8c1a79202177a9fa4c4ff8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc5-cp39-cp39-manylinux_2_24_i686.whl
Algorithm Hash digest
SHA256 0e54300b0e590377aee87087a40feb337626f759a50a6363980a08e2ff776d30
MD5 acf98aadaec921e0a011b37b6c639029
BLAKE2b-256 208bdc23d152dbcbb9921e2acfcfb790feec4b9f0742b144c398c556d394a16f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc5-cp38-cp38-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 0e96e1c598978a55c25991717c87e3b847a35c665b7c981c4840122d8f186f07
MD5 c085d64b0fd2ca2579a7c91e7c8a7692
BLAKE2b-256 72cff7b7beafb05c47a8f30bcfbf715be44bd5beb00df39b127368bc43030dc8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc5-cp38-cp38-manylinux_2_24_i686.whl
Algorithm Hash digest
SHA256 0f762ef64e6d0a21136a6013088aac43b3af41662bda593633fb0d590488dca8
MD5 b9e1c80f9f2f732c284bb7debafcd3a8
BLAKE2b-256 09123097686721e08e8aadd63c86ea4bc72dada2ca1e76549dd2aba17fce03bf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc5-cp37-cp37m-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 2c9c91a77b4e3be82dd70d18eded166bea8f9c307da8a7368373635a0ba1b741
MD5 9faf9c7c8e7fbf5be41dec511253d160
BLAKE2b-256 2f034283b70d10688b597b6a54ae39b248543fdd6cd1eed27f35eb26e9ab2f31

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for raylib_drm-6.0.0.0rc5-cp37-cp37m-manylinux_2_24_i686.whl
Algorithm Hash digest
SHA256 f85665b44869de968b6727caaa792c5e16b6dceb831d83b627f82daade99a248
MD5 b023bd864ed40d7bfd31bd6b3e181a33
BLAKE2b-256 47e90be6c53438b363de5054e1359418f37058914e73461c5310dff3707ff61f

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