3

我有一个使用诗歌和毒物的 python 项目。它有源代码、测试和脚本(juptext 笔记本)。我无法在脚本中导入开发依赖项,但可以在测试中导入。

当我遇到这个问题时,我创建了以下最小示例。起初,它不起作用,然后我摆弄它,现在它起作用了。所以我剥离了有实际问题的项目,所以除了项目名称、位置、虚拟环境和 .git 目录之外,它无法区分,但这仍然不起作用。

更新删除所有构建工件和最小示例的 virtualenv 使其再次停止工作

更新将行添加到 tox 命令scripts: poetry install修复了最小示例

源代码、测试和脚本在以下布局中

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]
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'

当我运行毒药时,我得到

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
4

3 回答 3

0

假设您已经安装了诗歌,并且 tox 和 pytest 是 pyproject.yml 中的依赖项(注意poetry run,请参阅https://python-poetry.org/docs/cli/#run):

[tox]
envlist = py37
isolated_build = True
skipsdist = True

[testenv]
whitelist_externals = poetry
commands=
    poetry run pytest

可选地,您可以通过将最后一位更改为来在运行测试时进行安装(但是您需要在诗歌之外安装 tox,这可能会导致您出现问题)

commands=
    poetry install
    poetry run pytest

此外,根据您的根文件夹和测试所在的位置,您可以配置 tox 的路径以通过添加更改目录

changedir = tests

在这种情况下,如果您在目录 foo 中执行 tox,则整个文件将如下所示:

[tox]
envlist = py37
isolated_build = True
skipsdist = True

[testenv]
whitelist_externals = poetry
commands=
    poetry run pytest
changedir = tests
于 2020-02-10T16:59:58.000 回答
0

我相信类似以下的东西应该有效:

pyproject.toml

[tool.poetry]
name = "foo"
version = "0.1.0"
description = ""
authors = ["foo maker"]

[tool.poetry.dependencies]
python = "^3.7"
requests = "*"
#
pytest = { version = "^4.6", optional = true }

[tool.poetry.extras]
test = ["pytest"]

# [tool.poetry.dev-dependencies]
# use 'test' extra instead

[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"

tox.ini

[tox]
envlist = test, scripts
isolated_build = true

[testenv]
basepython = python3.7
whitelist_externals =
    pytest
    bash
extras =
    test
commands =
    test: pytest
    scripts: bash -c 'for f in scripts/*.py; do python "$f"; done'
于 2020-02-04T10:56:09.737 回答
-1

首先,我需要使用poetry install. 然后附加poetry run到命令的开头以启用依赖项。同样运行这样的python脚本只会运行第一个,将其他的名称作为参数传递给第一个程序。而是使用for f in scripts/*.py; do python "$f"; done(见这里

全部一起

poetry install
poetry run bash -c 'for f in scripts/*.py; do python "$f"; done'
于 2020-01-31T21:21:19.893 回答