SOLVED: Issue with order of extension re-initialisation

I am having an issue initialising extensions on startup without getting errors. I have already implemented a workaround, which consists in force-reinit-ing all the extensions post-startup like so:

def SoftReset(self) -> None:
	for shortcut in op:
		if shortcut.isupper() and shortcut != 'UTILS':
			run(f"op.{shortcut}.par.reinitextensions.pulse()")

This works but is not ideal because the toe file always open with errors that get cleared on reinit.

The issue:
My system requires both external python libraries (installed in a specific folder) and to access custom classes from another python file (stored in a separate folder). This means that I cannot simply use the “Preferences → Python 64-bit Module Path” because it accepts only a single path, and thus cannot accept both custom locations.

On startup, I do check for paths, and if they aren’t there, add them with the following method:

def AddDependenciesTopath(self) -> None:
	dep_path = f'{project.folder}/../python/libraries'
	norm_dep_path = os.path.normpath(dep_path)
	if norm_dep_path not in sys.path:
		sys.path.insert(0, norm_dep_path)
	
	scripts_folder = f'{project.folder}/../globscripts'
	norm_scripts_folder = os.path.normpath(scripts_folder)
	if norm_scripts_folder not in sys.path:
		sys.path.insert(0, norm_scripts_folder)

however this seems to run after the extensions tried to initialise and thus after all the errors have been thrown on the console. These errors, have all to do with libraries not being found:
ModuleNotFoundError:

Error retrieving extension for /SETTINGS: tdError: Module compilation error. See /SETTINGS/SettingsExt for details. 
<string>, line 1, in <module>
Context:(Extension 1)
DAT compile error: /project/engine_1/EngExt
Traceback (most recent call last):
  File "/project/engine_1/EngExt", line 1
    r = previousimport(*args, **kw)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'tdtypes'

Error retrieving extension for /project/engine_1: tdError: Module compilation error. See /project/engine_1/EngExt for details. 
<string>, line 1, in <module>
Context:(Extension 1)
DAT compile error: /project/engine_1/template1/VidExt
Traceback (most recent call last):
  File "/project/engine_1/template1/VidExt", line 2
    r = previousimport(*args, **kw)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'tdtypes'

This is also particularly weird because all my extensions are set to not reinit on start, so they should be able to remain dormant until the startup procedure has completed and thus the paths are present. I am sure I am doing it wrong, but I’ve tried multiple things and nothing works.

If the only way to solve this is to install the libraries at system level, and add environment variables, I will resort to that, but I would prefer not to.

You want to use the EnvContext which recently got introduced and resolves these issues and is a great tool.

Besied taking care of setting up the virtual environment you can also add the path to your own library using the extra-paths to the TDPyEnvManagerContext.yaml

The cool part is that you do not even need to use the EnvManagerCOMP but can use your own package manager like poetry, uv etc. if that is your jam (it is mine :wink: )

1 Like

Hi @plusplusone, thanks for this. I had seen it, but I was hoping to sort it out in a custom way because the TDPyEnvManager seems way too complex for my current needs.

However, if this is the standard way to go, I will have a look. Thank you very much!

The cool thing is that you do not need to use the EnvManagerCOMP itself, it is enough to create the TDPyEnvManagerContext.yaml file next to your project TOE-File and TD takes care of the rest, so realy easy to setup.

1 Like

The TDPyEnvManager COMP is really just a frontend around the context, and an helper to access CLI and other useful things quickly.

1 Like