簡體   English   中英

使用 tox 運行腳本時詩歌依賴項不可用

[英]poetry dependencies not available when running script with tox

我有一個使用詩歌和毒葯的 python 項目。 它有源代碼、測試和腳本(juptext notebooks)。 我無法在腳本中導入開發依賴項,但可以在測試中導入。

當我遇到這個問題時,我創建了以下最小示例。 起初,它不起作用,然后我擺弄了它,現在它起作用了。 所以我刪除了有實際問題的項目,所以除了項目名稱、位置、虛擬環境和 .git 目錄之外,它是無法區分的,但這仍然不起作用。

UPDATE刪除所有構建工件和最小示例的 virtualenv 使其再次停止工作

更新添加行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]
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

我相信以下內容應該有效:

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'

假設您已經在 pyproject.yml 中安裝了詩歌和 tox 和 pytest 依賴項(注意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

在這種情況下,如果您在執行 tox 的目錄 foo 中,整個文件將如下所示:

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

[testenv]
whitelist_externals = poetry
commands=
    poetry run pytest
changedir = tests

首先,我需要使用poetry install來安裝依賴項。 然后在命令的開頭追加poetry run以啟用依賴項。 同樣運行這樣的python腳本只會運行第一個,將其他人的名稱作為參數傳遞給第一個程序。 而是for f in scripts/*.py; do python "$f"; done使用for f in scripts/*.py; do python "$f"; done for f in scripts/*.py; do python "$f"; done for f in scripts/*.py; do python "$f"; done (見這里

全部一起

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM