Skip to content

Optimize Path Handling to Improve Code Compatibility and Maintainability#377

Merged
chflame163 merged 2 commits intochflame163:mainfrom
lcolok:main
Oct 29, 2024
Merged

Optimize Path Handling to Improve Code Compatibility and Maintainability#377
chflame163 merged 2 commits intochflame163:mainfrom
lcolok:main

Conversation

@lcolok
Copy link
Contributor

@lcolok lcolok commented Oct 28, 2024

优化路径处理逻辑,提升代码兼容性和可维护性

问题背景

在使用 ComfyUI-to-Python-Extension 导出工作流为 Python 代码时发现,导出的代码无法正确执行。原因是原有代码通过 __main__ 计算 extensions 路径,这种方式依赖于执行文件的位置。当导出的 Python 文件在其他位置运行时,就会计算出错误的路径。

改动内容

  • 使用 ComfyUI 官方的 folder_paths 模块替代自定义路径计算
  • 移除对 __main__ 的依赖,改用 folder_paths.base_path 获取根路径
  • 优化目录创建逻辑,使用 os.makedirs 提供更可靠的目录创建

改进优势

  • 立即解决:修复 ComfyUI-to-Python-Extension 导出代码的执行问题
  • 长期可维护:依赖 ComfyUI 官方模块,随 ComfyUI 升级自动维护
  • 更好兼容性:支持未来可能出现的更多复杂运行环境
  • 代码质量:遵循 ComfyUI 最佳实践,提高代码可维护性

测试验证

  • 基础功能和导出代码执行测试通过
  • 向后兼容,无需用户调整

简单但前瞻性的改动,既解决当前问题,又为未来扩展打好基础。


Optimize Path Handling to Improve Code Compatibility and Maintainability

Background

Issue discovered when using ComfyUI-to-Python-Extension to export workflows as Python code. The original code calculates extensions path via __main__, which depends on execution file location. This causes path resolution errors when exported Python files are run from different locations.

Changes

  • Replace custom path calculation with ComfyUI's official folder_paths module
  • Remove __main__ dependency, use folder_paths.base_path for root path resolution
  • Optimize directory creation with os.makedirs for more reliable handling

Benefits

  • Immediate Fix: Resolve execution issues with ComfyUI-to-Python-Extension exported code
  • Long-term Maintainability: Rely on ComfyUI's official module that updates with ComfyUI versions
  • Better Compatibility: Support for future complex runtime environments
  • Code Quality: Follow ComfyUI best practices for better maintainability

Testing

  • Basic functionality and exported code execution verified
  • Backward compatible, no user adjustments needed

A simple yet forward-looking change that not only solves current issues but also lays groundwork for future extensibility.

@chflame163 chflame163 merged commit e5e0dec into chflame163:main Oct 29, 2024
@chflame163
Copy link
Owner

Thanks for your work

siyonomicon pushed a commit to siyonomicon/ComfyUI_LayerStyle that referenced this pull request Feb 15, 2026
Optimize Path Handling to Improve Code Compatibility and Maintainability
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