gpt4 book ai didi

python - 使用 tox 运行脚本时诗歌依赖项不可用

转载 作者:行者123 更新时间:2023-12-02 03:14:20 25 4
gpt4 key购买 nike

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

当我遇到这个问题时,我创建了以下最小示例。一开始不行,后来我就摆弄了一下,现在可以了。因此,我删除了存在实际问题的项目,这样除了项目名称、位置、虚拟环境和 .git 目录之外,它是无法区分的,但这仍然不起作用。

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

更新将行scripts:诗安装添加到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"

毒物.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

最佳答案

我相信类似以下的内容应该有效:

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'

关于python - 使用 tox 运行脚本时诗歌依赖项不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60008018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com