构建httomolib的conda recipe时遇ModuleNotFoundError错误求助
问题
我有一个名为httomolib的Python包,正尝试将其发布到Anaconda Cloud。我为它编写的conda recipe中,meta.yml文件内容如下:
{% set name = "httomolib" %} {% set version = "0.0.1" %} package: name: {{ name|lower }} version: {{ version }} source: path: .. build: preserve_egg_dir: False script: {{ PYTHON }} -m pip install -e . number: 0 requirements: host: - python - pip - setuptools run: - python - cupy - numpy - scipy - pillow - scikit-image - pytest test: requires: - pip - pytest imports: - httomolib - httomolib.misc - httomolib.prep - httomolib.recon source_files: - tests/* commands: - pip check - pytest about: home: https://github.com/DiamondLightSource/httomolib summary: 'A library of data processing and reconstruction methods for tomography' description: | A library of data processing and reconstruction methods for tomography
当执行conda build recipe --numpy 1.21 --python 3.9指定Python 3.9、NumPy 1.21版本构建并测试该recipe时,出现如下导入错误:
ModuleNotFoundError: No module named 'httomolib'
请问该recipe中缺少了什么内容?是否与Python或setuptools相关?(注:recipe是包含meta.yml的文件夹)
解决方案
核心问题出在build阶段的安装方式,以及可能缺少必要的包配置文件,具体修复点如下:
1. 替换开发模式安装命令
当前build脚本使用pip install -e .(开发模式),这种方式仅在Python的site-packages目录创建源码软链接,而conda build的隔离环境在测试阶段无法正确识别该链接,导致模块找不到。
将build脚本修改为标准pip安装命令:
build: preserve_egg_dir: False script: {{ PYTHON }} -m pip install . --no-deps -vv number: 0
--no-deps:禁止pip自动安装依赖,由conda统一管理依赖版本,避免版本冲突;-vv:开启详细日志,便于排查后续安装问题。
2. 确保项目根目录有正确的包配置文件
conda通过pip安装包时,依赖项目根目录的setup.py或pyproject.toml识别包结构,需确认:
- 若使用
setup.py,必须包含packages=setuptools.find_packages(),确保所有子模块(httomolib.misc等)被正确识别; - 若使用
pyproject.toml,需通过[project]字段正确声明包名、版本及packages条目。
3. 可选:添加noarch标识(纯Python包)
如果你的包是纯Python实现,无平台相关编译代码,可在build部分添加noarch: python,让构建的包支持跨平台使用:
build: preserve_egg_dir: False script: {{ PYTHON }} -m pip install . --no-deps -vv number: 0 noarch: python
内容的提问来源于stack exchange,提问作者namannimmo
相关产品推荐
相关产品推荐

