Select to view content in your preferred language

Trying to configure ArcGIS Pro/ArcPy + VS Code, but can't activate conda environment - where am I going wrong?

12439
25
Jump to solution
10-11-2022 06:59 AM
mfcallahan
Occasional Contributor II

I'm attempting to configure VS Code for ArcPy/ArcGIS Pro development, and things have been going ok so far, however I have run into an issue I am not sure how to resolve. I'm also curious to hear how other in the community have configured VS Code - I want to make sure I'm not way off base. Here is how I have configured VS Code:

I first cloned a Python environment from the ArcGIS Pro Package Manager, and then set the location of the Python interpreter in VS Code to the "python.exe" in my cloned env (which for me, was in this folder: %LocalAppData%\ESRI\conda\envs\my-cloned-environment\python.exe).

select_interpreter

 And that was actually all I needed to do in order to run and debug a script. I am able to create a new Python debugging configuration in VS Code, set breakpoints step through the code, and get IntelliSense on all ArcPy modules & methods. However, there is still something I am not fully understanding, I think..

On VS Code Terminal launch, I get the following error:

not_activated

 That looks to me like the system PATH environment variable has not been configured for Conda, and Powershell does not recognize the conda command. The Conda docs actually recommend not modifying the PATH env var, but if I do, I can resolve the error above, and the conda command is recognized.

However, that yields another error, "CommandNotFoundError: Your shell has not been properly configured to use ‘conda activate’.”

working1-1

 Looking at the error message, I see that it is saying to run command conda init powershell, however, this also yields an error an I am stuck at this point, unsure how to proceed. Running VS Code as admin did not resolve the issue.

conda_2

 To further compound my confusion, I came across this in the Esri docs saying "propy.bat" or "proenv.bat" should be used, but I don't know how I would integrate with VS Code:

https://pro.arcgis.com/en/pro-app/latest/arcpy/get-started/using-conda-with-arcgis-pro.htm

I'm curious to hear how other VS Code users have their local development environment configured, if you ran into any of these issues, how you resolved them, etc. Or am I just completely way off base in my methods and attempts to get VS Code configured lol??

0 Kudos
1 Solution

Accepted Solutions
Brian_Wilson
Regular Contributor II

VSCODE understands Conda, but you have to tell it how to find conda.

Edit->Preferences

Search for conda and you should see "Python: Conda Path"

If you still have the Python path set you should probably clear that.

I set the "Conda path" to C:\Program Files\ArcGIS\Pro\bin\Python\Scripts so that VSCode can find conda.

After setting the Conda path, I restart VSCode and I note in the task bar I can see "Discovering Python environments" flash by and then do F1->Python: Select Interpreter.

Now it shows me the list of Conda environments, which on my computer looks like this.

BrianWilson7_0-1665504547969.png

 

