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

11372
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
25 Replies
SFM_TravisBott
Occasional Contributor III

I am also having trouble executing - I don't even seem to have the Python: Conda Path option when selecting settings. Should I be setting the interpreter to conda.exe rather than python.exe packaged with Pro? I also have no other environments available in C:\Users\{User}\AppData\Local\ESRI\conda\envs. 

Feeling like a bit of a numbskull here trying to get going, especially when their documentation makes it seem like it should be so simple. 

0 Kudos
Brian_Wilson
Occasional Contributor III

There are tabs on the settings page, one for "User" and one for "Workspace". When I switch to "Workspace", the "Python: Conda Path" setting disappears. Maybe you clicked on Workspace? I guess they don't want you to have a conda set different in a workspace than in your global (user) settings so they don't let us do that.

Setting the python path to conda.exe wont work. Don't do that.

If you have conda working, like at command line, you can see where your environments are stored. They can be in different places. (Anyplace, really). Each environment is a folder with Python and packages and other stuff stored in it.

So it's possible for C:\Users\{User}\AppData\Local\ESRI\conda\envs to be EMPTY and you still have envs... this is what my computer shows in  "envs directories" right now when I do "conda info"

 envs directories : C:\Program Files\ArcGIS\Pro\bin\Python\envs
                          C:\Users\bwilson\AppData\Local\ESRI\conda\envs
                          C:\Users\bwilson\Miniconda3\envs
                          C:\Users\bwilson\.conda\envs
                          C:\Users\bwilson\AppData\Local\conda\conda\envs

It's including some directories that I added by editing my .condarc config file. My .condarc has this in it. I think the other folders are added because of where I have my conda.exe installed (C:\Users\bwilson\Miniconda3) and the .conda folder my home directory (C:\Users\bwilson\.conda)

envs_dirs:
- C:/Program Files/ArcGIS/Pro/bin/Python/envs
- C:/Users/bwilson/AppData/Local/ESRI/conda/envs

0 Kudos
SFM_TravisBott
Occasional Contributor III

Thank you for the reply. Very helpful. I am not particularly savvy but was able to get mine working without really understanding what I did. The first post in this link, and the page it links to, were helpful.

I typed this below into the command prompt....

conda init powershell

 

...this succeeded in letting me actually run code, though it would still show an error at the initialization of any script. 

Then I made the settings change (in VS code, Terminal: Select Default Profile), setting the terminal to command prompt. 

Everything runs smoothly now. 

0 Kudos
AlexRobinson13
New Contributor

Hey there, 

I followed your instructions and now I am getting these messages. Any clues for me?

Windows PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.

PS C:\Users\AROBINSO> conda activate arcgispro-py3

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
If using 'conda activate' from a batch script, change your
invocation to 'CALL conda.bat activate'.

To initialize your shell, run

$ conda init <SHELL_NAME>

Currently supported shells are:
- bash
- cmd.exe
- fish
- tcsh
- xonsh
- zsh
- powershell

See 'conda init --help' for more information and options.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.

0 Kudos
cdevault_pickett
New Contributor III

@Brian_Wilson Thanks for the help back in November but I'm just now circling back to this. I've got things to the point where I receive a similar message that @AlexRobinson13 is experiencing.

PS C:\Users\cdevault> activate
PS C:\Users\cdevault> conda activate "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3"

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
If using 'conda activate' from a batch script, change your

To initialize your shell, run

    $ conda init <SHELL_NAME>

Currently supported shells are:
  - bash
  - cmd.exe
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell

See 'conda init --help' for more information and options.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.


PS C:\Users\cdevault> & "C:/Program Files/ArcGIS/Pro/bin/Python/envs/arcgispro-py3/python.exe" c:/Users/cdevault/Desktop/Print.py
test
PS C:\Users\cdevault> 

 

0 Kudos
I_AM_ERROR
Occasional Contributor

I know this isn't directly related to the question at hand, but since there are like-minded VSCode users in this thread perhaps it'll be allowed...

Is anyone successfully debugging in VS using attach to process w/ ArcGIS Pro? I know there is some documentation for Visual Studio, and i've seen some issues/discussions on GitHub related to it but I haven't managed to get it working.

0 Kudos
mfcallahan
Occasional Contributor II

Yes, that would be amazing, but everything I've read while investigating the same thing seems to indicate that it is not possible.. If I could have a workflow similar to Visual Studio ArcGIS Pro Add-in development with VS Code & Python by attaching the debugger to the running arcgispro.exe instance I would be so happy 😀

0 Kudos
I_AM_ERROR
Occasional Contributor

Just submitted an idea if anyone else wants to dogpile on it 🙂

Debug ArcGIS Pro python tools in VS Code using Att... - Esri Community

0 Kudos
mfcallahan
Occasional Contributor II

If PyCharm can do this, there's got to be a way for VS Code to do this as well 🤔 I would definitely love to see an official Esri "VS Code ArcGIS Pro Debugger extension"

0 Kudos
JasonJordan00
Occasional Contributor

After following Brian's answer above I was also able to add the following to my settings.json file and VS Code finally starts up my Pro env in the terminal by default without the conda init warning. I'm using command prompt as my default shell.

 

"python.terminal.activateEnvironment": false
"terminal.integrated.profiles.windows": {

	"PowerShell": {
		"source": "PowerShell",
		"icon": "terminal-powershell"
	},
	"Command Prompt": {
		"path": [
			"${env:windir}\\Sysnative\\cmd.exe",
			"${env:windir}\\System32\\cmd.exe"
		],
		"args": [

		"/k", 
		"C:/Program Files/ArcGIS/Pro\\bin/Python/Scripts/proenv.bat"],
		"icon": "terminal-cmd"
	},
	"Git Bash": {
		"source": "Git Bash"
	}
}

 

 

JasonJordan00_0-1669820185939.png

 

0 Kudos