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

Chatroom: Discord

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

Full documentation

Quickstart

pip3 install raylib==5.0.0.4

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

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

(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

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

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

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 must 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 my_project

Point your browser to http://localhost:8000

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

Tanki

Alloy Bloxel Editor

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

File details

Details for the file raylib_drm-5.5.0.0.dev3-pp310-pypy310_pp73-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.5.0.0.dev3-pp310-pypy310_pp73-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9689beba46d676e642516b3424fd360a0dc29490e87b2a8fde855c359dde9122
MD5 d89e96ce6173d5e0f4c2494fcb4c0dc6
BLAKE2b-256 91bb21750ebf03ad72e58b8440b3bbc2544545f79ab886769bcb5df7078cbd30

See more details on using hashes here.

File details

Details for the file raylib_drm-5.5.0.0.dev3-pp39-pypy39_pp73-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.5.0.0.dev3-pp39-pypy39_pp73-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f5687179cadb65e9636d402e35b79a14733301dd552cd4599da5a49cbf04b87a
MD5 6d05b5595ff431cd958a5965e2553173
BLAKE2b-256 16adb1de0c8e6e013b6966521a1a4bd7cb4779194b9b9189aa3b97059d19bad4

See more details on using hashes here.

File details

Details for the file raylib_drm-5.5.0.0.dev3-cp313-cp313-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.5.0.0.dev3-cp313-cp313-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e6c243422b1e3ed1c3a5fffcbd295ba2106cd0552f654c0a8c560275c27f27ca
MD5 8a43addd219fdf513f009d867d99cba3
BLAKE2b-256 b48efeb001660d12eca4b1544770479f3d1981f930e2cc54095240b9ad27ba67

See more details on using hashes here.

File details

Details for the file raylib_drm-5.5.0.0.dev3-cp312-cp312-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.5.0.0.dev3-cp312-cp312-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b10001c73cf4764ccd30779a83ae5ae5f952e5ed1b87e741a5a0dc02fe2467c6
MD5 b21614628f440f3d1dc989d8718f681e
BLAKE2b-256 9310a3af514b503a9ccc6314e7caa317850bd011952ce4307cd96e10ce1615c2

See more details on using hashes here.

File details

Details for the file raylib_drm-5.5.0.0.dev3-cp312-cp312-manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.5.0.0.dev3-cp312-cp312-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 c86d5ec72e82cd2a8df84f4e603677ab0254516679da660313676006fed53ad7
MD5 c4ab5718e06c5ee9164486720b728914
BLAKE2b-256 169abb37d3ea9345683f7c65a995aca8c9e6aeb1b99b22a4414ffd57ce9617cc

See more details on using hashes here.

File details

Details for the file raylib_drm-5.5.0.0.dev3-cp311-cp311-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.5.0.0.dev3-cp311-cp311-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b456d0423c35db7937db67d81befba558d25526840815464a9eb37cf39817b77
MD5 687f2cd8d5a056f0f6a8513007bd9ac0
BLAKE2b-256 5c708cc22dba00a71b7dfbd20ca18d05a2199b7022fd9e98e212170ee30f6650

See more details on using hashes here.

File details

Details for the file raylib_drm-5.5.0.0.dev3-cp311-cp311-manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.5.0.0.dev3-cp311-cp311-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 6c48f577d38ef55632718af36d3e696ce4a7d077de47a9995b27ce109a8e0ff8
MD5 15ce15ec1d6fec0167541cd5b0862bbd
BLAKE2b-256 54f0e5842dfe501ce7f3a470c5f622b3d7ea8a433ae139448323b86ee31e9cbd

See more details on using hashes here.

File details

Details for the file raylib_drm-5.5.0.0.dev3-cp310-cp310-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.5.0.0.dev3-cp310-cp310-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4f4de212227eda0e8d3a13c3215dc69dc75613883bb7c4be4ce34d0eb9b31b31
MD5 698a8fba716e4206d89fa208e85b6d23
BLAKE2b-256 21bf6690a18d4e6b3ac1acc35f73ded7e7d34534c8facbbeb84124d700e629d8

See more details on using hashes here.

File details

Details for the file raylib_drm-5.5.0.0.dev3-cp310-cp310-manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.5.0.0.dev3-cp310-cp310-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 d3edf32ba3c85aabdf23ec44b6aa750e29ebf0fba871b0380d327197ab9e5198
MD5 1209d0dff21898ae39ed9ec8585d5f4e
BLAKE2b-256 63a46f2ce3aea011fe466500a79c6c6ddf68dbad3a846f83bb150e32266e9c94

See more details on using hashes here.

File details

Details for the file raylib_drm-5.5.0.0.dev3-cp39-cp39-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.5.0.0.dev3-cp39-cp39-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b8702a1be857404071b4e8c7e0fc7758be457d31525edf1fdf17f48b951925d3
MD5 50a926bb6e7c3a68cca5775b0329ccf3
BLAKE2b-256 e6f8956a2e789ecc395891405a1e5ee99653c057f7108016d87e0dbb20001cfc

See more details on using hashes here.

File details

Details for the file raylib_drm-5.5.0.0.dev3-cp39-cp39-manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for raylib_drm-5.5.0.0.dev3-cp39-cp39-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 8a72a480f1db7a4c0d2934b837b430099c1c6a79a28717dc9cd9dac01f63a158
MD5 85f695dfa43b3023b81a5af5de47acde
BLAKE2b-256 7b79d6741e5491e8e601f3cf53959363ed3d23a03163cfd8057dacd46b5a8f84

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page