使用tox运行脚本时,诗歌依存关系不可用

时间:2020-01-31 16:45:51

标签: python tox python-poetry

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

遇到此问题时,我创建了以下最小示例。最初,它没有用,然后我在弄弄它,现在它正在用。因此,我剥离了存在实际问题的项目,因此除了项目名称,位置,虚拟环境和.git目录之外,其他项目都无法区分,但是仍然无法正常工作。

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

更新将行mat3(inverse(transpose(modelview)))添加到固定的tox命令中,仅 最小示例

源代码,测试和脚本采用以下布局

scripts: poetry install

文件为空或如下:

foo_script.py

foo
  +--foo
  |  +--__init__.py
  |
  +--tests
  |  +--__init__.py
  |  +--test_foo.py
  |
  +--scripts
  |  +--foo_script.py
  |
  +--pyproject.toml
  +--tox.ini

test_foo.py

import requests

pyproject.toml

import requests
import pytest

def test():
    assert True

tox.ini

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

3 个答案:

答案 0 :(得分: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'

答案 1 :(得分: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

答案 2 :(得分:-1)

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

在一起

poetry install
poetry run bash -c 'for f in scripts/*.py; do python "$f"; done'

花费的时间比我想的要长