anaconda / anaconda-project

Tool for encapsulating, running, and reproducing data science projects
https://anaconda-project.readthedocs.io/en/latest/
Other
221 stars 88 forks source link

execute custom scripts on prepare #239

Open mcg1969 opened 4 years ago

mcg1969 commented 4 years ago

It would be good if there is a way to execute a script of some sort upon environment preparation.

AlbertDeFusco commented 3 years ago

Would something like this fulfill this need? Multiple commands could be tagged as run_after_prepare and they would execute in the order shown in the YAML file when running anaconda-project prepare.

Or do you think a more specialized specification of post-prepare commands be more inline with what you want?

name: prepare-with-script

packages:
  - python=3.8

env_specs:
  default: {}

commands:
  my-command:
    unix: <some action here>
    win: <some action here>
    env_spec: default
    run_after_prepare: True
jbednar commented 3 years ago

Seems like with the proposal above, if someone does "anaconda-project run", my-command would be executed twice, once after prepare and once since it's the default command. But I guess you'd interpret it as "make sure it's run at least once"?

AlbertDeFusco commented 3 years ago

Indeed, the command should only be run once on the cold use of anaconda-project run.

Does someone have an example of a post-prepare script that can motivate this?

jbednar commented 3 years ago

I do not, but I guess the semantics would have to be that if run_after_prepare==True, that command cannot be the default command.

mforbes commented 3 years ago

@AlbertDeFusco An example of a post-prepare script is to install a python kernel for use with Jupyter notebooks. Something like:

 python -m ipykernel install --user --name "my-env" --display-name "Python 3 (my-env)"

In particular, I am trying to do this on CoCalc which runs its own customized notebook server that needs kernels to be properly installed.

AlbertDeFusco commented 3 years ago

Interesting. So for CoCalc you need to make sure that the kernel created using anaconda-project arrives in the ~/.local/share/jupyter directory rather than the project environment directory.

https://doc.cocalc.com/howto/install-python-lib.html#configure-a-jupyter-kernel-for-my-custom-anaconda-environment

mforbes commented 3 years ago

Yes: the "notebook" is not run with the jupyter from the project environment, but from somwhere else, and so does not know about the project envifornment directory unless one properly installs the kernel.

mcg1969 commented 3 years ago

Thank you for your input, @mforbes ! In fact, we encounter this scenario often: the conda environment used to run Jupyter Notebook or JupyterLab is different than the environment running the kernel. The nb_conda_kernels package is useful for such situations, and I wonder whether or not CoCalc might consider using something like that, or similar.

Nevertheless I agree that a post-prepare script would handle this scenario well.