首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在诗歌中使用nox?

如何在诗歌中使用nox?
EN

Stack Overflow用户
提问于 2020-01-16 19:24:42
回答 3查看 1.7K关注 0票数 6

我想在由poetry管理的项目中使用nox

不顺利的是在nox会话中安装dev依赖项。

我有如下所示的noxfile.py

代码语言:javascript
复制
import nox
from nox.sessions import Session
from pathlib import Path

__dir__ = Path(__file__).parent.absolute()


@nox.session(python=PYTHON)
def test(session: Session):
    session.install(str(__dir__))  # I want to use dev dependency here
    session.run("pytest")

如何在nox会话中安装dev依赖?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-01-25 06:02:00

目前,session.install还不支持poetryinstall,只支持shell中的runs pip。您可以使用更通用的方法session.run来激活poetry

示例:

代码语言:javascript
复制
@nox.session(python=False)
def tests(session):
    session.run('poetry', 'shell')
    session.run('poetry', 'install')
    session.run('pytest')

当您设置会话时,您可以通过自己禁用python virtualenv (python=False)的创建并使用poetry shell激活poetry的会话来完成所有操作。

票数 2
EN

Stack Overflow用户

发布于 2020-06-18 23:32:36

在经历了一些尝试和错误之后,与我在@Yann的答案中所评论的相反,poetry似乎忽略了nox传递的VIRTUAL_ENV变量。

受Claudio Jolowicz的精彩系列Hypermodern Python的启发,我用以下方法解决了这个问题:

代码语言:javascript
复制
@nox.session(python=PYTHON)
def test(session: Session) -> None:
    """
    Run unit tests.

    Arguments:
        session: The Session object.
    """
    args = session.posargs or ["--cov"]
    session.install(".")
    install_with_constraints(
        session,
        "coverage[toml]",
        "pytest",
        "pytest-cov",
        "pytest-mock",
        "pytest-flask",
    )
    session.run("pytest", *args)

在这里,我只是使用pip安装一个PEP517包。

不幸的是,通过pip安装的PEP517不支持可编辑("-e")开关。

仅供参考:install_with_constraints是我从Claudio借来的函数,经过编辑后可以在Windows上运行:

代码语言:javascript
复制
def install_with_constraints(
    session: Session, *args: str, **kwargs: Any
) -> None:
    """
    Install packages constrained by Poetry's lock file.

    This function is a wrapper for nox.sessions.Session.install. It
    invokes pip to install packages inside of the session's virtualenv.
    Additionally, pip is passed a constraints file generated from
    Poetry's lock file, to ensure that the packages are pinned to the
    versions specified in poetry.lock. This allows you to manage the
    packages as Poetry development dependencies.

    Arguments:
        session: The Session object.
        args: Command-line arguments for pip.
        kwargs: Additional keyword arguments for Session.install.
    """
    req_path = os.path.join(tempfile.gettempdir(), os.urandom(24).hex())
    session.run(
        "poetry",
        "export",
        "--dev",
        "--format=requirements.txt",
        f"--output={req_path}",
        external=True,
    )
    session.install(f"--constraint={req_path}", *args, **kwargs)
    os.unlink(req_path)
票数 2
EN

Stack Overflow用户

发布于 2021-09-05 00:32:35

我想使用不带poetry的nox会话,所以我使用poetry为session.install()生成参数,如下所示:

代码语言:javascript
复制
@nox.session
def tests(session: nox.sessions.Sessio0n):
    """ Run all tests """
    session.install(*requirements_txt, '.', 'pytest')
    session.run('pytest', 'tests/')


# Get requirements.txt from poetry
import tempfile, subprocess
with tempfile.NamedTemporaryFile('w+') as f:
    subprocess.run(f'poetry export --no-interaction --dev --format requirements.txt --without-hashes --output={f.name}', shell=True, check=True)
    f.seek(0)
    requirements_txt = f.readlines()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59768651

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档