Skip to content

Fix spawned process imports by using absolute import paths #7773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

kevin314
Copy link

While developing a custom node involving multiprocessing, I ran into an issue where import utils.extra_config in main.py would fail with ModuleNotFoundError: No module named 'utils.extra_config'; 'utils' is not a package.

I believe this is happening because multiprocessing spawns new Python processes that re-execute the main module (main.py) and its imports in isolation. These child processes don't inherit the paths of the current working directory. As a result, relative imports like import utils.extra_config fail when the spawned process doesn't have the appropriate path context.

To resolve this, I added:
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) to the top of main.py

This ensures that ComfyUI is always on the import path, regardless of how or where main.py is executed.

I also updated the import to import ComfyUI.utils.extra_config, as adding the parent directory of ComfyUI to sys.path requires that imports be specified with the full package path from that point forward.

@comfyanonymous
Copy link
Owner

This is a bad because it's going to break all installs where the folder name is not "ComfyUI".

@kevin314
Copy link
Author

Thanks for the comment!

it's going to break all installs where the folder name is not "ComfyUI".

That's a good point!

After additional testing, it looks like we can actually just set the path as where main.py is located. No need to start from the parent directory. This way we avoid issues if the ComfyUI directory is renamed.

@kevin314
Copy link
Author

This is ready for another review! Open to alternative solutions for this issue if anyone has other ideas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants