0

I am using IPython in Jupyter notebooks on my Windows PC for a variety of tasks that require different python environments for different tasks. Jupyter notebook server is started in a specific environment for the task at hand.

For the sake of example, suppose I have the base environment and environments for different application domains, e.g. geodata, ml, voice, etc.

One thing I am missing is the ability to run conda commands from within a notebook cell using ! or magic for system shell access. For example, since conda environment is not activated in the default shell, I get

In : ! conda env list
Out: "conda" is not recognized as an internal or external command.`

So my problem is to run conda commands such as conda env list or conda install -y packagename from a notebook cell.

One workaround I could come up with is similar to the suggestion in this post. It is to use conditional processing symbols && to run activate.bat together with the actual command I want to run.

This gets verbose so I define a few variables to shorten the actual command.

ENV_NAME ="geodata"
CMD_SHELL = "%windir%\\System32\\cmd.exe"
CMD_ACTIVATE = "C:\\ProgramData\\Anaconda3\\Scripts\\activate.bat"
CMD_ACTIVATE_ENV = f"{CMD_ACTIVATE} %userprofile%\\.conda\\envs\\{ENV_NAME}"

Then to run conda env list I can run this in a notebook cell:

! {CMD_SHELL} /C "{CMD_ACTIVATE_ENV} && conda env list"

or to get package «packagename» installed in the current environment I can run

! {CMD_SHELL} /C "{CMD_ACTIVATE_ENV} && conda install -y «packagename»"

And my question is: is there an easier way to get the same result? I'd prefer to be able to run just

! conda install -n geodata -y «packagename»
Dima Chubarov
  • 16,199
  • 6
  • 40
  • 76

0 Answers0