Coder Social home page Coder Social logo

Comments (3)

t-wolfeadam avatar t-wolfeadam commented on June 16, 2024 1

@t-wolfeadam I just figured this out. I did exactly what you put here. I didn't know that pickle saved the path to the module used.

There's that, but still the issue of a discrepancy between loading from the active environment in an interactive shell (which works) versus when called via Pypeline (which does not) when the idea is that they should behave the same.

After looking into it more, the way Pypeline creates the Python environment causes __name__ to be set to "builtins" (vs "__main__" in an interactive shell). The same thing happens to PyCharm's shell (SO ref) - something for me to look into at some point

from anylogic-pypeline.

t-wolfeadam avatar t-wolfeadam commented on June 16, 2024

Interesting use case!

Is there a limitation with "where" on my computer pypeline actually runs?

I thought the answer was "no": before this problem, I would've touted that it runs the same way as if you execute a script or use in an interactive terminal relative to the model's directory. Turns out pickling is an exception!

To test this, I started by creating a setup (assumingly) similar to yours (see here) and generated the pkl object from running the shown script directly.

I then added a 'helper' script in the model directory for streamlining the python code needed in AL and for testing outside of AL (see here) - as you can see, running that directly does work. However, if I try importing the helper from a separate interactive terminal, it fails for a similar reason it does in AL (see here). Double however, the successful "direct loading" seen in the screenshot fails when using Pypeline -- that, I don't know why.

I did find a workaround though: you just need a custom unpickler that redirects the class lookup to the correct module. Updating the 'helper.py' script to look like the following allows it to work from anywhere.

class MyCustomUnpickler(pickle.Unpickler):
    def find_class(self, module, name):
        if module == "__main__":
            module = "pickler"  # script name that generated the file
        return super().find_class(module, name)

def get_obj():
    with open("python_files/pickling_test/pickled_object.pkl", "rb") as f:
        unpickler = MyCustomUnpickler(f)
        obj = unpickler.load()
    return obj

In case you want to explore yourself: Model323.zip

from anylogic-pypeline.

luke-leiter avatar luke-leiter commented on June 16, 2024

@t-wolfeadam I just figured this out. I did exactly what you put here. I didn't know that pickle saved the path to the module used.

from anylogic-pypeline.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.