You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are are number of open issues regarding specific details of creating session virtual environments (e.g., #680, #662, #260). For example, the creation of conda virtual environments is often intimately linked to an environment.yaml file. While dependencies can often be installed after the virtual environment creation, this can be limiting. This is especially true for locked conda environments (e.g. from conda-lock) which combines creating the environment and installing all dependencies. If a callable could be passed as a venv_backend, you could do something like the following:
defcreate_conda_lock_env(
location: str,
interpreter: str|None=None,
reuse_existing: bool=False,
venv_params: Any=None,
) ->CondaEnv:
venv=CondaEnv(
location=location,
interpreter=interpreter,
reuse_existing=reuse_existing,
venv_params=venv_params,
)
# assume venv_params contains conda-lock.yml fileifisinstance(venv_params, str) andos.path.exists(venv_params):
lock_file=venv_paramselse:
raiseValueError("pass `venv_params={conda-lock-file}`")
# Custom creating (based on CondaEnv.create)ifnotvenv._clean_location():
logger.debug(f"Re-using existing conda env at {venv.location_name}.")
venv._reused=Trueelse:
cmd= ["conda-lock", "install", "--prefix", venv.location, lock_file]
logger.info(
f"Creating conda env in {venv.location_name} with conda-lock {lock_file}"
)
nox.command.run(cmd, silent=True, log=nox.options.verboseorFalse)
returnvenv# Note that it's on the end user/custom backend to make sure passing python=.... makes sense.@nox.session(python="3.11",venv_backend=create_conda_lock_env,venv_params="py311-test-conda-lock.yml",)deftest(session: nox.Session) ->None:
...
Similarly, you could use this approach to:
Create environments using micromamba
Customize python search path for python interpreters
def_create_venv(self) ->None:
ifcallable(self.func.venv_backend):
# if passed a callable backend, always use just that# i.e., don't overridebackend=self.func.venv_backendelse:
backend= (
self.global_config.force_venv_backendorself.func.venv_backendorself.global_config.default_venv_backend
)
ifbackend=="none"orself.func.pythonisFalse:
self.venv=PassthroughEnv()
returnreuse_existing= (
self.func.reuse_venvorself.global_config.reuse_existing_virtualenvs
)
ifcallable(backend):
self.venv=backend(
location=self.envdir,
interpreter=self.func.python,
reuse_existing=reuse_existing,
venv_params=self.func.venv_params,
)
returnelifbackendisNoneorbackend=="virtualenv":
...
Down the road, it would be nice to be able to create custom environment classes by subclassing nox.virtualenv.ProcessEnv.
Describe alternatives you've considered
You can currently use things like conda-lock with nox, but they require creating the environment twice. First nox creates the environment, then conda-lock recreates the environment. This is not ideal.
Anything else?
I have a minimally working example. I'm not totally sure how to integrate this into the test suite. If theres interest, I'm happy to make a PR.
The text was updated successfully, but these errors were encountered:
How would this feature be useful?
There are are number of open issues regarding specific details of creating session virtual environments (e.g., #680, #662, #260). For example, the creation of conda virtual environments is often intimately linked to an
environment.yaml
file. While dependencies can often be installed after the virtual environment creation, this can be limiting. This is especially true for locked conda environments (e.g. from conda-lock) which combines creating the environment and installing all dependencies. If a callable could be passed as avenv_backend
, you could do something like the following:Similarly, you could use this approach to:
conda env create -f environment.yaml
For the last one, you could do the following:
In general, this would provide a means for third party packages to hook in to nox.
Describe the solution you'd like
I believe this should be straight forward. I'd simply replace the logic in
nox.session.SesssionRunner._create_env
with something like:
Down the road, it would be nice to be able to create custom environment classes by subclassing
nox.virtualenv.ProcessEnv
.Describe alternatives you've considered
You can currently use things like conda-lock with nox, but they require creating the environment twice. First nox creates the environment, then conda-lock recreates the environment. This is not ideal.
Anything else?
I have a minimally working example. I'm not totally sure how to integrate this into the test suite. If theres interest, I'm happy to make a PR.
The text was updated successfully, but these errors were encountered: