Enhance isolated conda installation to support conda init

2162
9
05-31-2022 12:04 AM
Status: Implemented
Labels (1)
Luke_Pinner
MVP Regular Contributor

The corporate environment I work in has disabled cmd.exe and running of .bat & .cmd files as part of their IT Security policy and we are unable to install a separate miniconda.  We do have access to powershell and Visual Studio Code to run python scripts.

As of Pro 2.8, the isolated conda installation does not support conda init (ref.).  This means we can not activate conda envs in powershell or VS Code and therefore can't run scripts that use additional packages installed into clones (that rely on a properly activated environment).

Please enhance the ArcGIS Pro isolated conda installation to support conda init.

9 Comments
omdaniel

@Luke_Pinner I also work for an agency that disables cmd.exe. I have spent an inordinate amount of time thinking about this issue. My targeted conclusion is that “conda init” is the wrong approach to providing powershell integration. My recommendation is that ESRI should repurpose conda-hook.ps1 and Conda.psm1 (ships with Anacona/miniconda) and modify these scripts to work with conda build that comes with ArcGIS Pro. See this article from last year that talks about using the conda-hook.ps1 script to initialize conda and activate arcgispro-py3 environment (Setting up Windows Terminal to launch an ArcGIS Pro Python prompt - Nithiya Streethran). The procedure in the link also requires having the full Anaconda or miniconda installation alongside ArcGIS Pro and uses their conda-hook.ps1 to activate acrgispro-py3. I have adapted this solution to work with VS Code and I haven’t seen any other post about this solution. Adding the following to your user settings.json

 

    "terminal.integrated.profiles.windows": {
      "ArcGIS Pro": {
        "path": "powershell.exe",
        "args": ["-ExecutionPolicy","ByPass","-NoExit","-Command","& 'C:\\Program Files\\Anaconda3\\shell\\condabin\\conda-hook.ps1'"]
      }
    },
    "terminal.integrated.profiles.windows": {
      "Anaconda": {
        "path": "powershell.exe",
        "args": ["-ExecutionPolicy","ByPass","-NoExit","-Command","& 'C:\\Program Files\\Anaconda3\\shell\\condabin\\conda-hook.ps1'"]
      }
    },

 

Then for the individual project settings.json you can add…

 

"terminal.integrated.defaultProfile.windows": "ArcGIS Pro",

 

I find this approach to be better than “conda init” for several reasons: “conda init” makes every instance of powershell conda aware (which some may not want); it tethers powershell to the version of conda that ran “conda init” and this may cause issues if you are using both ArcGIS Pro and Anaconda.

If ESRI provides a script similar to conda-hook.ps1 / Conda.psm1  (ArcGIS Pro 3.0 please!) it would allow the end user to launch separate shells either tethered to the conda that comes with ArcGIS Pro or Anaconda and they wouldn’t mangle one another’s shell environment.

Luke_Pinner

@omdaniel interesting, can you provide the link to the article? You just linked to the conda repo.

HannesZiegler

Thank you for the insight @omdaniel, I've forwarded this potential alternative solution to the rest of the team for consideration.

omdaniel

@Luke_Pinner I corrected the link in my original post, it now goes to the article, also I can go into more detail on launching arcgispro-py3 shells in a standalone terminal and from within VS Code

omdaniel

@HannesZiegler Thank you, I can provide more detail if anyone wants to dive into this deeper. I have hacked the conda-hook.ps1 & Conda.psm1 to work with ArcGIS Pro even if someone doesn't have Anaconda/miniconda but my quick fix would need some work if it were incorparted into a future ArcGIS Pro. The big advantage of this proposal is that the conda that comes with ArcGIS Pro and Anaconda/miniconda can co-exist without stepping on each other; whereas, "conda init" hijacks' every instance of powershell, locking it the version of conda that ran "conda init"

There are a number of stackoverflow questions on this topic and if you filter the issues on microsoft/vscode-python github by "powershell conda activate" (Issues · microsoft/vscode-python · GitHub) you will see that most of the response direct people towards a "conda init" approach; although, a couple of stackoverflow responses and some the the issue tracker responses suggest the same solution I suggested above. The point being that most people who go searching for a solution to this will first find what I believe to be a non-optimal solution.

Seeing that ESRI and Anaconda recently announced a partnership (Anaconda Announces Collaboration with Esri) I assume proper solution can be found perhaps in collabartion with the maintainers of microsoft/vscode-python. 

Luke_Pinner

Thanks for updating the link @omdaniel .  I also tried hacking the conda-hook.ps1 to work with ArcGIS Pro conda and it seems to work ok.  I didn't bother setting up the integrated terminal in VS Code, just added the call to conda-hook.ps1 in my profile.ps1. I'm aware doing that also "hijacks" every instance of powershell, but that's what I want.

 

Now to find a way around conda activate dot sourcing etc/conda/{activate|deactivate}.d scripts and failing because it's running in constrained language mode...

HannesZiegler
Status changed to: In Product Plan

This will be part of work for compatibility with our activation scripts after upgrading conda.

Luke_Pinner

@HannesZiegler thankyou. This will make the lives of powershell conda users much easier.

AmeliaBradshaw
Status changed to: Implemented

This Idea has been implemented in ArcGIS Pro 3.1. Please see the What's New documentation for more new features in Pro 3.1.

The Ideas in ArcGIS Pro 3.1 blog will be wrapped up soon, highlighting all Ideas implemented in this release, including this one. Once complete, I will add the link to this comment.