使用Nox+Flit打包Python时ofjustpy_engine依赖缺失问题
问题解决:Nox调用Flit时
ModuleNotFoundError: No module named 'ofjustpy_engine' 核心问题分析
报错出现在Flit生成可编辑包元数据阶段:当Flit尝试导入你的ofjustpy/__init__.py时,发现依赖的ofjustpy_engine模块不存在。原因有两点:
pyproject.toml中存在拼写错误,导致Flit定位模块异常- Pip构建可编辑包时默认使用隔离环境,该环境未正确加载
ofjustpy_engine依赖
步骤1:修正pyproject.toml配置
首先修复tool.flit.metadata下的模块名称拼写错误,同时确保构建依赖声明正确:
[build-system] requires = ["flit_core >=3,<4", "ofjustpy-engine"] build-backend = "flit_core.buildapi" [tool.flit.metadata] name = "ofjustpy" module = "ofjustpy" # 修正原拼写错误"ofjuspty" version = "1.0.0" author = "Kabira K." author-email = "kabira@monallabs.in" description = "Full stack webdev framework in pure Python" [tool.flit.metadata.dependencies] python = ">=3.6" ofjustpy_engine = "*" # 其余配置保持不变
步骤2:重构Nox会话脚本
去掉多余的pipenv关联步骤(Nox自带独立虚拟环境,无需依赖外部环境),并通过--no-build-isolation参数让Pip直接使用当前虚拟环境的依赖:
import nox import tempfile @nox.session(python="3.11") def badges(session): # 直接安装所有所需工具与依赖 session.install( "pytest", "pytest-cov", "flake8", "flake8-html", "ofjustpy-engine", "genbadge[all]", "flit_core>=3,<4" ) # 禁用构建隔离,使用当前环境依赖安装可编辑包 session.run("pip", "install", "-e", ".", "--no-build-isolation") # 生成测试报告 session.run( "pytest", "--cov", "--junitxml=reports/junit/junit.xml", "--cov-report=xml:reports/coverage/coverage.xml", "--cov-report=html:reports/coverage/coverage.html", ) # 修正flake8检查路径为你的实际包路径 session.run( "flake8", "src/ofjustpy", "--exit-zero", "--format=pylint", "--statistics", "--output-file=reports/flake8/flake8stats.txt", ) # 生成各类徽章 session.run("genbadge", "tests", "-i", "reports/junit/junit.xml", "-o", "badge_tests.svg") session.run("genbadge", "coverage", "-i", "reports/coverage/coverage.xml", "-o", "badge_coverage.svg") session.run("genbadge", "flake8", "-i", "reports/flake8/flake8stats.txt", "-o", "badge_flake8.svg")
关键修改说明
- 禁用构建隔离:
--no-build-isolation参数让Pip跳过默认的隔离构建环境,直接使用Nox虚拟环境中已安装的ofjustpy_engine,避免Flit生成元数据时找不到依赖 - 移除pipenv步骤:Nox会自动创建独立虚拟环境,无需导入外部pipenv的依赖列表,避免环境冲突
- 修正路径与拼写:修复了Flit模块名称和flake8检查路径的错误,确保工具能正确定位你的代码
内容的提问来源于stack exchange,提问作者Kabira K
相关产品推荐
相关产品推荐

