Python构建模块时出现ModuleNotFoundError

编程语言 2026-07-12

我是PyPI上 WBGAPI包的维护作者。我一直在使用setuptools来构建并分发这个包。好久没发更新了。上一次直接运行我的 setup.py 包,也就是 python setup.py setup.py sdist bdist_wheel,但这现在已被弃用,构建也无法正常工作。所以我想更新我的流程,仍然希望使用setuptools。

然而,当我尝试构建时,遇到了一个奇怪的ModuleNotFoundError:

ModuleNotFoundError: No module named 'wbgapi'

这是我正在尝试构建的模块。

除了新增的 pyproject.toml 文件外,我的项目和源代码目录结构没有太大变化;现在看起来是这样的:

dist/
pyproject.toml
setup.py
wbgapi/
  __init__.py
  __pycache__
  __version__.py
  data.py
  economy_coder.py
  economy_metadata.py
  economy.py
  income.py
  lending.py
  lookup-data.yaml
  region.py
  series_metadata.py
  series.py
  source.py
  time.py
  topic.py
  utils.py

pyproject.toml:

[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

setup.py(几年前曾经成功运行过):

import setuptools
import sys

from wbgapi.__version__ import __version__ as pkgVersion

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="wbgapi",
    version=pkgVersion,
    author="**REDACTED**",
    author_email="**REDACTED**",
    description="wbgapi provides a comprehensive interface to the World Bank's data and metadata APIs",
    license='MIT',
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/tgherzog/wbgapi",
    packages=setuptools.find_packages(),
    include_package_data=True,
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
        "Development Status :: 5 - Production/Stable",
    ],
    install_requires=['requests', 'PyYAML', 'tabulate'],
    python_requires='>=3.0',
)

而且当我构建时,我得到:

>>> python3 -m build
* Creating isolated environment: venv+pip...
* Installing packages in isolated environment:
  - setuptools
* Getting build dependencies for sdist...
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/pyproject_hooks/_in_process/_in_process.py", line 389, in <module>
    main()
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/pyproject_hooks/_in_process/_in_process.py", line 373, in main
    json_out["return_val"] = hook(**hook_input["kwargs"])
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/pyproject_hooks/_in_process/_in_process.py", line 317, in get_requires_for_build_sdist
    return hook(config_settings)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/private/var/folders/s3/30jxqzz1463c1df3w58z77y00000gn/T/build-env-jvvxqp77/lib/python3.12/site-packages/setuptools/build_meta.py", line 338, in get_requires_for_build_sdist
    return self._get_build_requires(config_settings, requirements=[])
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/private/var/folders/s3/30jxqzz1463c1df3w58z77y00000gn/T/build-env-jvvxqp77/lib/python3.12/site-packages/setuptools/build_meta.py", line 301, in _get_build_requires
    self.run_setup()
  File "/private/var/folders/s3/30jxqzz1463c1df3w58z77y00000gn/T/build-env-jvvxqp77/lib/python3.12/site-packages/setuptools/build_meta.py", line 317, in run_setup
    exec(code, locals())
  File "<string>", line 5, in <module>
ModuleNotFoundError: No module named 'wbgapi'

ERROR Backend subprocess exited when trying to invoke get_requires_for_build_sdist

我需要传递哪些配置选项来构建?

解决方案

我拿了你提供的setup代码,在导入版本号之前添加了以下内容,结果就起作用了:

import os.path
sys.path.append(os.path.abspath('.'))

abspath 的作用是,如果某个构建步骤改变了当前工作目录,路径就不会改变)

显然,build 系统正在把路径重置为一个不包含项目目录的默认值。

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章