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

执行poetry run test报错:No file/folder found for package myproject

问题描述

我的项目结构如下:

myproject on  main [$!?] is 📦 v1.0.0 via  v18.14.0 via 🐍 v3.10.9 
❯ tree -L 1
.
├── build
├── deploy
├── Dockerfile
├── poetry.lock
├── pyproject.toml
├── README.md
├── scripts.py
└── src

pyproject.toml内容:

[tool.poetry]
name = "myproject"
version = "0.1.0"
description = ""
authors = [""]

[tool.poetry.scripts]
test = "scripts:test"

scripts.py内容:

import subprocess

def test():
    """
    Run all unittests.
    
    """
    subprocess.run(
        ['python', '-u', '-m', 'pytest']
    )
if __name__ == '__main__':
    test()

执行poetry run test时,报错:

No file/folder found for package myproject
解决方案

这个错误源于Poetry无法识别对应myproject的包结构,可通过以下步骤修复:

  • 规范src目录结构
    在src目录下创建与pyproject.toml中name一致的子目录myproject,并添加__init__.py文件,结构如下:

    src/
    └── myproject/
        ├── __init__.py
        # 其他项目代码文件
    

    这是Poetry识别Python包的基础结构要求。

  • 补充pyproject.toml的packages配置
    在[tool.poetry]区块下添加包路径配置,明确告诉Poetry从src目录加载myproject包:

    [tool.poetry]
    name = "myproject"
    version = "0.1.0"
    description = ""
    authors = [""]
    packages = [{ include = "myproject", from = "src" }]
    
  • 重新安装项目到Poetry环境
    完成上述调整后,执行以下命令让Poetry更新环境配置:

    poetry install
    

    之后再运行poetry run test即可正常执行。

如果只是临时需要运行测试脚本,也可以直接执行poetry run python scripts.py跳过包结构验证,但从项目长期规范角度,建议优先整理好包结构并配置正确的Poetry参数。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 17:10:26