已发布至PyPI的tud-sumo包无法导入问题求助
问题:PyPI包安装后无法导入(ModuleNotFoundError)
问题描述
- 已成功将
tud-sumo发布至PyPI,pip install无报错,pip list能显示该包,但在其他文件中导入时始终抛出ModuleNotFoundError - 其他Python包正常使用,两台电脑测试均出现相同问题,推测是包配置存在错误
项目文件结构
tud-sumo/ __init__.py simulation.py controllers.py events.py plot.py scenarios.py utils.py LICENSE README.md pyproject.toml setup.py
当前配置文件内容
setup.py
from setuptools import setup setup()
pyproject.toml
[build-system] requires = ["setuptools"] build-backend = "setuptools.build_meta" [project] name = "tud-sumo" version = "3.0.3" description = "TU Delft SUMO wrapper" readme = "README.md" authors = [{ name = "Callum Evans", email = "c.evans@tudelft.nl" }] license = { file = "LICENSE" } dependencies = [ "traci", "numpy", "matplotlib", "mpl-tools", "tqdm", "shapely", ] keywords = ["traffic simulator", "microscopic", "SUMO"] classifiers=[ "Development Status :: 4 - Beta", "Intended Audience :: Science/Research", "Topic :: Scientific/Engineering", "License :: OSI Approved :: Apache Software License", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12", "Programming Language :: Python :: 3.13", "Programming Language :: Python :: 3.14" ] [project.urls] Documentation = "https://tud-sumo.github.io/docs/" Repository = "https://github.com/tud-sumo/tud-sumo"
发布方式
通过python3 setup.py sdist生成源码包,twine upload dist/*上传至PyPI;已尝试卸载重装、清除pip缓存,问题未解决
解决方案
核心问题是未告知setuptools需要打包的模块目录,导致安装后的包仅包含配置文件,缺失实际源码文件。
修复步骤
修改
pyproject.toml,添加包的包含配置:
在[project]区块下新增:packages = ["tud-sumo"]或使用自动发现模式(更推荐,适用于复杂项目),在文件末尾新增:
[tool.setuptools] packages = find:重新构建并上传:
- 清理旧构建产物:
rm -rf dist/ build/ *.egg-info - 使用标准工具构建:
python -m build(替代直接调用setup.py,符合PEP 517规范) - 上传新版本:
twine upload dist/*
- 清理旧构建产物:
验证导入:
- 卸载旧版本:
pip uninstall -y tud-sumo - 安装新版本:
pip install tud-sumo - 测试导入:
python -c "import tud_sumo"(注意:Python模块名不支持连字符,包名tud-sumo在导入时需替换为tud_sumo,setuptools会自动处理映射)
- 卸载旧版本:
内容的提问来源于stack exchange,提问作者littlechef
相关产品推荐
相关产品推荐

