Visual Studio Code: Difference between revisions

From Wildsong
Jump to navigationJump to search
Brian Wilson (talk | contribs)
Brian Wilson (talk | contribs)
Line 36: Line 36:
and packages. Using .condarc works from shells but I found I needed an environment variable
and packages. Using .condarc works from shells but I found I needed an environment variable
CONDARC="J:/.condarc" too
CONDARC="J:/.condarc" too


  channels:
  channels:
Line 49: Line 48:
   - C:/Program Files/ArcGIS/Pro/bin/Python/pkgs
   - C:/Program Files/ArcGIS/Pro/bin/Python/pkgs
  changeps1: True
  changeps1: True
Confirm it's really reading your .condarc file with "conda info".
    active environment : arctic
    active env location : C:\Users\bwilson\AppData\Local\ESRI\conda\envs\arctic
            shell level : 2
      user config file : J:\.condarc
populated config files : C:\Program Files\ArcGIS\Pro\bin\Python\.condarc
                          J:\.condarc
          conda version : 4.8.3
    conda-build version : not installed
        python version : 3.6.10.final.0
      virtual packages : __cuda=10.1
      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
      envs directories : C:\Users\bwilson\AppData\Local\ESRI\conda\envs
                          J:\.conda\envs
                          C:\Program Files\ArcGIS\Pro\bin\Python\envs
                          C:\Users\bwilson\AppData\Local\conda\conda\envs
              platform : win-64
            user-agent : conda/4.8.3 requests/2.23.0 CPython/3.6.10 Windows/10 Windows/10.0.19041
          administrator : False
            netrc file : None
          offline mode : False


=== Using clone inside ArcGIS Pro fails ===
=== Using clone inside ArcGIS Pro fails ===

Revision as of 17:16, 29 June 2020

where does it ever end? Komodo -> Microsoft Visual Studio -> Atom -> Visual Studio Code

I am now learning Visual Studio Code

I am currently using VSC to edit Python to create Geoprocessing scripts that I can run in Docker containers.

Settings

The Nightmare of Conda and ArcGIS Pro

You have ArcGIS Pro 2.5 installed which installed the Conda environment for you but it's really old 4.3.x.

Open a PowerShell as administrator Run this command

conda install -n root conda

That should fix it. When you do a "conda --version" you should see the right thing, "conda 4.8.3".

Now set your Bash environment. In a bash shell do this to update .bash_profile and the restart the shell.

conda init bash

It says it modified these files.

modified      C:\Program Files\ArcGIS\Pro\bin\Python\Scripts\activate
modified      C:\Program Files\ArcGIS\Pro\bin\Python\Scripts\deactivate
modified      C:\Program Files\ArcGIS\Pro\bin\Python\etc\profile.d\conda.sh
modified      C:\Program Files\ArcGIS\Pro\bin\Python\etc\fish\conf.d\conda.fish
modified      C:\Program Files\ArcGIS\Pro\bin\Python\shell\condabin\conda-hook.ps1
modified      C:\Program Files\ArcGIS\Pro\bin\Python\etc\profile.d\conda.csh
modified      J:\.bash_profile

My .condarc file now looks like this

There are many problems fixed here including allowing a normal user to maintain environments and packages. Using .condarc works from shells but I found I needed an environment variable CONDARC="J:/.condarc" too

channels:
  - esri
  - conda-forge
  - defaults
ssl_verify: true
envs_dirs:
  - ${WINHOME}/AppData/Local/ESRI/conda/envs
pkgs_dirs:
  - ${WINHOME}/AppData/Local/ESRI/conda/pkgs
  - C:/Program Files/ArcGIS/Pro/bin/Python/pkgs
changeps1: True

Confirm it's really reading your .condarc file with "conda info".

    active environment : arctic
   active env location : C:\Users\bwilson\AppData\Local\ESRI\conda\envs\arctic
           shell level : 2
      user config file : J:\.condarc
populated config files : C:\Program Files\ArcGIS\Pro\bin\Python\.condarc
                         J:\.condarc
         conda version : 4.8.3
   conda-build version : not installed
        python version : 3.6.10.final.0
      virtual packages : __cuda=10.1
      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
      envs directories : C:\Users\bwilson\AppData\Local\ESRI\conda\envs
                         J:\.conda\envs
                         C:\Program Files\ArcGIS\Pro\bin\Python\envs
                         C:\Users\bwilson\AppData\Local\conda\conda\envs
              platform : win-64
            user-agent : conda/4.8.3 requests/2.23.0 CPython/3.6.10 Windows/10 Windows/10.0.19041
         administrator : False
            netrc file : None
          offline mode : False

Using clone inside ArcGIS Pro fails

It's a permissions problem, like most Windows developers, ESRI people do not understand permissions.

conda

Autopep error

Install that autopep thing for syntax highlighting.

conda install autopep8

It's not finding Python!!

Here is a doc telling where it looks.

Put the commonly used variables into your user settings, File -> Preferences -> Settings -> User -> Extensions -> Python -> Conda Path

"C:/Program Files/ArcGIS/Pro/bin/Python/Scripts/conda.exe"

Reload VSCode. Intellisense should work for you now too.

Now when you select a Python version, the right ones should appear. Make sure you pick the right environment (for me I named it "arcgispro-py3-vscode")

If you do it this way you should be able to use conda correctly from a bash shell too.

(base) brian@murre:~$ conda env list
# conda environments:
#
base                  *  C:\Program Files\ArcGIS\Pro\bin\Python 
arcgispro-py3            C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3
arcgispro-py3-vscode     C:\Users\brian\AppData\Local\ESRI\conda\envs\arcgispro-py3-vscode

conda activate arcgispro-py3-vscode
(arcgispro-py3-vscode) brian@murre:~$
python --version
Python 3.6.10 :: Anaconda, Inc.

and so on...

ESRI .PYT files are not treated as Python

https://stackoverflow.com/questions/29973619/how-to-make-vs-code-to-treat-other-file-extensions-as-certain-language

I put this in my global settings.json file (F1 File:Associations)

   "files.associations": {"*.pyt": "python"}

Remote development

I've now tried this tutorial and learned it works. Python in a Container I got a simple Flask app running and then switched to running even simpler Python scripts in the container.

When I needed to add volume support I discovered this page: VS Code Remote Development It explains the Remote Development extension pack.

  • I can use Remote - SSH to treat a remote machine (say, Bellman) as the host for a remote project. (* As recommended by John Sullivan.)
  • I can use Remote - Containers to treat a Docker container as the host.
  • I can use Remote - WSL too but I don't use Windows Subsystem for Linux currently.

Docker containers

I can keep the code on the local file system or in the container.

My first tests I used the Dockerfile to load my code into the image. It worked fine.

More options:

  • clone from github into a running container
  • keep code in a volume mounted on the container

Using a volume seems to make the most sense to me. In that setting I can still easily use command line git.

There is a sample based on python, it starts a Debian container and puts a Bash prompt into a Terminal window. It connects when you do F5. Your code is accessible on the local filesystem and in the mounted volume in the container.

git clone https://github.com/Microsoft/vscode-remote-try-python