The difference in behavior when using the Shotgun Python Console and the Python interpreter

Hello!
I am currently writing a python script to launch Maya from shotgun using the command line.

I’m confused here.
When I run the command I wrote, Maya starts up and the Redshift Render and menu’s mel set in the config are loaded.
However, the important sgtk is not imported and the shotgun panel does not show up.

# Error: Shotgun: Could not import sgtk! Disabling for now: No module named sgtk #

After starting Maya, I checked the path in sys.path, and it seems that the path of the sgtk module is not connected.

However, when I type the same command from the Shotgun Python Console and run it, this Maya will load sgtk and open the shotgun panel.
This looks identical to Maya launched from the Shotgun Desktop launcher…

I want to open Maya from Shotgun Desktop from Command Line, what’s wrong with this?
Or what am I missing?

my code:

import sys
import pprint

sg_python_path = r'C:\Program Files\Shotgun\Resources\Desktop\Python\tk-framework-desktopstartup\python\tk-core\python'
sys.path.append(sg_python_path)

import sgtk
# from sgtk.platform import Application

# Initialize the logger so we get output to our terminal.
sgtk.LogManager().initialize_custom_handler()
# Set debugging to true so that we get more verbose output, (should only be used for testing).
sgtk.LogManager().global_debug = True

# create an authenticator object. This is the main object which
# handles all authentication
authenticator = sgtk.authentication.ShotgunAuthenticator()
user = authenticator.get_user()
sgtk.set_authenticated_user(user)

# Start up a Toolkit Manager
mgr = sgtk.bootstrap.ToolkitManager()

# project information.
project_id = 0  # input ProjectID int
project = {"type": "Project", "id": project_id}

mgr.plugin_id = "basic.*"
mgr.pipeline_configuration = "Primary"
engine = mgr.bootstrap_engine("tk-shell", entity=project)

# Get application list.
pprint.pprint(engine.commands.keys())

if "maya2020" in engine.commands:
    engine.execute_command("maya2020", [])
engine.destroy()

My Environment
:Interpreter location I’m using

  • C:\Program Files\Shotgun\Python\python.exe

:OS

  • windows 10 Pro

:Shotgun Desktop version

  • App v1.6.1
  • Python 2.7.17
  • Startup v2.0.0
  • Engine v2.5.13
  • Core v0.19.18

Hi, sgtk is project centric, so you cannot just import it without using project context.

Hello, Kemot.

Thank you for your reply!
Your answer gave me a lot of hints.

I once rewrote the sgtk path to the project path.
However, this method did not work either.

sg_python_path = r'S:\project\shotgun\tk_config\install\core\python'
import sgtk

I still feel like something is missing …

But I missed a simpler answer.
I didn’t know there was a very easy way to boot the software from a tank command.

This is the closest and simplest I’ve been looking for!

I think your answer will give you a better understanding of Shotgun toolkit.
Thank you so much!