21

I'd like to add an option on my context menu (Windows 7 and 10) to open an Anaconda Prompt into the file location when I right-click the folder, but I can't figure out the right registry key.

Here's what I know how to do:

  • Add an item to the context menu that opens a normal command window at the folder location
  • Open an Anaconda prompt from cmd (run their "activate.bat" file)

What I can't figure out is how to combine these steps into a single registry key so I can open an Anaconda Prompt and then cd in that prompt to the current folder. But maybe I'm approaching this the wrong way.

Help from internet gurus is appreciated.

jrinker
  • 2,010
  • 2
  • 14
  • 17

3 Answers3

20
  1. Run Registry Editor (regedit.exe)
  2. Go to HKEY_CLASSES_ROOT > Directory > Background > shell
  3. Add a key named AnacondaPrompt and set its value to Anaconda Prompt Here
  4. Add a key under this key called command, and set its value to cmd.exe /K C:\Users\user\Anaconda3\Scripts\activate.bat change the location to wherever your Anaconda installation is located.
kdebugging
  • 507
  • 6
  • 10
12

In recent Anaconda versions (I'm at conda 4.5.5) they have changed the behaviour and the shortcut to Anaconda Prompt, so the new procedure is in fact a bit simpler than described by bdforbes.

The new way to launch Anaconda Prompt in a folder is

cmd.exe /K %%USERPROFILE%%\AppData\Local\Continuum\Anaconda3\Scripts\activate.bat

pushd is to change the current directory, %V is the current directory, and /K is to run a command.

So the modified cwp2.py is not needed anymore. Put the following contents in a .bat-file and run as administrator to add the needed keys to the registry (a modified version of the gist posted by Thibaud Ruelle in the comments to the other answer)

REG ADD HKCR\Directory\Background\shell\Anaconda\ /ve /f /d "Anaconda Prompt Here"
REG ADD HKCR\Directory\Background\shell\Anaconda\ /v Icon /f /t REG_EXPAND_SZ /d %%USERPROFILE%%\\Anaconda3\\Menu\\Iconleak-Atrous-Console.ico
REG ADD HKCR\Directory\Background\shell\Anaconda\command /f /ve /t REG_EXPAND_SZ /d "%windir%\System32\cmd.exe pushd "%V" "/K" %%USERPROFILE%%\Anaconda3\Scripts\activate.bat %%USERPROFILE%%\Anaconda3"
REG ADD HKCR\Directory\shell\Anaconda\ /ve /f /d "Anaconda Prompt Here"
REG ADD HKCR\Directory\shell\Anaconda\ /v Icon /f /t REG_EXPAND_SZ /d %%USERPROFILE%%\\Anaconda3\\Menu\\Iconleak-Atrous-Console.ico
REG ADD HKCR\Directory\shell\Anaconda\command /f /ve /t REG_EXPAND_SZ /d "%windir%\System32\cmd.exe pushd "%V" "/K" %%USERPROFILE%%\Anaconda3\Scripts\activate.bat %%USERPROFILE%%\Anaconda3"
Filip S.
  • 1,514
  • 3
  • 19
  • 40
  • Nice! Thanks for writing that up. I'll add a note in my answer. – bdforbes Jul 11 '18 at 10:24
  • Filip, you might want to check if the registry correctly launches the prompt from the drive root, as per the update to my answer. – bdforbes Aug 04 '18 at 10:22
  • 5
    Worth noting that this answer depends on where Anaconda is installed (which I appreciate seems obvious, but took me a while to figure out). To that end, my two `command` HKEYs become `C:\WINDOWS\System32\cmd.exe pushd "%V" "/K" "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\Scripts\activate.bat" "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64"`, and the icon is found at `"C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\Anaconda3_64\\Menu\\Iconleak-Atrous-Console.ico"`. You can find this out by checking the properties of the Anaconda shortcut. – Greedo Oct 27 '18 at 20:49
  • 1
    For me, the following just works: `cmd.exe /K %%USERPROFILE%%\AppData\Local\Continuum\Anaconda3\Scripts\activate.bat`. There is no need for `pushd` or the argument following `activate.bat`. But you will still need to make sure that the path to activate.bat is correct – Bob Nov 08 '18 at 15:24
  • 1
    The comment from @Bob also sidesteps unescaped inner quotes in the batch file. – Rich Thompson Mar 04 '19 at 03:51
  • 1
    Adapting this script to miniconda's directories turned out much harder than expected due to issues with quotes (for absolute paths it seems to need more quotes which you need some experience with cmd line to get right). Either way, this worked for me: ... REG_EXPAND_SZ /d "%windir%\System32\cmd.exe "/K" C:\ProgramData\Miniconda3\Scripts\activate.bat C:\ProgramData\Miniconda3" – JKJ May 07 '19 at 21:26
  • 3
    Windows 10 + conda 4.7, above code didn't work. We have to double the "%" before V. Otherwise everything else is the same (except perhaps changing the Anaconda path to activate.bat, depends on your install) – hyamanieu Nov 13 '19 at 10:07
7

UPDATE: The answer by Filip S. might work better on more recent versions of Anaconda.

ANOTHER UPDATE: I fixed an issue with using this launcher in the drive root (e.g. C:\ or D:\). It's very minor: some whitespace has been added to the registry entry (relevant part: "%V ") so that the trailing backslash does not confuse Windows.

Original post

I also wanted this functionality, so I made it. The key steps are outlined below, with the explanation further down.

Solution

Warning: Do not proceed unless you are comfortable editing the registry and are using a non-production system. And obviously don't run everything I tell you to, check that it's not doing anything nefarious. You don't know me!

1. Modify the Anaconda script that sets the working directory

Find the Anaconda script cwp.py (mine was in C:\Users\bdforbes\Anaconda3\) and copy it to cwp2.py in the same directory.

Modify cwp2.py to accept a target path as the second argument and change to that directory:

prefix = sys.argv[1]
cwd = sys.argv[2]
args = sys.argv[3:]

... (PATH setting code)

(REMOVE OLD LOGIC THAT CALLED os.chdir)

os.chdir(cwd)
sys.exit(subprocess.call(args, env=env))

Full code here: https://gist.github.com/bdforbes/9ef59cd01c22acefc20c5c92bd9550ae

2. Add the registry keys

In the registry, go to HKEY_CLASSES_ROOT\Directory\Background\shell\ and add a key Anaconda with default value "Open Anaconda Prompt Here", with a sub-key command with the following default value:

C:\Users\bdforbes\Anaconda3\pythonw.exe C:\Users\bdforbes\Anaconda3\cwp2.py C:\Users\bdforbes\Anaconda3 "%V " cmd.exe "/K" C:\Users\bdforbes\Anaconda3\Scripts\activate.bat C:\Users\bdforbes\Anaconda3

Add the same entries to HKEY_CLASSES_ROOT\Directory\shell\.

I've put a .reg file here, you just need to search replace bdforbes and replace it with your Windows account name. Don't run a .reg file without checking it first!

enter image description here enter image description here

3. Use your fancy new context menu item

Right click on a folder. You should see the new entry there which will let you open a new Anaconda prompt.

enter image description here

bdforbes
  • 1,486
  • 1
  • 13
  • 31
  • 2
    Instead of the .reg file, you can run a .bat file to avoid replacing the user name. I placed the code in [this gist](https://gist.github.com/thibaudruelle/eac3a11fee9ea719de0240c9eda97d8f). – Thibaud Ruelle Nov 30 '17 at 08:50
  • Seems like there has been some changes in how the Anaconda Prompt works, so the new regedit command I've found that works is `%windir%\System32\cmd.exe pushd "%V" "/K" C:\Users\username\Anaconda3\Scripts\activate.bat C:\Users\username\Anaconda3`. So the modified `cwp2.py` is not necessary anymore. – Filip S. Jul 11 '18 at 10:00
  • Thanks for letting me know, I'll check that out. What version of Anaconda are you using? – bdforbes Jul 11 '18 at 10:02
  • Not sure which parts of Anaconda are relevant, but I'm at `conda 4.5.5`. I've posted a complete reply below, but feel free to edit your answer instead :) – Filip S. Jul 11 '18 at 10:15
  • @bdforbes Can we change the current directory in Anaconda prompt to anything other than C: drive? Seems like it's not possible. Doesn't this sort of compels the user to put every script in C: ? – Sndn Aug 02 '18 at 13:46
  • @Sndn it should be possible. If you've added the context menu launcher, and you run it from a different drive, it should drop you there. Is that what you tried? – bdforbes Aug 02 '18 at 20:39
  • @Sndn actually it seems you're right, when I try the launcher on e.g. an external drive, it doesn't open anything. I'll have a look into it this weekend to see if I can fix it. – bdforbes Aug 02 '18 at 23:07
  • Hi @Sndn, did you mean can we open the prompt and _then_ change directory? Remember that in Windows, you need to change the drive explicitly like this: `D:`. That might fix your problem. However, I've found another issue that this launcher doesn't work from the drive root, e.g. from ``C:\`` or ``D:\``, because of some mangling of the arguments due to silly Windows backslashes. I'm trying to figure that out now. – bdforbes Aug 04 '18 at 10:03
  • I've now fixed the problem _I_ found, and I'm able to use this launcher fine from the drive root and from any drive. – bdforbes Aug 04 '18 at 10:20
  • @bdforbes I haven't yet added this to context menu, but I have changed the location anaconda prompt opens to my G:\ (I have two, C:\ and G:\) When I used to open it in C:, it wouldn't cd to G. Similarly now I can't cd to C:\. No problem now though since I have all my codes in G: – Sndn Aug 04 '18 at 13:55
  • Are you sure you switched the drive first? The Windows command prompt is weird in that you can't change drive letter like ``cd C:\``, you need to literally type `C:` first, without `cd`. – bdforbes Aug 04 '18 at 19:12