10

With Anaconda installed I got a anaconda base shortcut on Windows startmanu. To open the virtualenv I created (e.g., myenv), I have to click the anaconda base and type in activate myenv in the opened cmd window.

How can I create a shortcut to get to myenv with one-click, without open-and-typing like the above?

I've tried to create a copy of the base shortcut and change its command property i.e., %windir%\System32\cmd.exe "/K" C:\Programs\anaconda3\Scripts\activate.bat C:\Programs\anaconda3\envs\myenv. It does open the myenv cmdline, but seemed lost some buildin command,like conda.

I guess I need a little bit help on Windows bat skills.

John Wang
  • 4,562
  • 9
  • 37
  • 54
  • I think you may have your quoting wrong, `%WinDir%\System32\cmd.exe /K "C:\Programs\anaconda3\Scripts\activate.bat C:\Programs\anaconda3\envs\myenv"`. Type `cmd /?` at the prompt for the usage help on that particular command. – Compo May 27 '18 at 12:24
  • @Compo, using your version I got the same results, i.e., myenv opened,but some commands(e.g., `conda`) are not in the environment. ;-( – John Wang May 27 '18 at 12:31
  • I only provided a fix to your incorrect quoting, not an answer to your question, _which would have been provided in the answer area, not the comment area_. It was only so because you have not provided sufficient information in your question area for me to confidently provide an answer. – Compo May 27 '18 at 12:37
  • Well the answer would be in the batch file itself. – Gerhard May 27 '18 at 14:10
  • Since I also use `git-bash`, I found I can put `source /c/.../activate /c/.../myenv` into my `.bashrc` file. After that when I launch `git-bash`, it goes into myenv and works fine! I'll go with `git-bash` now. – John Wang May 27 '18 at 15:10
  • It's funny that you can create a shortcut to the base environment with `%windir%\system32\cmd.exe "/K" C:\appl\Anaconda3\Scripts\activate.bat base` and that will have conda.exe in the path and if you `conda activate myenv` from there conda.exe will remain in the PATH. But if you try to directly activate the `myenv` it won't have access to conda.exe – RubenLaguna Aug 09 '18 at 11:33

7 Answers7

7

putting the comments above together in a simple batch script works flawlessly:

@echo off    
set PATH=%PATH%;C:\ProgramData\Anaconda3\Scripts
%windir%\system32\cmd.exe "/K" C:\ProgramData\Anaconda3\Scripts\activate.bat <env-name>
Jonathan
  • 94
  • 1
  • 2
  • 3
    Beautiful! Adding a `cd /d e:\my-working-dir` before the last line make it totally perfect. Thanks man. – John Wang Nov 19 '18 at 00:57
  • 1
    This isn't a "shortcut" – BirdLaw Feb 05 '19 at 19:41
  • Create a shortcut to cmd, set Target to %windir%\system32\cmd.exe "/K" %USERPROFILE%\Miniconda3\Scripts\activate.bat base (I use Miniconda3). Double click to open a cmd prompt w/the env already activated. – Russ Mar 12 '21 at 16:45
3

The following works for me. The only change is that the parameter to activate.bat is simply the env name (not the full path) as you would normally type it after an activate command. Your quotes were fine, BTW. For instance:

%windir%\system32\cmd.exe "/K" C:\ProgramData\Anaconda3\Scripts\activate.bat myenv
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Gronk
  • 149
  • 2
  • 9
  • This activates the environment, yes, but the conda.exe will not not be there (unless you already added C:\ProgramData\Anaconda3\Scripts to you global PATH which is not the recommended option when installing Anaconda – RubenLaguna Aug 09 '18 at 11:28
  • `conda.bat` which is called by `activate.bat` already takes care of the PATH. – sdittmar Jun 18 '20 at 16:40
1

Mine automatically created a shortcut for Spyder with this format:

C:\Anaconda3\pythonw.exe C:\Anaconda3\cwp.py C:\Anaconda3\envs\py36 C:\Anaconda3\envs\py36\pythonw.exe C:\Anaconda3\envs\py36\Scripts\spyder-script.py
endolith
  • 25,479
  • 34
  • 128
  • 192
  • This is a great option for non command line apps. I just used is as `C:\Miniconda3\pythonw.exe C:\Miniconda3\cwp.py C:\Miniconda3\envs\env0 app.exe` . Replace env0 and app.exe with your respective values and no cmd.exe will be shown – neurosock Aug 17 '21 at 13:11
  • 1
    this requires short paths, as the full string must fit within 259 characters. – Mike T Dec 07 '22 at 23:17
1

Before creating new environment you can specify:

conda config --set shortcuts true

After that, you can see shortcuts for your new environment.

illuminato
  • 1,057
  • 1
  • 11
  • 33
1

For miniconda it's:

@echo off    
set PATH=%PATH%;C:\ProgramData\miniconda3\Scripts
%windir%\system32\cmd.exe "/K" C:\ProgramData\miniconda3\Scripts\activate.bat video

Was gonna leave it as a comment on Jonathan's, but I'm new ><.

0

I use ConEmu terminal, with traditional cmd.exe shell. Here's what my shortcut to MiniConda3 looks like: C:\Users\nmz787\Downloads\conemu_22_08_07\ConEmu64.exe -run {Shells::cmd} & C:\Users\nmz787\Miniconda3\condabin\conda.bat activate

nmz787
  • 1,960
  • 1
  • 21
  • 35
-2

You can get around this by installing the conda package into the environment that you want to activate.

From an Anaconda Prompt (from where conda is already accessible):

conda install -n myenv conda

Then you can create a windows shortcut with target %windir%\system32\cmd.exe "/K" C:\appl\Anaconda3\Scripts\activate.bat myenv

This is suboptimal as it pollutes your environment with the conda dependencies, and I wouldn't recommend it.

The other alternative is to add the C:\Anaconda3\Scripts directory to the PATH environment variable.

RubenLaguna
  • 21,435
  • 13
  • 113
  • 151