Skip to content

Instantly share code, notes, and snippets.

@maximlt
Last active February 23, 2024 18:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maximlt/5b1502b8f973855eed1ae49a570d0d84 to your computer and use it in GitHub Desktop.
Save maximlt/5b1502b8f973855eed1ae49a570d0d84 to your computer and use it in GitHub Desktop.
Make conda easily available on Windows

Make conda easily available on Windows

The goal is to lower the burden of running a script with Python when that Python executable is encapsulated within a conda environment. There are several ways to do that.

Add Anaconda Prompt to Windows right click context menu

Adapted from jiewpeng's gist (original gist here)

This describes how to add Anaconda Prompt to the Windows right click context menu. In doing so, the user can open a Command Prompt in any folder from the File Explorer, which is going to be activated

  1. Run regedit.exe (Windows + R and type regedit plus Enter)
  2. Navigate to HKEY_CLASSES_ROOT > Directory > Background > shell
  3. Add a key named AnacondaPrompt and set its value to Anaconda Prompt Here (or anything you'd like it to appear as in the right click context menu)
  4. Add a key under this key, called command, and set its value to cmd.exe "/K" C:\ProgramData\Miniconda3\Scripts\activate.bat C:\ProgramData\Miniconda3. To get that command, right click on the Anaconda Prompt shortcut available in the Windows Start menu, go to Properties, copy the text saved in the Target filed. It was %windir%\System32\cmd.exe "/K" C:\ProgramData\Miniconda3\Scripts\activate.bat C:\ProgramData\Miniconda3 in my case, I had to remove %windir%\System32\ at the beggining because it wouldn't work otherwise, but it's fine because cmd.exe is in the PATH. Actually the full path of the executable (here cmd.exe) could be used, this seems to be the usual way to do it (Powershell, GIT Bash, etc.). It is possible to activate a specific environment by providing its root folder path.

Notes:

  • The "/K" switch keeps the prompt open after the command is executed

Enhanced version - Add the possibility to activate specific environments with a Shift Right Click in the explorer (that's for Windows 10):

TODO: Write a Python script to change the registry:

  • It needs to be run from the base environment (which is the environment from which conda can be imported)
  • Get a list of the conda envs (see the result of conda.cli.python_api.run_command("info", "--env")
  • Find the location of Powershell (or Command Prompt), maybe with where powershell (subprocess?)
  • Check Windows version (start with windows 10)
  • Modify the registry:
    • HKEY_CLASSES_ROOT/Directory/Background/shell: Add a CondaEnvs key with 3 values:
      • Extended = "" (empty, it means that the shortcut is available only from the extended contextual menu, opened with a Shift Right Click)
      • MUIVerb = "Activate Conda Envs"
      • ExtendedSubCommandsKey = "Directory\ContextMenus\CondaEnvs" (link to other keys that specifies sub commands, so that the contextual menu is not bloated with conda environments)
    • HKEY_CLASSES_ROOT/Directory/Background/ContextMenus: Add a CondaEnvs key, with a subkey shell:
      • Add a new key for each environment, each key has a MUIVerb ("base" for instance) and a command subkey (C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy ByPass -NoExit -Command "& 'C:\Users\maxim\Miniconda3\shell\condabin\conda-hook.ps1' ; conda activate 'C:\Users\maxim\Miniconda3' ")

Question:

  • Does the above activate first conda base and then the target environment or directly the target environment?

Initialize conda in the Command Prompt with conda init

conda init allows to initialize various shells (cmd.exe by default on Windows, Powershell, bash, etc.) for conda. What it does exactly isn't so well described in its doc. As far as i understand it, it does at least the following.

  • Set the AutoRun key at HKCU\Software\Microsoft\Command Processor\AutoRun to something like "C:\ProgramData\Miniconda3\condabin\conda_hook.bat", after that running cmd.exe runs first conda_hook.bat which temporarily adds the path\to\MinicondaOrAnaconda\condabin path to the PATH, making the conda command available from the Command Prompt. In order not to run that hook file, open the Command Prompt with the /D flag (Windows + R and type cmd /D and Enter, check that ...\condabin is not in the PATH with echo %PATH%).
  • One weird thing though. To run conda init, the Command Prompt must be able to find conda in the first place! So that it works, that command must be executed from the folder path\to\MinicondaOrAnaconda\Scripts where conda.exe lies. Question: it's likely that running conda init from the Anaconda Prompt would allow initializing the Command Prompt (and Powershell and...).
  • What else? Does it directly activate base? It doesn't look like so but maybe something went wrong when I did it (actually, that worked in Powershell, but for some reasons not in the Command Prompt). It also modified some other files but I'm not sure which ones exactly. conda init --reverse aims at reversing all the changes made by conda init but I'm not sure it worked well. conda init --dry-run doesn't do anything but print the changes that would be made if conda init was to be run.

Add conda to the PATH

It is possible to add conda to the PATH directly during the install of Anaconda or Miniconda. The option isn't ticked by default, and the text describing the option turns red when it's ticked, because they just don't promote it. Yet, the option is there, so it might be useful for some people after all, the important thing is to know what it does exactly and what could go wrong.

If this option isn't selected, two useful paths can be added to the PATH (Google how to modify the PATH environment variable, there will be loads of websites explaining how to do that):

  • path\to\MinicondaOrAnaconda\Scripts to make conda available from the Command Prompt. Warning: it makes all the executable files (.bat and .exe) found in that folder available from the Prompt. Those executables are those installed in the base environment so there can be lots and lots of them. This is maybe why they do not recommand this approach during the install?
  • path\to\MinicondaOrAnaconda to make python available from the Command Prompt. This folder hosts python.exe, it's the Python version installed in the base environment. Now, running python from the Command Prompt will always start its interactive interpreter.

For my setting (conda==4.7.11), I get this nice little warning when I do so:

Warning:
This Python interpreter is in a conda environment, but the environment has
not been activated.  Libraries may fail to load.  To activate this environment
please see https://conda.io/activation

Obviously, this approach isn't recommended, so maybe, it's not such a good idea to add python directly to the PATH! So if conda is available from the PATH but not python, how to run a script like it's shown everywhere with python somescript.py???

  • Run conda activate to activate the base environment (if auto_activate_base is True, which is the default as far as I know) or conda activate someotherenv, after that, python will be available from the Command Prompt
  • Run conda run python somescript.py or conda run -n someotherenv python somescript.py to run the script with Python after activating a conda environment (base by default, I guess). When doing so, the startup time is probably a little bit longer thant just doint python somescript.py, but at least the conda environment is activated and that should make conda devs happy (warning: running just conda run python had a weird behaviour on my laptop, the usual >>> were not displayed, I could type in some normal Python code but that wouldn't print anything, to quit I had to execute quit(), so that's definitely not the way to open the interactive interpreter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment