Ren'Py python: blocks: init-phase execution and practical usage
Why Ren'Py python: blocks fail outside init and how to use them for persistent runtime state without freezing your game.
10 Oct 2025, 11:23 UTC

Problem: python blocks execute only during init
You write a python: block in your RenPy script expecting it to run when a player clicks a menu option or reaches a new screen, but the code never executes at that moment. The block runs only once, before the game even shows the first label. This init-only behavior catches many developers off guard, especially those transitioning from other engines or adding logic late in development.
Useful takeaway
Reserve python: blocks for initialization-time setup such as defining persistent variables hooking engine callbacks or loading resources and use python commands inside screens menus or call python statements for any code that must run during gameplay.
How python: blocks work
RenPy distinguishes between two Python execution contexts. The python: block (with a trailing colon) is a declaration that RenPy processes during the init phase which runs before any label before the title screen and before the player sees any content. Because it completes before the game state is visible any output or side effects are visible only in the developer console or through engine side channels. After init finishes the block is gone and the engine proceeds to parse labels render screens and handle player input.
python: renpy.python._init_flag = True; renpy.log('_verification_log set')Worked configuration: setting persistent state at init
Suppose you want a global flag that indicates whether a debug menu is available and you want to log that the flag was set. You can place the following in script.rpy inside an init block or at the top level (both are treated as init-phase):
python: renpy.python.debug_menu_enabled = True; renpy.log('debug menu flag set during init')Later from any screen or menu you can read that flag using a runtime python block:
screen main_menu(): python: if renpy.python.debug_menu_enabled: passCommon mistakes
- Using python: for UI updates or click-time logic. Because the block runs before the first screen appears any code inside it has no effect on what the player sees during gameplay.
- Including blocking I/O or long-running loops. Placing a time.sleep() file read-write with no async handling or a while True: loop inside a python: block will halt the init sequence. The player will see a blank screen or an endless loading spinner.
- Assuming variables set in python: are automatically available without the renpy.python prefix. The namespace is the designated way to store persistent data; direct module-level variables are not accessible later unless explicitly stored under renpy.python.
Verification steps
- In a new RenPy project add python: renpy.log('verification') inside an init block in script.rpy. Launch the game press ~ to open the developer console (enable with config.developer = True in options.rpy) and confirm the logged message appears.
- Relocate the python: block to after the first label statement and restart. The log message should not appear confirming the init-phase execution constraint.
- From a screen or menu python block access a variable set in an init python: block (e.g. if renpy.python._init_flag) to demonstrate runtime access patterns. Note that screen objects are only available after init completes.
Version assumptions
The init-only execution model for python: blocks has been consistent across RenPy 6.x through 8.x. Newer versions add stricter warnings for blocking code inside init blocks. Assume the latest stable release (RenPy 8.2 at time of writing) unless you are on a legacy series; the same patterns apply.
Decision summary
- Use python: blocks for: defining renpy.python variables registering init-time engine hooks one-time resource adjustments.
- Avoid python: blocks for: per-click logic frame-rate dependent updates any I-O that cannot be safely interrupted.
- Always test init-vs-runtime placement using the developer console the presence or absence of your log message is a reliable indicator.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.