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.
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
- Run
regedit.exe(Windows + Rand typeregeditplusEnter) - Navigate to HKEY_CLASSES_ROOT > Directory > Background > shell
- Add a key named
AnacondaPromptand set its value toAnaconda Prompt Here(or anything you'd like it to appear as in the right click context menu) - Add a key under this key, called
command, and set its value tocmd.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\Miniconda3in my case, I had to remove%windir%\System32\at the beggining because it wouldn't work otherwise, but it's fine becausecmd.exeis in thePATH. 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
CondaEnvskey 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
CondaEnvskey, with a subkeyshell:- Add a new key for each environment, each key has a
MUIVerb("base" for instance) and acommandsubkey (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' ")
- Add a new key for each environment, each key has a
- HKEY_CLASSES_ROOT/Directory/Background/shell: Add a
Question:
- Does the above activate first conda base and then the target environment or directly the target environment?
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
AutoRunkey atHKCU\Software\Microsoft\Command Processor\AutoRunto something like"C:\ProgramData\Miniconda3\condabin\conda_hook.bat", after that runningcmd.exeruns firstconda_hook.batwhich temporarily adds thepath\to\MinicondaOrAnaconda\condabinpath to thePATH, making thecondacommand available from the Command Prompt. In order not to run that hook file, open the Command Prompt with the/Dflag (Windows + Rand typecmd /DandEnter, check that...\condabinis not in thePATHwithecho %PATH%). - One weird thing though. To run
conda init, the Command Prompt must be able to findcondain the first place! So that it works, that command must be executed from the folderpath\to\MinicondaOrAnaconda\Scriptswhereconda.exelies. Question: it's likely that runningconda initfrom 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 --reverseaims at reversing all the changes made byconda initbut I'm not sure it worked well.conda init --dry-rundoesn't do anything but print the changes that would be made ifconda initwas to be run.
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\Scriptsto makecondaavailable from the Command Prompt. Warning: it makes all the executable files (.batand.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\MinicondaOrAnacondato makepythonavailable from the Command Prompt. This folder hostspython.exe, it's the Python version installed in the base environment. Now, runningpythonfrom 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 activateto activate the base environment (ifauto_activate_baseisTrue, which is the default as far as I know) orconda activate someotherenv, after that,pythonwill be available from the Command Prompt - Run
conda run python somescript.pyorconda run -n someotherenv python somescript.pyto 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 dointpython somescript.py, but at least the conda environment is activated and that should make conda devs happy (warning: running justconda run pythonhad 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 executequit(), so that's definitely not the way to open the interactive interpreter)