Skip to content

Chore: Add path to PYTHONPATH in correct order#282

Open
iLLiCiTiT wants to merge 9 commits intodevelopfrom
bugfix/addons-in-pythonpath
Open

Chore: Add path to PYTHONPATH in correct order#282
iLLiCiTiT wants to merge 9 commits intodevelopfrom
bugfix/addons-in-pythonpath

Conversation

@iLLiCiTiT
Copy link
Member

Changelog Description

Make sure to prepend the paths to PYTHONPATH.

Additional info

We're addin them with correct order in sys.path which works for AYON launcher process but any child process that is not AYON launcher process will use PYTHONPATH which might have paths from some of previous processes.

Testing notes:

Not sure how to test it, we discovered this issue during project bundles testing which was fixed with ynput/ayon-core#1710 .

@iLLiCiTiT iLLiCiTiT added the type: bug Something isn't working label Feb 18, 2026
@iLLiCiTiT iLLiCiTiT self-assigned this Feb 18, 2026
@BigRoy
Copy link
Contributor

BigRoy commented Feb 18, 2026

Does this enforce our dependencies in front of PYTHONPATH? or which python paths are enforcing at the start here?

Because we'll want to make sure that a studio can still sneak in e.g. Houdini's own libs at start of PYTHONPATH if they provide their own c-compiled lib of a matching dependency that ours might just not happen to be binary compatible with?

Or better, if AYON application enviroments or tool environments prepend something - I do want to actually get whatever I'm prepending before the AYON vars 🤔

@iLLiCiTiT
Copy link
Member Author

Does this enforce our dependencies in front of PYTHONPATH? or which python paths are enforcing at the start here?

These are addons and dependency package dependencies.

@BigRoy
Copy link
Contributor

BigRoy commented Feb 19, 2026

These are addons and dependency package dependencies.

Would they still go after app envs in the final resolution? Because the whole click dependencies and some other conflicts we've had in the past could be worked around in Houdini by doing "PYTHONPATH": ["{houdini_libs_path}", "{PYTHONPATH}"] inside app envs at the time. Would that still? If not, I'm not sure this change is for the better.

@iLLiCiTiT
Copy link
Member Author

Because the whole click dependencies and some other conflicts we've had in the past could be worked around in Houdini by doing "PYTHONPATH": ["{houdini_libs_path}", "{PYTHONPATH}"] inside app envs at the time. Would that still? If not, I'm not sure this change is for the better.

Didn't think about it, true. In that case we probably should replace existing addons paths instead.

@BigRoy
Copy link
Contributor

BigRoy commented Feb 19, 2026

Didn't think about it, true. In that case we probably should replace existing addons paths instead.

Didn't we technically solve it with the change in core, or what is still the issue this is trying to solve?

@iLLiCiTiT
Copy link
Member Author

iLLiCiTiT commented Feb 19, 2026

Didn't we technically solve it with the change in core, or what is still the issue this is trying to solve?

We had to fix it there because this is wrong... This is the source of the issue. We should not keep e.g. ayon_deadline in pythonpath if project bundle does not have it enabled.

Comment on lines 805 to 831
addons_dir = Path(get_addons_dir())
dependencies_dir = Path(get_dependencies_dir())
dep_dir_idx = None
python_paths = []
for idx, path in enumerate(os.getenv("PYTHONPATH", "").split(os.pathsep)):
if not path:
continue

p_path = Path(path)
# Ignore addons
if p_path.is_relative_to(addons_dir):
continue

# Ignore dependencies dir and store index of the path
if p_path.is_relative_to(dependencies_dir):
if dep_dir_idx is None:
dep_dir_idx = idx
continue
python_paths.append(path)

addon_paths, dep_package_path = distribution.get_python_paths()

sys.path.insert(0, dep_package_path)
if dep_dir_idx is not None:
python_paths.insert(dep_dir_idx, dep_package_path)
else:
python_paths.append(dep_package_path)
Copy link
Contributor

@BigRoy BigRoy Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate a bit on the what and how? :) Why does it need to be before the dependencies dir path?

Copy link
Member Author

@iLLiCiTiT iLLiCiTiT Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It removes all paths in PYTHONPATH that leads to addons dir or dependecy packages dir.
(ignore paths %LOCALAPPDATA%/Ynput/AYON/addons/* & %LOCALAPPDATA%/Ynput/AYON/dependency_packages/*)

If there was dependency package, then remember it's position, and replace it with current dependency package dir path.

start.py Outdated
Comment on lines 809 to 823
for idx, path in enumerate(os.getenv("PYTHONPATH", "").split(os.pathsep)):
if not path:
continue

p_path = Path(path)
# Ignore addons
if p_path.is_relative_to(addons_dir):
continue

# Ignore dependencies dir and store index of the path
if p_path.is_relative_to(dependencies_dir):
if dep_dir_idx is None:
dep_dir_idx = idx
continue
python_paths.append(path)
Copy link
Contributor

@BigRoy BigRoy Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced the idx here will work, or at least in any meaningful way, because the idx will be shifted wrong due to some paths being skipped here while the idx counter continues forward. As such, the index in python_paths list would not match it? Because we're essentially 'inserting it' at the new python_paths that we're generating 🤔

Why do need the whole idx tracking, seems redundant/wrong? Seems we want to put in same order - but since we're building the full list - doesn't that just mean to .append directly here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my perspective we should always prepend it... But you said it is not good idea 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments