如何让Poetry生成.bat/.exe文件以适配Windows单元测试?
解决Poetry在Windows下生成可直接调用的可执行文件问题
跨平台临时解决方案(无需修改Poetry配置)
直接通过Python解释器调用目标模块,这种方式完全避开shell依赖,Linux和Windows通用:
import sys import subprocess # 直接调用locust模块,替代包装脚本 subprocess.Popen([sys.executable, "-m", "locust", "..."])
让Poetry生成.exe包装文件的方法
方法1:使用Poetry第三方插件
借助poetry-plugin-build-exe插件生成Windows可执行文件:
- 安装插件到Poetry环境:
poetry self add poetry-plugin-build-exe - 在项目的
pyproject.toml中添加插件配置,指定你的入口点:[tool.poetry.plugins.build-exe] entry-points = ["locust = locust.main:main"] - 重新执行安装命令:
安装完成后,Scripts目录下会生成对应的poetry install --with dev.exe文件,此时直接使用subprocess.Popen(["locust", "..."])即可正常调用。
方法2:结合pip生成exe包装器
先通过Poetry导出依赖,再用pip的参数生成exe:
- 导出项目依赖到requirements文件:
poetry export -f requirements.txt --output requirements.txt --with dev - 使用pip安装并指定生成exe包装器:
pip install -r requirements.txt --install-option="--install-scripts=Scripts" --install-option="--executable=python.exe"
内容的提问来源于stack exchange,提问作者Cyberwiz
相关产品推荐
相关产品推荐