It found both my Esri environments and one(s) set up using "miniconda" which I have separately installed (you don't need miniconda) Note I installed "miniconda" because it works better for me from Bash shells so it finds that version of Python as well. (Having both the Esri conda and minconda installed works fine.)

Next I add the Scripts folder to my environment PATH settings,

BrianWilson7_1-1665505306028.png

Now I can restart VSCode, open a Powershell in a Terminal and run "conda info". Output looks like this on my computer:

PS C:\Users\bwilson\Documents\source\arcgis_tools> conda info

     active environment : arcgispro-py3
    active env location : C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3
       user config file : C:\Users\bwilson\.condarc
 populated config files : C:\Program Files\ArcGIS\Pro\bin\Python\.condarc
                          C:\Users\bwilson\.condarc
          conda version : 4.9.2
    conda-build version : not installed
         python version : 3.7.11.final.0
       virtual packages : __cuda=11.6=0
                          __win=0=0
                          __archspec=1=x86_64
       base environment : C:\Program Files\ArcGIS\Pro\bin\Python  (read only)
           channel URLs : https://conda.anaconda.org/esri/win-64
                          https://conda.anaconda.org/esri/noarch
                          https://conda.anaconda.org/conda-forge/win-64
                          https://conda.anaconda.org/conda-forge/noarch
                          https://repo.anaconda.com/pkgs/main/win-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/win-64
                          https://repo.anaconda.com/pkgs/r/noarch
                          https://repo.anaconda.com/pkgs/msys2/win-64
                          https://repo.anaconda.com/pkgs/msys2/noarch
          package cache : C:\Users\bwilson\AppData\Local\ESRI\conda\pkgs
                          C:\Program Files\ArcGIS\Pro\bin\Python\pkgs
                          C:\Users\bwilson\.conda\pkgs
       envs directories : C:\Users\bwilson\AppData\Local\ESRI\conda\envs
                          C:\Program Files\ArcGIS\Pro\bin\Python\envs
                          C:\Users\bwilson\.conda\envs
               platform : win-64
             user-agent : conda/4.9.2 requests/2.25.1 CPython/3.7.11 Windows/10 Windows/10.0.22621
          administrator : False
             netrc file : None
           offline mode : False

 

Almost there, when I try to do the normal activate for example "conda activate arcgispro29" it complains that I have not done "conda init" yet. If I do that, I restart VSCODE again, and find out it screws up my startup file. To recover I delete the startup file it created, C:\Users\bwilson\Documents\WindowsPowerShell\profile.ps1

Ignore the "conda init" thing by using the scripts that Esri installed, first run "proenv" then "activate". 

PS C:\Users\bwilson\Documents\source\arcgis_tools> proenv

(arcgispro-py3) C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3>activate arcgispro29

(arcgispro29) C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3>conda info
(arcgispro29) C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3>conda info
     active environment : arcgispro29
    active env location : C:\Users\bwilson\.conda\envs\arcgispro29
(etc etc)

 

so that seems to get us where you need to be. You can use Python from the VSCode debugger (F5) and you can also activate an environment in a shell if you want; you need that to install additional packages and manage conda from the command line. Like, to create new environments.

I could also tell you how I set up miniconda so that it works in Bash but you did not ask that.... I avoid PowerShell as much as possible. Personal preference.

 

View solution in original post

25 Replies
EarlMedina
Esri Regular Contributor

I've gotten this to work like so:

Edit the VS Code settings.json file located here on Windows: C:\Users\[YOUR_USER]\AppData\Roaming\Code\User\settings.json

From there, update the line with "python.pythonPath" to 

"python.pythonPath": "C:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\Scripts\\propy.bat"​

 

 

If for some reason that does not work, I have gotten it to work by pointing to the desired cloned environment directly like this:

"python.pythonPath": "C:\\Users\\[YOUR_USER]\\AppData\\Local\ESRI\\conda\\envs\\[NAME_OF_ENV]\\python.exe"​

 

Hope this helps!

0 Kudos
Brian_Wilson
Regular Contributor II

VSCODE understands Conda, but you have to tell it how to find conda.

Edit->Preferences

Search for conda and you should see "Python: Conda Path"

If you still have the Python path set you should probably clear that.

I set the "Conda path" to C:\Program Files\ArcGIS\Pro\bin\Python\Scripts so that VSCode can find conda.

After setting the Conda path, I restart VSCode and I note in the task bar I can see "Discovering Python environments" flash by and then do F1->Python: Select Interpreter.

Now it shows me the list of Conda environments, which on my computer looks like this.

BrianWilson7_0-1665504547969.png

 

It found both my Esri environments and one(s) set up using "miniconda" which I have separately installed (you don't need miniconda) Note I installed "miniconda" because it works better for me from Bash shells so it finds that version of Python as well. (Having both the Esri conda and minconda installed works fine.)

Next I add the Scripts folder to my environment PATH settings,

BrianWilson7_1-1665505306028.png

Now I can restart VSCode, open a Powershell in a Terminal and run "conda info". Output looks like this on my computer:

PS C:\Users\bwilson\Documents\source\arcgis_tools> conda info

     active environment : arcgispro-py3
    active env location : C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3
       user config file : C:\Users\bwilson\.condarc
 populated config files : C:\Program Files\ArcGIS\Pro\bin\Python\.condarc
                          C:\Users\bwilson\.condarc
          conda version : 4.9.2
    conda-build version : not installed
         python version : 3.7.11.final.0
       virtual packages : __cuda=11.6=0
                          __win=0=0
                          __archspec=1=x86_64
       base environment : C:\Program Files\ArcGIS\Pro\bin\Python  (read only)
           channel URLs : https://conda.anaconda.org/esri/win-64
                          https://conda.anaconda.org/esri/noarch
                          https://conda.anaconda.org/conda-forge/win-64
                          https://conda.anaconda.org/conda-forge/noarch
                          https://repo.anaconda.com/pkgs/main/win-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/win-64
                          https://repo.anaconda.com/pkgs/r/noarch
                          https://repo.anaconda.com/pkgs/msys2/win-64
                          https://repo.anaconda.com/pkgs/msys2/noarch
          package cache : C:\Users\bwilson\AppData\Local\ESRI\conda\pkgs
                          C:\Program Files\ArcGIS\Pro\bin\Python\pkgs
                          C:\Users\bwilson\.conda\pkgs
       envs directories : C:\Users\bwilson\AppData\Local\ESRI\conda\envs
                          C:\Program Files\ArcGIS\Pro\bin\Python\envs
                          C:\Users\bwilson\.conda\envs
               platform : win-64
             user-agent : conda/4.9.2 requests/2.25.1 CPython/3.7.11 Windows/10 Windows/10.0.22621
          administrator : False
             netrc file : None
           offline mode : False

 

Almost there, when I try to do the normal activate for example "conda activate arcgispro29" it complains that I have not done "conda init" yet. If I do that, I restart VSCODE again, and find out it screws up my startup file. To recover I delete the startup file it created, C:\Users\bwilson\Documents\WindowsPowerShell\profile.ps1

Ignore the "conda init" thing by using the scripts that Esri installed, first run "proenv" then "activate". 

PS C:\Users\bwilson\Documents\source\arcgis_tools> proenv

(arcgispro-py3) C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3>activate arcgispro29

(arcgispro29) C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3>conda info
(arcgispro29) C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3>conda info
     active environment : arcgispro29
    active env location : C:\Users\bwilson\.conda\envs\arcgispro29
(etc etc)

 

so that seems to get us where you need to be. You can use Python from the VSCode debugger (F5) and you can also activate an environment in a shell if you want; you need that to install additional packages and manage conda from the command line. Like, to create new environments.

I could also tell you how I set up miniconda so that it works in Bash but you did not ask that.... I avoid PowerShell as much as possible. Personal preference.

 

mfcallahan
Occasional Contributor II

You da man, @Brian_Wilson - thanks for replying, this worked for me! I had a very frustrating call with an Esri tech support representative for an hour trying to work thru this, but it ultimately ended it with a "we don't support this" answer and the ticket being closed 😑 I probably wasn't communicating things effectively to support, but Pro/ArcPy/Conda and actually using them is something that is so under-documented that even Esri's staff can't answer questions about how this type of stuff is configured..

Brian_Wilson
Regular Contributor II

Hey you're welcome!

VSCode is the only Microsoft tool I can say I am actually fond of. (Well-- I did like MSC a lot but that was in 1985.)

 

mfcallahan
Occasional Contributor II

Ha! Well, I'll give some credit where credit is due..not a lot of software companies have made a game that still works flawlessly 32 years later (on an operating system that didn't even exist in 1990!)

sol1.png

 MS Solitatire (1990) running on Pop!_OS 22.04 (2022)

0 Kudos
cdevault_pickett
New Contributor III

Thank you for this, I've been struggling with this annoyance for a while. I feel like I am so close, but I am still getting the error the original poster had where the term conda is not recognized after following your directions. I was able to set the Conda path and add that path as a system environment variable. But I still cannot run conda init, I get the same error still. Any other ideas? I'm using the standard python installation from Pro 2.9 (no clones or anything). 

Brian_Wilson
Regular Contributor II

Does VSCODE see your conda environments? You should be able to see them if you do F1->Python: Select Interpreter

vscode.png

Mine look different probably because I have a separate install of Miniconda too.

I don't have much use for PowerShell, I use bash most of the time. But I find Powershell can only find conda if I run the proenv script first.

 

 

0 Kudos
cdevault_pickett
New Contributor III

Thanks for the reply, I think it has something to do with permissions or something on my work computer. I followed the steps exactly on my personal computer and it works as expected. If I figure it out I'll reply here.

0 Kudos
Brian_Wilson
Regular Contributor II

I have a hard time letting go of problems before they are solved. 🙂

Back when the ArcGIS Pro version of conda was very out of date, I started using Miniconda in parallel with the ArcGIS Conda. Since then I just find it works better for me to use both.

From the installer page: "On Windows, macOS, and Linux, it is best to install Miniconda for the local user, which does not require administrator permissions and is the most robust type of installation."

So for example right now I have the ArcGIS version, not on my path so typing entire path,

$ /c/Program\ Files/ArcGIS/Pro/bin/Python/Scripts/conda info

     active environment : C:\tools\miniconda3\envs\arcgis-example
    active env location : C:\tools\miniconda3\envs\arcgis-example
            shell level : 0
       user config file : C:\Users\bwilson\.condarc
 populated config files : C:\Program Files\ArcGIS\Pro\bin\Python\.condarc
                          C:\Users\bwilson\.condarc
          conda version : 4.9.2

and the Miniconda version, on my shell path so I can type directly "conda"

conda activate arcgis-example
(arcgis-example)
bwilson@04-2288 MINGW64 /c/Users/bwilson/.conda
$ conda info

     active environment : arcgis-example
    active env location : C:\tools\miniconda3\envs\arcgis-example
            shell level : 1
       user config file : C:\Users\bwilson\.condarc
 populated config files : C:\Users\bwilson\.condarc
          conda version : 4.12.0

 

And I set conda path in VSCODE to point at the miniconda version