You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让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可执行文件:

  1. 安装插件到Poetry环境:
    poetry self add poetry-plugin-build-exe
    
  2. 在项目的pyproject.toml中添加插件配置,指定你的入口点:
    [tool.poetry.plugins.build-exe]
    entry-points = ["locust = locust.main:main"]
    
  3. 重新执行安装命令:
    poetry install --with dev
    
    安装完成后,Scripts目录下会生成对应的.exe文件,此时直接使用subprocess.Popen(["locust", "..."])即可正常调用。

方法2:结合pip生成exe包装器

先通过Poetry导出依赖,再用pip的参数生成exe:

  1. 导出项目依赖到requirements文件:
    poetry export -f requirements.txt --output requirements.txt --with dev
    
  2. 使用pip安装并指定生成exe包装器:
    pip install -r requirements.txt --install-option="--install-scripts=Scripts" --install-option="--executable=python.exe"
    

内容的提问来源于stack exchange,提问作者Cyberwiz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 13:12:42