【发布时间】:2023-04-06 09:28:01
【问题描述】:
我有一个使用诗歌和毒物的 python 项目。它有源代码、测试和脚本(juptext 笔记本)。我无法在脚本中导入开发依赖项,但我可以在测试中导入。
当我遇到这个问题时,我创建了以下最小示例。起初,它不起作用,然后我摆弄它,现在它起作用了。所以我剥离了有实际问题的项目,所以除了项目名称、位置、虚拟环境和 .git 目录之外,它无法区分,但这仍然不起作用。
更新删除所有构建工件和最小示例的 virtualenv 使其再次停止工作
UPDATE 将scripts: poetry install 行添加到 tox 命令修复仅最小示例
源代码、测试和脚本如下布局
foo
+--foo
| +--__init__.py
|
+--tests
| +--__init__.py
| +--test_foo.py
|
+--scripts
| +--foo_script.py
|
+--pyproject.toml
+--tox.ini
文件要么是空的,要么如下:
foo_script.py
import requests
test_foo.py
import requests
import pytest
def test():
assert True
pyproject.toml
[tool.poetry]
name = "foo"
version = "0.1.0"
description = ""
authors = ["foo maker"]
[tool.poetry.dependencies]
python = "^3.7"
requests = "*"
[tool.poetry.dev-dependencies]
pytest = "^4.6"
[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"
tox.ini
[tox]
envlist = test, scripts
isolated_build = true
skipsdist = true
[testenv]
basepython = python3.7
whitelist_externals =
pytest
bash
commands =
test: pytest
scripts: bash -c 'python3 scripts/*.py'
当我运行 tox 时,我得到了
test run-test-pre: PYTHONHASHSEED='4126239415'
test run-test: commands[0] | pytest
============================= test session starts ==============================
platform linux -- Python 3.6.9, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
cachedir: .tox/test/.pytest_cache
rootdir: /home/#######/foo
collected 1 item
tests/test_foo.py . [100%]
============================== 1 passed in 0.09s ===============================
scripts run-test-pre: PYTHONHASHSEED='4126239415'
scripts run-test: commands[0] | bash -c 'python3 scripts/*.py'
Traceback (most recent call last):
File "scripts/foo_script.py", line 1, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
ERROR: InvocationError for command /bin/bash -c 'python3 scripts/*.py' (exited with code 1)
___________________________________ summary ____________________________________
test: commands succeeded
ERROR: scripts: commands failed
【问题讨论】:
-
您是否在全球范围内安装了
pytest?名为test的tox testenv 应该调用命令pytest,但据我所知,它没有在任何地方声明为依赖项。所以这个 testenv 应该会失败。 -
@sinoroc 显然是这样。没有意识到这一点
-
据我所知,tox 无法识别 poetry 的 dev-dependencies(我是不知道为什么诗歌在这里发明了自己的东西)。您可能想要遵循的更常见的模式是使用 test extra 来确保将 pytest 等测试依赖项安装在_tox 环境,就像在这个答案中解释的那样:stackoverflow.com/a/59522588/11138259
标签: python tox python-poetry