how to relaod python code fast

Hi. Im not sure if I’m doing things right (the fast way).

Ever time blender execute python *py, code it checks for compiled version *.pyc and if it exist it runs from it, without recompiling py file even if it was updated, right?

So to run updated *.py code I make flowing steps:

  • close blender
  • delete *.pyc files from script folder
  • run blender - then blender creates new *pyc file from fresh script

Is there faster way to reload script in blender? Preferably It would be cool if blender checked for py script change even if *.pyc exist and recompiled it on the fly…

I think you are really running into a problem with registered classes. When I make 1-off scripts that don’t register classes, then I can just change the text in the text editor and re-run it.

What are you changing?

I was not precise enough. I work on plugin and there are bunch of py files in folder, and all have pyc version.
So I wish, that If I change one or 2 py files, blender would recognize them and recompile appropriate pyc files.
Is that possible to do, without colosing blender and deleting manually all pyc files (it forces blender to recompile all plugin scripts on next start) ?

Look at how imp.reload() is used in some init.py files in the blender scripts addon folder.


if "bpy" in locals():
    import imp
    imp.reload(properties)
    imp.reload(ui)
    imp.reload(operators)
    imp.reload(export)

else:
    import bpy
    from . import properties
    from . import ui
    from . import operators
    from . import export

No!

“Python source code is automatically compiled into Python byte code by the CPython interpreter. Compiled code is usually stored in PYC (or PYO) files, and is regenerated when the source is updated, or when otherwise necessary.”
http://effbot.org/zone/python-compile.htm

This behavior is little different with blender though. Make sure you add the imp.reload stuff (see above) as well as register and unregister functions (e.g. bpy.utils.register_module)

When you updated .py code, hit F8 in Blender and it will reload all addons!