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

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

Uploaded PyPymanylinux: glibc 2.24+ x86-64

raylib_drm-5.5.0.3-pp310-pypy310_pp73-manylinux_2_24_x86_64.whl (2.6 MB view details)

Uploaded PyPymanylinux: glibc 2.24+ x86-64

raylib_drm-5.5.0.3-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.5.0.3-cp314-cp314-manylinux2014_aarch64.whl (1.9 MB view details)

Uploaded CPython 3.14

raylib_drm-5.5.0.3-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.5.0.3-cp313-cp313-manylinux2014_aarch64.whl (2.0 MB view details)

Uploaded CPython 3.13

raylib_drm-5.5.0.3-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.5.0.3-cp312-cp312-manylinux2014_aarch64.whl (2.0 MB view details)

Uploaded CPython 3.12

raylib_drm-5.5.0.3-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.5.0.3-cp311-cp311-manylinux2014_aarch64.whl (1.9 MB view details)

Uploaded CPython 3.11

raylib_drm-5.5.0.3-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.5.0.3-cp310-cp310-manylinux2014_aarch64.whl (1.9 MB view details)

Uploaded CPython 3.10

File details

Details for the file raylib_drm-5.5.0.3-pp311-pypy311_pp73-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.5.0.3-pp311-pypy311_pp73-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 3cd9e9a619d5c023709e8843c93a8af874e4e9c959308d8f4784c63d91e4a271
MD5 f90c52913bb7fa0b9d6e4b3fddcdf8af
BLAKE2b-256 ed3a9ad23eb203a64dbdc4766ca09bed997ddcbd9c392fd49bc764cbd6bb7b3e

See more details on using hashes here.

File details

Details for the file raylib_drm-5.5.0.3-pp310-pypy310_pp73-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.5.0.3-pp310-pypy310_pp73-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 b863adacf3ce2318c1c50a8223fa211a813edc825f17b71bcaf165d29ef28bde
MD5 58a87929abf2b873ac795d9ca043e0e3
BLAKE2b-256 445ad3e5d23a968eb0095e502582aa73d346f63ae3e588a2171baf5664fdcdcf

See more details on using hashes here.

File details

Details for the file raylib_drm-5.5.0.3-cp314-cp314-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.5.0.3-cp314-cp314-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 c522e15cc230a1a4f9d3366fadbf5d47dcc51420e041203be2a2d0d6ed7e66af
MD5 65157a2e5def17c0178dd97acf1cbfc9
BLAKE2b-256 98a792c49ff32d317348c8a6b244af471d9b162e9cbab75de011f810460b8d36

See more details on using hashes here.

File details

Details for the file raylib_drm-5.5.0.3-cp314-cp314-manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.5.0.3-cp314-cp314-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 541accb671ae4a951b67b1facdcb17307ead13e8ff05269042e5e15af944cc89
MD5 89eb0882e390848101d227095d51e3c5
BLAKE2b-256 1d1510fe84553ec8e633570cccb661c59e6c9b2639770c221f6a412a90b021e9

See more details on using hashes here.

File details

Details for the file raylib_drm-5.5.0.3-cp313-cp313-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.5.0.3-cp313-cp313-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 cd1d4875ee43a706a6d9387fc60a75983c0d51bff2de3a6c9299f71ae5e636e0
MD5 8c564b54ac5da7a6d5259211dd4928bb
BLAKE2b-256 86d3f40f56dc9278018824552b91ed63b9a3f030a9bf93f57f16384c9d96dbf7

See more details on using hashes here.

File details

Details for the file raylib_drm-5.5.0.3-cp313-cp313-manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.5.0.3-cp313-cp313-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 b2cb3a3325d50c8b9dab812c7a79781b69cf5e6070f761f80e518157cdd5fa08
MD5 16707ecebee994c2d2dea0a9cbbf440e
BLAKE2b-256 cc04016fa74ef7f81cda0add6c9c74dbe0f761070ab60990e05481edc0a053db

See more details on using hashes here.

File details

Details for the file raylib_drm-5.5.0.3-cp312-cp312-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.5.0.3-cp312-cp312-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 bf36ae52c1db1b1cf5829c1f575a1d42696f82e2b1ca163165829233c615c746
MD5 e370f780e3004137769286480f3d42cb
BLAKE2b-256 695d39a69fd9531acdf0efbfb9c99997c28ce43a8c416c8fde505c847a8e0df1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for raylib_drm-5.5.0.3-cp312-cp312-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 2b6e192403b962094e41cfeb962e49cdd0fefc6d4b6fd9943cd5c1e3adc6be9d
MD5 fec2ea585bc25acbca2155f49fa7a472
BLAKE2b-256 f2e61883c451322a42d0aea1c526b4e4e03cdc72f0cd8edcb57cd920d746772f

See more details on using hashes here.

File details

Details for the file raylib_drm-5.5.0.3-cp311-cp311-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.5.0.3-cp311-cp311-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 bd1aba306904b13d55a96ec18c277cc40d17c2db6f02c501be1a184c8bcb29a8
MD5 9241695a1a6317f2774eb4c4d4193f3d
BLAKE2b-256 21b3bd1600011c8a89a43007def6786a92c5bc3c975334add0f7e86b0fe16ae1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for raylib_drm-5.5.0.3-cp311-cp311-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 1c59439d35461ab338b2d05b440690ff01c3bb3c1763974356717cf79842d768
MD5 ea371aa10a9cda758db70d3a15204ef0
BLAKE2b-256 bbbc58eeb5b82a57016fe292674574aef671e31182127621fc6067b8ab8440ab

See more details on using hashes here.

File details

Details for the file raylib_drm-5.5.0.3-cp310-cp310-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.5.0.3-cp310-cp310-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 efcdb112ce179d78204873fe198f9d67c990a3435312d274fa58550ea6d0a309
MD5 67b73ba1874cc10e5dc72cd1980ea244
BLAKE2b-256 b9fae1db98812bfd243f5c2aff0ae06a5518c3d8a4545aab1267db3e619fb716

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for raylib_drm-5.5.0.3-cp310-cp310-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 2e5c5165693b15e70203ad4a7a328d6bf1d2a96f755721d78d948be283e9f2b7
MD5 771c4d12c59dd195b8779d6990451f30
BLAKE2b-256 35de49aa2b04acc26a5a19df7afe6d76914397a634ab32d68772b80ddf281f05

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