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

Add ability for many nox tasks to reuse the same session/virtual env #167

Open
DDaxler opened this issue Feb 18, 2019 · 9 comments
Open

Comments

@DDaxler
Copy link

DDaxler commented Feb 18, 2019

How would this feature be useful?
On windows its slow to build a new environment, having to build identical environments seems a waste.

Describe the solution you'd like

import nox

@nox.session(python="3.6")
def all_tests(session):
    session.install('-rrequirements.txt')
    session.install('-rrequirements_test.txt')
    session.install('-rrequirements_docs.txt')
    session.install('-rrequirements_dist.txt')

@nox.session(reuse="all_tests")
def build_docs(session):
    session.run('sphinx-build', 'source', 'build/html')

@nox.session(reuse="all_tests")
def pylint(session):
    session.run("pylint", "code")

@nox.session(reuse="all_tests")
def yapf(session):
    session.run("yapf", "-r", "-d", "code")

reuse is the new "function"

Describe alternatives you've considered
Havent found an easy way to reuse environments.

@stsewd
Copy link
Collaborator

stsewd commented Feb 19, 2019

I think a way of doing this would be using https://nox.readthedocs.io/en/stable/config.html#nox.sessions.Session.posargs, of course you'll lose the feature of listing sessions (nox -l)

Also, maybe #72 is more useful here?

@stsewd
Copy link
Collaborator

stsewd commented Aug 24, 2019

Maybe doing this from the command line? Like nox -s lint tests --share-env.

@paunovic
Copy link

paunovic commented Jan 10, 2020

Are there plans to implement this? I'd be happy to work on it and create a feature PR.

@theacodes
Copy link
Collaborator

theacodes commented Jan 11, 2020 via email

@cs01
Copy link
Contributor

cs01 commented Jan 11, 2020

A related approach that would save time around environment creation/reuse is #265.

@theacodes
Copy link
Collaborator

I like the solution proposed at #286 (comment)

@Spectre5
Copy link

I also like the solution proposed at #286 (comment). Just a quick thought, you can use "notify" to simulate some parts of this, with some caveats:

  • Only one session can run on any Nox invocation, as you can't use that shared environment more than once.
  • This doesn't attempt to handle the "requires", which would certainly be nice to have at times.
import nox

nox.options.reuse_existing_virtualenvs = True

@nox.session
def shared_venv(session):
    session.install('black')
    session.install('isort')
    session.install('flake8')

    if session.posargs and session.posargs[0] == 'format':
        session.run('black', '.')
        session.run('isort', '.')
    elif session.posargs and session.posargs[0] == 'lint':
        session.run('black', '--check', '.')
        session.run('isort', '--check-only', '.')
        session.run('flake8')

@nox.session
def format(session):
    session.notify('shared_venv', posargs=['format'])

@nox.session
def lint(session):
    session.notify('shared_venv', posargs=['lint'])

This could then be used as below. The first invocation will only create that shared virtual environment since the other two only notify that one, which will be ignored since each session can only run once. The second two only "lint" or only "format" the code. But only one could be done on each invocation of Nox.

$ nox
$ nox -s lint
$ nox -s format

This obviously isn't a very great solution, but just something that came to mind. I'd certainly like to get the proper solution implemented.

@gschaffner
Copy link

Hi all! There is an implementation of this over on #631 (comment). It would be great to get some feedback there.

@henryiii
Copy link
Collaborator

henryiii commented Apr 6, 2024

FYI, this was the syntax I came up with thinking about this before reading the issues:

import nox

@nox.env(python="3.6")
def all_tests(session):
    session.install('-rrequirements.txt')
    session.install('-rrequirements_test.txt')
    session.install('-rrequirements_docs.txt')
    session.install('-rrequirements_dist.txt')

@all_tests.session()
def build_docs(session):
    session.run('sphinx-build', 'source', 'build/html')

@all_tests.session()
def pylint(session):
    session.run("pylint", "code")

@all_tests.session()
def yapf(session):
    session.run("yapf", "-r", "-d", "code")

You could even pass a restricted session in this case that wouldn't allow install in the sessions to reduce potential for order dependent behavior. I assume this is quite a bit of work, just putting down what I though of.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

8 participants