TankInitError: loading Toolkit platform from a different project

I am running some code to publish files from my app.
However in our case sometimes these files may belong to a different project.

I want to be able to find the context from the path to properly register these files (even if they belong in a different project).

How can I get around the TankInitError: You are loading the Toolkit platform from the pipeline configuration located in error?

We use the following function and it seems to work. The key is deleting all sgtk-related modules from sys.modules and removing TANK_CURRENT_PC from the environment. There was an article about this on the Shotgun website a while back.

def get_sgtk(proj_name, script_name):
    """ Load sgtk path and import module
    If sgtk was previously loaded, replace include paths and reimport
    """
    project_path = get_proj_tank_dir(proj_name)

    sys.path.insert(1, project_path)
    sys.path.insert(1, os.path.join(
        project_path,
        "install", "core", "python"
    ))

    # unload old core
    for mod in filter(lambda m: m.startswith("sgtk") or m.startswith("tank"), sys.modules):
        sys.modules.pop(mod)
        del mod

    if "TANK_CURRENT_PC" in os.environ:
        del os.environ["TANK_CURRENT_PC"]

    import sgtk
    setup_sgtk_auth(sgtk, script_name)
    return sgtk

Overall, I too find it annoying that you cannot use the same toolkit, but there’s all kinds of questions about versioning, etc.

Maybe this was the article you saw?

This one seems neater than I remember. I guess it was refreshed? Thanks for sharing!

Yes! We did refresh it last year and I changed the code a bit.

Hmmm, seems like this is going to be more hassle than it’s potentially worth :thinking:

Thanks though! It’s an interesting read!