PyMeshGen 是一款开源的 Python 非结构网格生成工具,专注于为 CFD/FEA 提供易用的二维网格生成解决方案。本文将详细介绍 PyMeshGen 的多种打包方式和安装方法,帮助用户快速部署和使用。

PyMeshGen 自开源以来,已收到37Star和15fork,受到了从业者和算法研究者的关注。作为一款基于 Python 的网格生成工具,PyMeshGen V1.1.1提供了多种安装和打包方式,以满足不同用户的需求:
本文将逐一介绍这些打包和安装方法的原理、步骤和最佳实践。

PyPI(Python Package Index)是 Python 官方的软件包仓库。通过 PyPI 安装 PyMeshGen 具有以下优势:
pip install --upgrade 获取最新版本
pip install pymeshgen这条命令会安装 PyMeshGen 的核心功能,包括:
.cas 格式导入PyMeshGen 提供了两个可选的 extra 包,用于支持高级功能:
机器学习/神经网络平滑:
pip install "pymeshgen[ml]"安装内容包括:
torch:PyTorch 深度学习框架torch-geometric:图神经网络库stable-baselines3:强化学习框架trimesh:三维网格处理库几何导入导出(OpenCASCADE):
pip install "pymeshgen[occ]"安装内容包括:
pythonocc-core:OpenCASCADE 的 Python 绑定,支持 STEP、IGES 等 CAD 格式完整安装:
pip install "pymeshgen[full]"一次性安装所有可选依赖。
安装完成后,可以通过以下命令验证:
# 检查版本pip show pymeshgen# 命令行入口pymeshgen --version# GUI 入口pymeshgen-gui命令行模式:
pymeshgen --case".\config\30p30n.json"GUI 模式:
pymeshgen-guiPython 库调用:
from data_structure.parameters import Parametersfrom PyMeshGen import PyMeshGenparams = Parameters("FROM_CASE_JSON", r".\config\30p30n.json")mesh = PyMeshGen(params)git clone https://github.com/cfd-dev/PyMeshGen.gitcd PyMeshGenPyMeshGen 依赖 meshio 作为子模块:
git submodule update --init --recursive核心依赖:
pip install -r requirements.txt可选依赖:
pip install -r requirements-optional.txtpip install -e .-e 参数表示"editable"模式,修改源码后无需重新安装即可生效,非常适合开发调试。
cd unittestspython run_tests.pypython run_quick_tests.pyPyMeshGen 采用现代化的 pyproject.toml 构建系统,基于 setuptools 后端:
[build-system]requires = ["setuptools>=77", "wheel"]build-backend = "setuptools.build_meta"构建配置要点:
requires-python | >=3.8 |
license | GPL-2.0-or-later |
dynamic | |
packages.find |
# 安装构建工具python -m pip install --upgrade build twine# 构建源码包和 wheelpython -m build# 检查构建产物python -m twine check dist/*构建产物:
dist/pymeshgen-<version>.tar.gz | ||
dist/pymeshgen-<version>-py3-none-any.whl |
PyMeshGen 的版本号来源于项目根目录的 VERSION 文件:
1.1.1发布新版本前,需要更新此文件:
# 更新版本号echo"1.2.0" > VERSION# 提交更改git add VERSIONgit commit -m "Bump version to 1.2.0"pyproject.toml 中定义了三个入口点:
[project.scripts]pymeshgen = "PyMeshGen:PyMeshGen"pymeshgen-mixed = "PyMeshGen:PyMeshGen_mixed"[project.gui-scripts]pymeshgen-gui = "start_gui:main"pymeshgen:命令行网格生成入口pymeshgen-mixed:混合网格生成入口pymeshgen-gui:GUI 启动入口(Windows 下不显示控制台窗口)包含的包:
[tool.setuptools.packages.find]include = [ "adfront2*", "data_structure*", "delaunay*", "fileIO*", "gui*", "meshsize*", "optimize*", "utils*", "visualization*",]排除的目录:
exclude = [ "unittests*", "backup*", "build*", "dist*", "examples*", "temp_docs*",]包数据文件:
[tool.setuptools.package-data]"*" = ["*.txt", "*.md", "*.json", "*.cas"]确保配置文件、文档和示例网格文件都被包含在分发包中。
PyMeshGen 配置了两条 GitHub Actions 工作流:
持续集成(python-package.yml):
发布到 PyPI(publish-pypi.yml):
步骤 1:更新版本号
echo"1.1.2" > VERSIONgit add VERSIONgit commit -m "Bump version to 1.1.2"步骤 2:创建 Git 标签
git tag v1.1.2git push origin master --tags步骤 3:创建 GitHub Release
在 GitHub 仓库页面创建 Release,关联标签 v1.1.2。
步骤 4:自动发布
GitHub Actions 自动执行:
对于测试目的,可以手动触发工作流:
Run workflowrepository = testpypiPyPI 发布使用 Trusted Publisher(OIDC)机制,无需手动配置 API token:
cfd-dev | |
PyMeshGen | |
publish-pypi.yml | |
testpypipypi |
虽然 PyPI 是主要的分发方式,但 PyMeshGen 也提供了 Windows 可执行文件打包方案,适用于:
Windows 打包依赖以下工具:
打包脚本位于 packaging/windows/ 目录:
packaging/windows/├── build_app.py # 主打包脚本├── build.bat # Windows 批处理入口├── PyMeshGen.spec # PyInstaller 配置└── PyMeshGen.iss # Inno Setup 安装包配置方式一:使用 Python 脚本
# 仅打包可执行文件python packaging\windows\build_app.py# 完整打包(exe + ZIP + 安装包)python packaging\windows\build_app.py --all方式二:使用批处理
packaging\windows\build.batPyMeshGen.exe | dist/ | |
PyMeshGen-v*.zip | releases/ | |
PyMeshGen-Setup-*.exe | installer/ |
PyMeshGen.spec 文件的关键配置:
# 隐藏导入hiddenimports=['PyQt5','vtk','numpy',# ... 其他依赖]# 数据文件datas=[ ('config/', 'config/'), ('VERSION', '.'),# ... 其他资源]PyMeshGen.iss 定义了安装包的:
PyMeshGen 的核心依赖包括:
numpy | |
scipy | |
PyQt5 | |
vtk | |
meshio |
pythonocc-core | pip install pymeshgen[occ] | |
torch | pip install pymeshgen[ml] | |
torch-geometric | pip install pymeshgen[ml] | |
stable-baselines3 | pip install pymeshgen[ml] | |
trimesh | pip install pymeshgen[ml] |
pythonocc-core 在某些平台上通过 pip 安装可能遇到困难,推荐使用 conda:
conda install -c conda-forge pythonocc-core问题 1:pip 安装速度慢
# 使用国内镜像源pip install pymeshgen -i https://pypi.tuna.tsinghua.edu.cn/simple问题 2:依赖冲突
# 创建虚拟环境python -m venv pymeshgen_envpymeshgen_env\Scripts\activate # Windowssource pymeshgen_env/bin/activate # Linux/macOSpip install pymeshgen问题 3:GUI 无法启动
# 检查 PyQt5 是否正确安装pip show PyQt5# 重新安装pip install --force-reinstall PyQt5问题 4:VTK 渲染问题
# 检查 VTK 版本pip show vtk# 确保 VTK 版本与 PyQt5 兼容pip install vtk==9.2.6问题 5:构建失败
# 确保子模块已初始化git submodule update --init --recursive# 清理后重新构建rm -rf dist/ build/ *.egg-infopython -m buildPyMeshGen 提供了完善的打包和安装体系:
👇点击左下方“阅读原文”访问项目!
全文结束,感谢观看,创作不易。
😁😁
欢迎关注、留言、点赞、分享、推荐!
👇👇
【往期回顾】