Are you copying your model somewhere in the script? It’s possible you may be creating multiple copies of something which may be messing up stuff.
Also, code and error message are needed to help out really. There is no context without it…