[solved] Error message when saving (pickle math module)

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
jeffster
Veteran
Posts: 384
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

[solved] Error message when saving (pickle math module)

#1 Post by jeffster »

Trying to save a game I'm getting an error:

Code: Select all

Exception: Could not pickle <module 'math' (built-in)>. (perhaps store.math = <module 'math' (built-in)>)
where "import math" is done in a called renpy label:

Code: Select all

label move (from_loc, to_loc):
    python:
        import math
        go_from = locations[from_loc]
        go_to = locations[to_loc]
        dx = abs( go_from[2] - go_to[2] )
        dy = abs( go_from[3] - go_to[3] )
        distance = int( math.sqrt( dx**2 + dy**2 ) )
        mess = "Flying from {i}[go_from[1]]{/i} to {i}[go_to[1]]{/i}.\nDistance: [distance] km."

    call screen confirm(mess, yes_action=Return("go"), no_action=Return("no"))
    #...and so on...
Traceback:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "renpy/common/00action_file.rpy", line 382, in __call__
    renpy.save(fn, extra_info=save_name)
Exception: Could not pickle <module 'math' (built-in)>. (perhaps store.math = <module 'math' (built-in)>)

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "renpy/common/_layout/screen_load_save.rpym", line 35, in script
    $ ui.interact()
  File "/home/otto/Games/RenPy/renpy-7.4.8-sdk/renpy/ast.py", line 923, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "/home/otto/Games/RenPy/renpy-7.4.8-sdk/renpy/python.py", line 2235, in py_exec_bytecode
    exec(bytecode, globals, locals)
  File "renpy/common/_layout/screen_load_save.rpym", line 35, in <module>
    $ ui.interact()
  File "/home/otto/Games/RenPy/renpy-7.4.8-sdk/renpy/ui.py", line 298, in interact
    rv = renpy.game.interface.interact(roll_forward=roll_forward, **kwargs)
  File "/home/otto/Games/RenPy/renpy-7.4.8-sdk/renpy/display/core.py", line 3276, in interact
    repeat, rv = self.interact_core(preloads=preloads, trans_pause=trans_pause, pause=pause, pause_start=pause_start, **kwargs)
  File "/home/otto/Games/RenPy/renpy-7.4.8-sdk/renpy/display/core.py", line 4110, in interact_core
    rv = root_widget.event(ev, x, y, 0)
  File "/home/otto/Games/RenPy/renpy-7.4.8-sdk/renpy/display/layout.py", line 1090, in event
    rv = i.event(ev, x - xo, y - yo, cst)
  File "/home/otto/Games/RenPy/renpy-7.4.8-sdk/renpy/display/transition.py", line 48, in event
    return self.new_widget.event(ev, x, y, st) # E1101
  File "/home/otto/Games/RenPy/renpy-7.4.8-sdk/renpy/display/layout.py", line 1090, in event
    rv = i.event(ev, x - xo, y - yo, cst)
  File "/home/otto/Games/RenPy/renpy-7.4.8-sdk/renpy/display/layout.py", line 1090, in event
    rv = i.event(ev, x - xo, y - yo, cst)
  File "/home/otto/Games/RenPy/renpy-7.4.8-sdk/renpy/display/screen.py", line 720, in event
    rv = self.child.event(ev, x, y, st)
  File "/home/otto/Games/RenPy/renpy-7.4.8-sdk/renpy/display/layout.py", line 1090, in event
    rv = i.event(ev, x - xo, y - yo, cst)
  File "/home/otto/Games/RenPy/renpy-7.4.8-sdk/renpy/display/layout.py", line 1297, in event
    rv = super(Window, self).event(ev, x, y, st)
  File "/home/otto/Games/RenPy/renpy-7.4.8-sdk/renpy/display/layout.py", line 273, in event
    rv = d.event(ev, x - xo, y - yo, st)
  File "/home/otto/Games/RenPy/renpy-7.4.8-sdk/renpy/display/layout.py", line 1090, in event
    rv = i.event(ev, x - xo, y - yo, cst)
  File "/home/otto/Games/RenPy/renpy-7.4.8-sdk/renpy/display/layout.py", line 1090, in event
    rv = i.event(ev, x - xo, y - yo, cst)
  File "/home/otto/Games/RenPy/renpy-7.4.8-sdk/renpy/display/layout.py", line 273, in event
    rv = d.event(ev, x - xo, y - yo, st)
  File "/home/otto/Games/RenPy/renpy-7.4.8-sdk/renpy/display/behavior.py", line 979, in event
    return handle_click(self.clicked)
  File "/home/otto/Games/RenPy/renpy-7.4.8-sdk/renpy/display/behavior.py", line 914, in handle_click
    rv = run(action)
  File "/home/otto/Games/RenPy/renpy-7.4.8-sdk/renpy/display/behavior.py", line 329, in run
    return action(*args, **kwargs)
  File "renpy/common/00action_file.rpy", line 382, in __call__
    renpy.save(fn, extra_info=save_name)
  File "/home/otto/Games/RenPy/renpy-7.4.8-sdk/renpy/loadsave.py", line 420, in save
    reraise(t, e, tb)
  File "/home/otto/Games/RenPy/renpy-7.4.8-sdk/renpy/loadsave.py", line 401, in save
    dump((roots, renpy.game.log), logf)
  File "/home/otto/Games/RenPy/renpy-7.4.8-sdk/renpy/loadsave.py", line 49, in dump
    cPickle.dump(o, f, cPickle.HIGHEST_PROTOCOL)
  File "/home/otto/Games/RenPy/renpy-7.4.8-sdk/renpy/python.py", line 2332, in module_pickle
    raise Exception("Could not pickle {!r}.".format(module))
Exception: Could not pickle <module 'math' (built-in)>. (perhaps store.math = <module 'math' (built-in)>)

Linux-5.10.53-1-MANJARO-x86_64
Ren'Py 7.4.8.1895
Luft Empire 0.2
(There was a thread with similar bug, but it's marked as solved, so I'm starting a new thread.)

PS. Solved: import libraries at the init time (init python).
Last edited by jeffster on Wed Aug 04, 2021 9:08 am, edited 1 time in total.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2384
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Re: Error message when saving (pickle math module)

#2 Post by Ocelot »

Library import must be done at init time.

If you will import it during the game, imported names will be recognised as "changed during game" and added to the save file. As you already noticed, Pickle cannot save modules.
< < insert Rick Cook quote here > >

jeffster
Veteran
Posts: 384
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

Re: Error message when saving (pickle math module)

#3 Post by jeffster »

Thank you, Ocelot. I hope your answer will be placed in the Ren'Py documentation. =)

PS. Actually I tried to do that, but got another error (doesn't happen now), probably because of some caching issue or something. So having docs on that would be great.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2384
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: [solved] Error message when saving (pickle math module)

#4 Post by Ocelot »

It actually does mention that you should import modules in init phase:
https://www.renpy.org/doc/html/python.h ... d-packages
In either case, the module or package can be imported from an init python block:
< < insert Rick Cook quote here > >

Post Reply

Who is online

Users browsing this forum: Google [Bot]