Repository: ynput/OpenPype
Issue: Handle python 2/3 host dependencies in Deadline 

## Bug

When python 2 host application is used for rendering on Deadline, we don't run `pre_python_2_prelaunch.py` adding python 2 specific modules. But the environment is injected with python 3 code and this will crash openpype specific stuff initialized there (for example avalon-core initialization, etc.). After `GlobalJobPreload` script is done and host application is launched, you'll see errors similar to this in Job Reports:

```
2022-03-01 16:02:46:  0: STDOUT: Failed to execute userSetup.py
2022-03-01 16:02:46:  0: STDOUT: Traceback (most recent call last):
2022-03-01 16:02:46:  0: STDOUT:   File "E:\\projects\\openpype\\3x\\openpype\\hosts\\maya\\startup\\userSetup.py", line 2, in <module>
2022-03-01 16:02:46:  0: STDOUT:     import avalon.api
2022-03-01 16:02:46:  0: STDOUT:   File "E:\\projects\\openpype\\3x\\repos\\avalon-core\\avalon\\api.py", line 13, in <module>
2022-03-01 16:02:46:  0: STDOUT:     from . mongodb import (
2022-03-01 16:02:46:  0: STDOUT:   File "E:\\projects\\openpype\\3x\\repos\\avalon-core\\avalon\\mongodb.py", line 6, in <module>
2022-03-01 16:02:46:  0: STDOUT:     import pymongo
2022-03-01 16:02:46:  0: STDOUT:   File "E:\\projects\\openpype\\3x\\.venv\\lib\\site-packages\\pymongo\\__init__.py", line 116, in <module>
2022-03-01 16:02:46:  0: STDOUT:     from pymongo.mongo_client import MongoClient
2022-03-01 16:02:46:  0: STDOUT:   File "E:\\projects\\openpype\\3x\\.venv\\lib\\site-packages\\pymongo\\mongo_client.py", line 46, in <module>
2022-03-01 16:02:46:  0: STDOUT:     from pymongo import (common,
2022-03-01 16:02:46:  0: STDOUT:   File "E:\\projects\\openpype\\3x\\.venv\\lib\\site-packages\\pymongo\\uri_parser.py", line 32, in <module>
2022-03-01 16:02:46:  0: STDOUT:     from pymongo.srv_resolver import _HAVE_DNSPYTHON, _SrvResolver
2022-03-01 16:02:46:  0: STDOUT:   File "E:\\projects\\openpype\\3x\\.venv\\lib\\site-packages\\pymongo\\srv_resolver.py", line 18, in <module>
2022-03-01 16:02:46:  0: STDOUT:     from dns import resolver
2022-03-01 16:02:46:  0: STDOUT:   File "E:\\projects\\openpype\\3x\\.venv\\lib\\site-packages\\dns\\resolver.py", line 809
2022-03-01 16:02:46:  0: STDOUT:     raise NoResolverConfiguration(f'cannot open {f}')
```

Changed files:
openpype/lib/applications.py, openpype/hooks/pre_python_2_prelaunch.py

diff --git a/openpype/hooks/pre_python_2_prelaunch.py b/openpype/hooks/pre_python_2_prelaunch.py
deleted file mode 100644
index 84272d2e5d..0000000000
--- a/openpype/hooks/pre_python_2_prelaunch.py
+++ /dev/null
@@ -1,35 +0,0 @@
-import os
-from openpype.lib import PreLaunchHook
-
-
-class PrePython2Vendor(PreLaunchHook):
-    """Prepend python 2 dependencies for py2 hosts."""
-    order = 10
-
-    def execute(self):
-        if not self.application.use_python_2:
-            return
-
-        # Prepare vendor dir path
-        self.log.info("adding global python 2 vendor")
-        pype_root = os.getenv("OPENPYPE_REPOS_ROOT")
-        python_2_vendor = os.path.join(
-            pype_root,
-            "openpype",
-            "vendor",
-            "python",
-            "python_2"
-        )
-
-        # Add Python 2 modules
-        python_paths = [
-            python_2_vendor
-        ]
-
-        # Load PYTHONPATH from current launch context
-        python_path = self.launch_context.env.get("PYTHONPATH")
-        if python_path:
-            python_paths.append(python_path)
-
-        # Set new PYTHONPATH to launch context environments
-        self.launch_context.env["PYTHONPATH"] = os.pathsep.join(python_paths)
diff --git a/openpype/lib/applications.py b/openpype/lib/applications.py
index e72585c75a..ad59ae0dbc 100644
--- a/openpype/lib/applications.py
+++ b/openpype/lib/applications.py
@@ -1319,6 +1319,41 @@ def _merge_env(env, current_env):
     return result
 
 
+def _add_python_version_paths(app, env, logger):
+    """Add vendor packages specific for a Python version."""
+
+    # Skip adding if host name is not set
+    if not app.host_name:
+        return
+
+    # Add Python 2/3 modules
+    openpype_root = os.getenv("OPENPYPE_REPOS_ROOT")
+    python_vendor_dir = os.path.join(
+        openpype_root,
+        "openpype",
+        "vendor",
+        "python"
+    )
+    if app.use_python_2:
+        pythonpath = os.path.join(python_vendor_dir, "python_2")
+    else:
+        pythonpath = os.path.join(python_vendor_dir, "python_3")
+
+    if not os.path.exists(pythonpath):
+        return
+
+    logger.debug("Adding Python version specific paths to PYTHONPATH")
+    python_paths = [pythonpath]
+
+    # Load PYTHONPATH from current launch context
+    python_path = env.get("PYTHONPATH")
+    if python_path:
+        python_paths.append(python_path)
+
+    # Set new PYTHONPATH to launch context environments
+    env["PYTHONPATH"] = os.pathsep.join(python_paths)
+
+
 def prepare_app_environments(data, env_group=None, implementation_envs=True):
     """Modify launch environments based on launched app and context.
 
@@ -1331,6 +1366,8 @@ def prepare_app_environments(data, env_group=None, implementation_envs=True):
     app = data["app"]
     log = data["log"]
 
+    _add_python_version_paths(app, data["env"], log)
+
     # `added_env_keys` has debug purpose
     added_env_keys = {app.group.name, app.name}
     # Environments for application