Adding "Open Anaconda Prompt here" to context menu (Windows)
Asked Answered
P

4

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.

Pennell answered 10/10, 2017 at 9:12 Comment(0)
M
21
  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.
Millihenry answered 25/2, 2021 at 0:38 Comment(1)
This kind of answers makes me realize I know nothing about windows... Thanks for the helpful answer !!Fossa
G
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"
Guttery answered 11/7, 2018 at 10:14 Comment(7)
Nice! Thanks for writing that up. I'll add a note in my answer.Brower
Filip, you might want to check if the registry correctly launches the prompt from the drive root, as per the update to my answer.Brower
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.Whitworth
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 correctOntologism
The comment from @Ontologism also sidesteps unescaped inner quotes in the batch file.Salted
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"Pyxidium
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)Aldos
B
9

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

Brower answered 18/10, 2017 at 5:50 Comment(11)
Instead of the .reg file, you can run a .bat file to avoid replacing the user name. I placed the code in this gist.Mcguire
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.Guttery
Thanks for letting me know, I'll check that out. What version of Anaconda are you using?Brower
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 :)Guttery
@Brower 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: ?Osmious
@Osmious 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?Brower
@Osmious 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.Brower
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.Brower
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.Brower
@Brower 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:Osmious
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.Brower
B
0

kdebugging's answer worked for me with the following tip for finding the command value:

I found the correct location for the activate.bat file from the start menu shortcut. I am using miniconda, so the value for my key ended up being

cmd.exe /K C:\ProgramData\miniconda3\Scripts\activate.bat

I found this by:

  1. Navigate in File Explorer to C:\ProgramData\Microsoft\Windows\Start Menu\Programs then click into directory of your Anaconda Installation. Mine is Miniconda3 (64-bit))
  2. Right click the shortcut that corresponds to the program you want to launch, and click Properties. My shortcut is Anaconda Prompt (miniconda3)
  3. Copy the path for activate.bat in the Target field. Mine was C:\ProgramData\miniconda3\Scripts\activate.bat and use that in the command value for kdebugging's answer. Note that there are other elements in the shortcut that will not be usable in the registry value, so be careful what you copy.

Hope this helps!

Blennioid answered 11/12, 2023 at 16:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.