conda / menuinst

Cross platform menu item installation
https://conda.github.io/menuinst/
BSD 3-Clause "New" or "Revised" License
38 stars 44 forks source link

Easily creating shortcuts with the powershell_shortcut_miniconda and console_shortcut_miniconda packages, and can this be replicated with menuinst ? #246

Open jypeter opened 3 months ago

jypeter commented 3 months ago

Checklist

What happened?

My problem

I have installed miniconda3 on Windows 10 (and on several Linux platforms) and thoroughly documented all the steps, so that our students can understand and replicate this, and can then create their own clean environments, without altering the base environment

After installing miniconda3 on Windows, you end up with a Miniconda3 (64-bit) Windows menu, a shortcut to open an Anaconda Powershell Prompt and one to open an Anaconda Prompt, both with the base environment activated

I had no idea where these convenient shortcuts were coming from, and I wanted to find a clean way to create direct shortcuts to new (non-base) environments, on Windows. So that students can directly start a specific environment, without having to type conda activate env ). You've got to make things easy for new users, if you want to get them past notebooks...

What I have tried

I have relatively quickly found out about menuinst on Google and played a bit (a lot, too much, actually) with in order to understand more about the json config files. I figured that generating and distributing a good json config file to our students would do the trick

Unfortunately, I have read and re-read the menuinst documentation (and filled some issues along the way) and got the impression I was running in circles and not getting anywhere near my easy environment initialization goal.

The documentation is not very clear/detailed about using non-base environments. The closest thing I found was Specifying different shortcut names for base and non-base environments. But this seemed like a very convoluted way to get thins done, and I did not really get it working. Maybe I'll fill an issue about that. For selecting a non based environment, I was more expecting something like : use a parameter in the json file to specify the environment name, and then create the shortcuts by calling menuinst in the base environment

I also tried to do some reverse engineering by comparing the content of my json files to the generated .lnk and .bat files but this only got me some severe headaches. I'm mostly a Linux user and I know next to nothing about .bat files and powershell syntax

What seems to work (without using menuinst explicitly...)

I tried to understand where the Windows shortcuts (to base) that appeared when I installed miniconda3 were coming from, and I eventually found out that they were provided by the powershell_shortcut_miniconda and console_shortcut_miniconda packages bundled with miniconda3 !

It is actually trivial to install or uninstall either the powershell or the console shortcuts (or both) by just installing or removing the powershell_shortcut_miniconda and console_shortcut_miniconda packages

In the example below, I easily create a Windows menu shortcut to my spyder_test environment, from the base environment

(base) PS C:\Users\jypeter\menuinst_test> conda list menuinst
# packages in environment at C:\Utils\miniconda3_2024-03:
#
# Name                    Version                   Build  Channel
menuinst                  2.1.1           py312h275cf98_0    conda-forge

(base) PS C:\Users\jypeter\menuinst_test> conda install -n spyder_test powershell_shortcut_miniconda
Channels:
 - conda-forge
 - defaults
