Robert Lugg
Robert Lugg

Reputation: 1192

specify commands to run after conda create from yml file

I have a environment.yml file which I used to create a Python environment using:

conda env create --file environment.yml.

After the environment is created, I need to perform some operations (such as registering a kernel with jupiter-lab):

ipython kernel install --name=to_the_edge

I would like to embed one or more shell commands to run "post install" so that the setup is self-contained within the .yml file. Is there a way to do this? Or is there a different way within conda to get close to what I'm after?

I would also like a way to specify shell commands to be run after conda activate, but that's a secondary hope.

Maybe this isn't possible because conda works cross platform?

Upvotes: 7

Views: 2650

Answers (1)

merv
merv

Reputation: 76810

This isn't really possible with standard Conda commands, but there are some options to obtain such functionality.

Jupyter and Conda

The best practice for Jupyter and Conda is to have a single env that has jupyter installed and also has nb_conda_kernels. You always launch jupyter notebook from this env. The nb_conda_kernels package enables Jupyter to automatically detect any other envs that have ipykernel (or other language equivalents, e.g., r-irkernel). Hence, you don't need any additional registration, but simply need to include ipykernel in the YAML. See the docs for nb_conda_kernels.

Running scripts at install

This cannot be done from a YAML. However, you could build your own custom package that does this at install time and then include that in your YAML. You would have to provide the .sh, .bat, etc. to run the commands. See the documentation on adding pre-link, post-link, and unlinked scripts to a package recipe.

Through this route, you can also add activate and deactivate scripts that are run when the env is activated and deactivated, respectively. You can also add such scripts manually, i.e., without a custom package. For example, the docs show how to define environment variables at activation, but you can run arbitrary scripts.

Upvotes: 5

Related Questions