Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Poetry with Nox #2920

Closed
1 task done
danieleades opened this issue Sep 15, 2020 · 13 comments
Closed
1 task done

Using Poetry with Nox #2920

danieleades opened this issue Sep 15, 2020 · 13 comments
Labels
area/docs Documentation issues/improvements

Comments

@danieleades
Copy link
Contributor

danieleades commented Sep 15, 2020

  • I have searched the issues of this repo and believe that this is not a duplicate.

Issue

I've been trying to migrate an existing project to Poetry. It is using Tox to test a matrix of different package versions. The difficulties of trying to get Poetry and Tox to play nicely together for matrices of package versions is fairly well-documented (see https://stackoverflow.com/questions/59377071/how-can-i-get-tox-and-poetry-to-work-together-to-support-testing-multiple-versio)

I'm playing around with Nox as an alternative, and it's looking very promising for being easier to integrate with Poetry, but there are still a few caveats.

I have the following Nox script for testing against a couple of different python interpreters, and a couple of different versions of sphinx-

import nox

@nox.session(python = ["3.5", "3.6"]
@nox.parametrize("sphinx", ["^2.4", "^3.0"])
def tests(session, sphinx)
    session.run('poetry', 'add', f'sphinx@{sphinx}', external=True)
    session.run('poetry', 'install', external=True)
    session.run('pytest')

this works perfectly, except that it modifies the pyproject.toml file in my project's root. Is there a better way for me to arrange this?

@danieleades danieleades added area/docs Documentation issues/improvements status/triage This issue needs to be triaged labels Sep 15, 2020
@danieleades
Copy link
Contributor Author

see also #1745

@danieleades
Copy link
Contributor Author

cross-posted to wntrblm/nox#347

@max-hoffman
Copy link

I ran into the same problem this afternoon, decided to just use tmp directories so that I could modify and discard the repo repeatedly:

from distutils.dir_util import copy_tree
import os
import pathlib
import shutil
import tempfile

import nox

@nox.session(python=["3.8", "3.7", "3.6"])
@nox.parametrize("numpy", ["1.19.2", "^1.18.4", "^1.17.5"])
@nox.parametrize("pillow", ["7.0.0", "^6.0.0", "^5.4.0"])
def tests(session, numpy, pillow):
    with tempfile.TemporaryDirectory() as tmp_dir:
        base_dir = pathlib.Path(os.path.abspath("."))
        copy_tree(base_dir, tmp_dir)
        os.chdir(tmp_dir)

        args = session.posargs or ["--cov=flywheel"]
        session.run('poetry', 'add', f"numpy@{numpy}", f"pillow@{pillow}", external=True)
        session.run("poetry", "install", external=True)
        session.run("pytest", *args)

@danieleades
Copy link
Contributor Author

@max-hoffman that's a neat solution. I was considering just caching and replacing the pyproject.toml and poetry.lock.
Tell me, are you using virtualenvs inside your project? Any interactions there?

@danieleades
Copy link
Contributor Author

@sdispater, the Nox maintainers have reached out and I suspect would be keen to investigate a tighter integration between Poetry and Nox. Is this something you'd be interested in looking into?
Specifically, the use case is around using Poetry and Nox to run tests over a dependency matrix

@max-hoffman
Copy link

@danieleades a try/except/finally block where the original pyproject.toml and poetry.lock are always restored would probably be more efficient than what I wrote. I am running the code with poetry global mode in a docker image. I'd need to take a closer look at what caches are maintained between session runs, but I'm starting with fresh nox and poetry caches in the environment I'm running in.

@danieleades
Copy link
Contributor Author

@max-hoffman the try/except/finally approach doesn't work for me.

i've got

import nox
from pathlib import Path

python_versions = [
    "3.8",
]

sphinx_versions = [
    "^2.3",
]

def cache(file: Path) -> (Path, str):
    with open(file) as f:
        cache = f.read()
    return (file, cache)

def dump(cache: (Path, str)):
    (file, data) = cache
    with open(file, 'w') as f:
        f.write(data)

project_file = cache('./pyproject.toml')
lockfile = cache('./poetry.lock')

try:

    @nox.session(python=python_versions)
    @nox.parametrize("sphinx", sphinx_versions)
    def tests(session, sphinx):
        session.run('poetry', 'add', f'sphinx@{sphinx}', external=True)
        session.run('poetry', 'install', external=True)
        session.run('nosetests')

finally:
    dump(project_file)
    dump(lockfile)

try as I might, I can't get that finally block to run. not sure how Nox handles exiting...

@danieleades
Copy link
Contributor Author

I ran into the same problem this afternoon, decided to just use tmp directories so that I could modify and discard the repo repeatedly:

from distutils.dir_util import copy_tree
import os
import pathlib
import shutil
import tempfile

import nox

@nox.session(python=["3.8", "3.7", "3.6"])
@nox.parametrize("numpy", ["1.19.2", "^1.18.4", "^1.17.5"])
@nox.parametrize("pillow", ["7.0.0", "^6.0.0", "^5.4.0"])
def tests(session, numpy, pillow):
    with tempfile.TemporaryDirectory() as tmp_dir:
        base_dir = pathlib.Path(os.path.abspath("."))
        copy_tree(base_dir, tmp_dir)
        os.chdir(tmp_dir)

        args = session.posargs or ["--cov=flywheel"]
        session.run('poetry', 'add', f"numpy@{numpy}", f"pillow@{pillow}", external=True)
        session.run("poetry", "install", external=True)
        session.run("pytest", *args)

i was doing something like this and decided to throw in a print to double-check the interpreter version. I found that is was using the version installed by Poetry (3.8 for me) in every case, regardless of the version configured in the Nox session. Can you confirm whether you see the same behaviour?

@layday
Copy link

layday commented Oct 5, 2020

You could do something like this:

from shutils import copytree

import nox


@nox.session(python=["3.8", "3.7", "3.6"])
@nox.parametrize("numpy", ["1.19.2", "^1.18.4", "^1.17.5"])
@nox.parametrize("pillow", ["7.0.0", "^6.0.0", "^5.4.0"])
def tests(session, numpy, pillow):
    tmp_dir = session.create_tmp()
    copytree(base_dir, tmp_dir)

    session.install("poetry")
    session.run("poetry", "add", f"numpy@{numpy}", f"pillow@{pillow}")
    session.run("poetry", "install")

    args = session.posargs or ["--cov=flywheel"]
    session.run("pytest", *args)

The issue with this is that it introduces all of Poetry's dependencies in the test session's virtual env.

@layday
Copy link

layday commented Oct 5, 2020

You could also use nox-poetry which exports a requirements file with Poetry and defers to pip for installing packages.

@aidan-melen
Copy link

aidan-melen commented Nov 19, 2020

We should all thank @cjolowicz for this cookiecutter template taht implements nox-poetry: https://cookiecutter-hypermodern-python.readthedocs.io/en/2020.10.15.1/

He also wrote nox-poetry plugin

@Secrus
Copy link
Member

Secrus commented May 16, 2022

Poetry team can't and won't keep track of all the tools that are used and without it, maintenance of such documentation would be impossible.

@Secrus Secrus closed this as completed May 16, 2022
@Secrus Secrus added wontfix and removed status/triage This issue needs to be triaged labels May 16, 2022
Copy link

github-actions bot commented Mar 2, 2024

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area/docs Documentation issues/improvements
Projects
None yet
Development

No branches or pull requests

5 participants