Platform: win-64
Collecting package metadata (repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: C:\Utils\miniconda3_2024-03\envs\spyder_test

  added / updated specs:
    - powershell_shortcut_miniconda

The following NEW packages will be INSTALLED:

  powershell_shortc~ pkgs/main/win-64::powershell_shortcut_miniconda-0.0.1-haa95532_2

Proceed ([y]/n)? y

After testing the Windows menu shortcut, I can also easily remove the shortcut, still from the base environment

(base) PS C:\Users\jypeter\menuinst_test> conda remove -n spyder_test powershell_shortcut_miniconda Channels:
 [...]
The following packages will be REMOVED:

  powershell_shortcut_miniconda-0.0.1-haa95532_2

Proceed ([y]/n)? y

I can also create and remove the shortcuts directly from the non-base environment. Note that menuinst is not installed in that environment, but I guess that if conda can be called from this non-base environment, it can also use menuinst from the base environment

(base) PS C:\Users\jypeter\menuinst_test> conda activate spyder_test

(spyder_test) PS C:\Users\jypeter\menuinst_test> conda list menuinst
# packages in environment at C:\Utils\miniconda3_2024-03\envs\spyder_test:
#

# Name                    Version                   Build  Channel

(spyder_test) PS C:\Users\jypeter\menuinst_test> where.exe conda
C:\Utils\miniconda3_2024-03\condabin\conda.bat

(spyder_test) PS C:\Users\jypeter\menuinst_test> where.exe python
C:\Utils\miniconda3_2024-03\envs\spyder_test\python.exe
C:\Users\jypeter\AppData\Local\Microsoft\WindowsApps\python.exe

(spyder_test) PS C:\Users\jypeter\menuinst_test> conda install powershell_shortcut_miniconda
[...]
## Package Plan ##

  environment location: C:\Utils\miniconda3_2024-03\envs\spyder_test
  added / updated specs:
    - powershell_shortcut_miniconda

The following NEW packages will be INSTALLED:

  powershell_shortc~ pkgs/main/win-64::powershell_shortcut_miniconda-0.0.1-haa95532_2

Proceed ([y]/n)? y
[...]

(spyder_test) PS C:\Users\jypeter\menuinst_test> conda remove powershell_shortcut_miniconda
Channels:
 - conda-forge
 - defaults
Platform: win-64
Collecting package metadata (repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: C:\Utils\miniconda3_2024-03\envs\spyder_test

  removed specs:
    - powershell_shortcut_miniconda

The following packages will be REMOVED:

  powershell_shortcut_miniconda-0.0.1-haa95532_2

Proceed ([y]/n)? y

How do 'powershell_shortcut_miniconda' and 'console_shortcut_miniconda' work ?

I'm glad I found an easy solution to my initial problem, but this raised other questions

I have found the following json files in the packages

C:\Utils\miniconda3_2024-03\pkgs\console_shortcut_miniconda-0.1.1-haa95532_2\Menu\console_shortcut.json

{
    "menu_name": "Miniconda${PY_VER} ${PLATFORM}",
    "menu_items":
    [
        {
            "name": "Anaconda Prompt (${DISTRIBUTION_NAME})",
            "system": "%windir%\\System32\\cmd.exe",
            "scriptarguments": ["/K", "${ROOT_PREFIX}\\Scripts\\activate.bat", "${PREFIX}"],
            "icon": "${MENU_DIR}/anaconda_console.ico"
        }
    ]
}

C:\Utils\miniconda3_2024-03\pkgs\powershell_shortcut_miniconda-0.0.1-haa95532_2\Menu\powershell_shortcut.json

{
    "menu_name": "Miniconda${PY_VER} ${PLATFORM}",
    "menu_items":
    [
        {
            "name": "Anaconda Powershell Prompt (${DISTRIBUTION_NAME})",
            "system": "%windir%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
            "scriptarguments": ["-ExecutionPolicy", "ByPass", "-NoExit", "-Command", "& '${ROOT_PREFIX}\\shell\\condabin\\conda-hook.ps1' ; conda activate '${PREFIX}' "],
            "icon": "${MENU_DIR}/anaconda_powershell_console.ico"
        }
    ]
}

My new questions:

My other requirement: Windows Terminal profiles

I would also like to easily create Windows Terminal profiles that would open a Windows terminal using the base or a non-base environment.

I understand that this can theoretically be achieved with the terminal_profile option, and has been covered by #196 . Can somebody provide a detailed example for both cases (base and non-base). I have already spent way tp much time on this and I need to move on to creating actual complex (and non-base!) environments

There are lots of online tutorials to do this, but they may be obsolete or not safe. And I'd like a clean way to do this (distributing a json file would be nice)

Thanks!

Additional Context

No response

marcoesters commented 3 months ago

The shortcuts you mention still use the menuinst v1 syntax. The automatic addition of the environment name has been removed in v2 (see #180 about how I tried to re-introduce it). Here is an example recipe for a menuinst v1 shortcut: https://github.com/AnacondaRecipes/console_shortcut-feedstock/tree/main/recipe

There is an easier way to do it if you don't mind the word base in your shortcut name by using the {{ ENV_NAME }} placeholder. So, you would see something like Anaconda Prompt (base) and Anaconda Prompt (environment) instead of Anaconda Prompt and Anaconda Prompt (environment).

Version 2 of menuinst can still install v1 shortcuts: https://github.com/conda/menuinst/tree/main/menuinst/_legacy

The placeholders for v1 are defined here: https://github.com/conda/menuinst/blob/main/menuinst/_legacy/win32.py#L103-L119

The syntax for placeholders has changed from v1 to v2, as you noticed, from ${...} to {{ ... }}.

You can add a Windows terminal shortcut by adding the terminal_profile option, but it requires v2. This will create a terminal profile where command is being executed. I am working on something like that for the console shortcuts, but the package isn't fully fleshed out yet. Note that profiles with the same name are overwritten, so you need to include the {{ ENV_NAME }} placeholder in the terminal_profile name if you want something for base and non-base environments.

Please let me know if I missed anything.