-
-
Notifications
You must be signed in to change notification settings - Fork 153
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
Allow passing callable venv_backend for creation of session virtualenv #753
base: main
Are you sure you want to change the base?
Conversation
This branch allows passing a callable for `venv_backend` in `nox.session`. This allows the user to apply any special commands during the virtualenv creation. As an example, `noxfile.py` includes some demo session using this functionality. * conda-env-backend: creates an environment using `conda env create` * conda-lock-backend: creates an environment using `conda-lock install` * dev-example: Creates a development. This creates an environment in `.nox/.venv` for demo purposes Other potential uses: * Alter the python interpreter search path * Add smart caching of environment. i.e., add check in callback to reinstall/recreate only if dependencies have changed The whole point is that the approach is general. Theres a degree of buyer beware with this functionality. In the examples in `noxfile.py`, the correct python version must be specified in the `environment.yaml` files. To me, the added functionality is worth it.
Pretty basic testing, but now have coverage
Any chance someone could take a look at this? |
* Added micromamba_test sessions to `noxfile.py` * Updated conda env create section of cookbook to also include a micromamba example
Thank you for the suggestion @jayqi! I added sessions |
I think this merits broader discussion because you can't use the callable without accessing undocumented Nox internals. |
(I was thinking that, was getting worried about how much of nox's internals would be expected to be touched by users with this. #762 changed the internals a bit, and we might not have been able to do that if this was public) |
Thank you for taking a look at this PR! I agree that using private/undocumented internals is less than ideal. But I still think functionality like this would be awesome. Before #762, I was myself playing with using uv via the callable backend. I'd like to take a look at #762 to get a feel of what internals have changed. Would it make sense to propose making a minimal set of the private internals touched here public? I'd be happy to take a crack at that. Thanks again! |
Let's wait till after the next release, then it might be interesting to discuss. A few more environment related changes are going in. |
I think I have a better solution, that will require less interaction with nox internals. There are two things I'm trying to override with the callback.
For (1), the obvious use case is using nox to manage development environments. Something like the following: @nox.session(venv_location=".venv", ....)
def dev(session: nox.Session) -> None:
.... For (2), the use case is customizing the creation of conda environments. Instead of trying to hook into the virtualenv creation, you could instead just ask for the @nox.session(venv_skip_create=True)
def test(session: nox.Session) -> None:
if not session.virtualenv._reused: # I'd like to make the "reused property public"
session.run_always(
"conda",
"env",
"create",
"-f",
f"py{session.python}-environment.yaml",
"--prefix",
session.virtualenv.location, # might add session.location, session.location_name?
)
... where as noted, I'd like to make the I'm happy to work on this. Should I make a new PR with the above functionality? If so, should I split into 2 PR's or just one? |
IMO, venv location makes sense. Being able to set it to something other than |
Excellent! I'll put a new PR together with that functionality. |
This addresses #751
This PR allows for callable
venv_backend
tonox.session
. This functionality can be used to, for example,environment.yaml
files during creationThis is a general solution that should be applicable to a number of open issues.
Main changes:
nox.sessions.SessionRunner._create_venv
to look for a callable.Customized virtual environment creation
indocs/tutorial.rst
and example usagein
docs/cookbook.rst
.tests_session.py
. Nothing too deep, but there is coverage.conda_tests
session ofnoxfile.py
to use this functionality. This now creates conda environments usingconda env create -f ....
with environment files underenvironment/py{py_version}-conda-test.yaml
. These files are created fromrequirements-conda-test.txt
. There is also a session to use conda-lock.This is primarily for demonstration purposes.
Open issues:
SessionRunner._reused
andSessionRunner._clean_location
). Does it make sense to make these public? I think that this is "advanced" functionality, so I'm happy to go with it.The parameters where copied from the previous
SessionRunner._create_env
calls toVirtualEnv
, etc, with the addition ofrunner
. I added this if someone wants to access the calling runner instance. This might be seen as repetitive, as all these parameters are derived fromrunner
.Please let me know how I can improve the PR. Thank you!