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

Setuptools与pyproject.toml无法识别Python包位置问题排查

Setuptools打包后无法运行命令行工具问题

目录结构

project_root
├── LICENSE.md
├── README.md
├── VulcanSportsCLIenv
│   └── pyvenv.cfg
├── pyproject.toml
├── requirements.txt
├── src
│   └── VulcanSports
│       ├── VulcanSports_CLI.py
│       ├── __init__.py
│       └── __main__.py
└── tests
    └── __init__.py

pyproject.toml配置内容

#pyproject.toml

[build-system]
requires = ["setuptools>=67.0", "setuptools-scm"]
build-backend = "setuptools.build_meta"

[project]
name = "VulcanSports"
version = "0.0.1"
authors = [
    {name = "Abe Mankavil", email = "abe.m.mankavil@gmail.com"},
]
description = "Command line interface for quick access to sports scores and odds"
readme = "README.md"
requires-python = ">=3.8"
classifiers = [
    "Development Status :: 1 - Planning",
    "Programming Language :: Python :: 3",
    "License :: OSI Approved :: GNU General Public License v3 (GPLv3)"
]
dependencies = [
    "requests"
]
keywords = ['Vulcan','Sports']

[tool.setuptools.packages.find]
#include = ["VulcanSports/*.py"]  
exclude = ["/VulcanSportsCLIenv"]
namespaces = false

问题描述

在项目根目录执行python -m pip install .后构建成功,包在pip list中可见,但在目录外执行python VulcanSports时出现错误:

/Users/abemankavil/Desktop/VulcanSportsProject/VulcanSports-CLI/VulcanSportsCLIenv/bin/python: can't open file '/Users/abemankavil/Desktop/VulcanSportsProject/VulcanSports-CLI/VulcanSports': [Errno 2] No such file or directory

问题原因与解决办法

核心问题

你用python VulcanSports的运行方式本身就不对,而且pyproject.toml里没配置命令行入口点,导致包安装后系统不知道怎么直接通过名称调用你的CLI工具。

具体分析

  1. python VulcanSports是用来执行当前目录下名为VulcanSports的脚本文件,但你的包安装后代码存在Python的site-packages目录里,不是当前目录,所以会提示找不到文件。
  2. 你的包结构里有__main__.py,可以通过python -m VulcanSports运行,但这不是最便捷的方式。要直接用VulcanSports命令启动,必须在pyproject.toml中配置入口点。

修复步骤

方法1:通过模块方式临时运行

直接执行:

python -m VulcanSports

这会调用包中的__main__.py,前提是该文件里有正确的执行逻辑。

方法2:配置命令行入口点(推荐)

修改pyproject.toml,在[project]部分添加脚本入口配置,比如:

[project.scripts]
VulcanSports = "VulcanSports.VulcanSports_CLI:main"

这里假设VulcanSports_CLI.py中有一个名为main的函数作为CLI的启动入口。

修改完成后重新安装包:

pip install . --upgrade

之后就能直接在终端输入VulcanSports命令启动你的工具了。

额外说明

你的目录结构符合src-layout规范,没有问题;[tool.setuptools.packages.find]的配置也能正确识别src下的VulcanSports包,所以目录结构和基础打包配置是正常的,问题出在缺少命令行入口配置和错误的运行方式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 21:07:13