Python environment construction (Anaconda + VSCode) @ Windows10 [January 2020 version]

background

When creating a Python environment with Anaconda + VS Code, I was in trouble because the latest setting method did not come out, so I will publish my memo for those who will do it from now on. It seems that there was a change in VS Code specifications around 2017. Be careful when reading old commentary.

Referenced article: @ Atupon0302: Build Python environment with Anaconda + Visual Studio Code in Windows 10 environment [September 2017]

What to write in this article

In this article, I will write the procedure to build a Python environment with Anaconda and VS Code on a Windows 10 PC, and build and debug a simple program.

Since the purpose of introducing Python is to play with machine learning, we will use Anaconda, which can store Python libraries for machine learning at once. (Before, I had a lot of trouble if I put in the machine learning related libraries individually ...)

Prerequisite environment

procedure

Put Anaconda

Installation of the main unit

Get the module for Windows from the Download link on the Anaconda page. Select 3.x as the Python version. All the contents of the installer are by default, just press the Next button. I was "Anaconda 2019.10 for Windows Installer". anaconda1.png

Operation check

Start Anaconda3> Anaconda Prompt from the start menu. Execute the following with Anaconda Prompt, and when the version number is returned, it's OK.

conda -V
python -V 

Creating a virtual environment

Create your own virtual environment to hold the library configuration of the execution environment. By default, there is a base environment, which already contains useful libraries such as numpy, so copy the base and create a new one. If you don't use the --clone option, you can create a brand new environment. I will add my favorite library here. (See below)

#Create py37 environment (when duplicating base)
conda create -n py37 --clone base 

#Create py37 environment (when creating only python and adding necessary libraries later)
conda create -n py37 python=3.7

Check the created environment.

#Display environment list
conda info -e
(base) C:\Users\XXXX>conda info -e
# conda environments:
#
py37                     C:\Users\XXXX\.conda\envs\py37
base                  *  C:\Users\XXXX\Anaconda3

The conda activate command to switch to the new environment.

#Enable the py37 environment
conda activate py37

If successful, the prompt will have the environment name (py37)

(base) C:\Users\XXXX>activate py37

(py37) C:\Users\XXXX>

The display of the libraries in that environment is conda list

#View version of numpy library
conda list numpy
(py37) C:\Users\XXXX>conda list numpy
# packages in environment at C:\Users\XXXX\Anaconda3\envs\py37:
#
# Name                    Version                   Build  Channel
numpy                     1.16.5           py37h19fb1c0_0
numpy-base                1.16.5           py37hc3f5095_0
numpydoc                  0.9.1                      py_0

If you need to add a library, add it with conda search`` conda install.

#When looking for a django library
conda search django

#When adding the django library
conda install django

To exit the environment, use the conda deactivate command. (There is an explanation that the command is deactivate, but it is currently deprecated, so set it to conda deactivate.)

#Get out of the environment
conda deactivate

Use the conda command like the PIP command provided by Python. In the Anaconda environment, use the conda command instead of pip to manage packages.

Python path

As mentioned above, I will create a virtual environment according to my work, so I will not set the Windows PATH. (Set from the control panel)

Insert VS Code

Installation of the main unit

Get the module for Windows from the Download link on the vscode page and insert it. The contents of the installer are entered by default. vscode.png

Operation check

Launch Visual Studio Code from the Start menu. It is described as VS Code below.

Setup: Allow Python to be built and debugged

000231.png

  1. Enter "python.pythonpath" in the search key and set the Python path. For the value, refer to the value entered as conda info -e on Anaconda. C:\Users\XXXX\Anaconda3\envs\py37\python.exe

  2. Enter "python.condapath" in the search key and set the path of the Conda command. The value specifies the directory name where conda.exe is located. (Scripts) C:\Users\XXXX\Anaconda3\Scripts\

Create a suitable working folder and write test.py. At this time, if the working folder is under the virtual environment of Anaconda, the breakpoint cannot be used in the debug execution described later. When developing with VS Code, a development folder different from the Anaconda virtual environment is required (even though the environment was divided, it is a little troublesome ...)

Reference: Problem that does not stop at breakpoint when debugging Python virtual environment with VS Code

The following is working in c: \ dev \ pydev.

test.py


print("foo")
print("bar")
print("Fuga Fuga")

If you run "Ctrl + Shift + B" (build task execution command) with test.py open in VS Code, you will get a warning message that there are no tasks. When you click the message, the message to create a task from the template is displayed. Click it further and select "Others" from the template type to create the task setting file (tasks.json). task01.png task02.png task03.png

Edit tasks.json as follows and save it. The point is to write the Python path specified in settings.json in command.

tasks.json


{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "python build",
            "type": "shell",
            "command": "${config:python.pythonPath}",
            "args": ["${file}"],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

「Ctrl + Shift + B」

After saving task.json, if you execute "Ctrl + Shift + B" (build task execution command) with test.py open, the execution result will be displayed on the console. 000241.png

「F5」

If you execute "F5" (build task execution command) with test.py open in VS Code, it will be executed in debug mode. When you place a breakpoint, it will be highlighted in yellow and you can step through it. 000242.png

Where I got stuck

I was using the latest version of Python Extention (2020.1), but I get an error that conda cannot be found when using PythonExtention 2019.12 version, so use 2020.1 / 9154) occurred. I updated Python Extention from 2020.1.57204 to 2020.1.58038 and it disappeared. Is this okay?

Reference link

Official: Python with VS Code Official: Environment Variable Settings

Recommended Posts

Python environment construction (Anaconda + VSCode) @ Windows10 [January 2020 version]
Anaconda python environment construction on Windows 10
python windows environment construction
VScode environment construction (Windows10, Python, C ++, C, Git)
Python + Anaconda + Pycharm environment construction
Python environment construction (Windows10 + Emacs)
Anaconda3 python environment construction procedure
Python environment construction under Windows7 environment
Python environment construction memo on Windows 10
Anaconda environment construction on Mac (2018 version)
Python environment construction (pyenv, anaconda, tensorflow)
[Python3] Development environment construction << Windows edition >>
Python3.6 environment construction (using Win environment Anaconda)
Python environment construction
python windows environment
Environment construction (python)
python environment construction
Python --Environment construction
Python environment construction
python environment construction
Python (anaconda) development environment construction procedure (SpringToolsSuites) _2020.4
Python project environment construction procedure (for windows)
Python3 + venv + VSCode + macOS development environment construction
Windows + gVim + Poetry python development environment construction
Environment construction of TensorFlow + JupyterNotebook + Matplotlib on Windows version Anaconda (August 2017 version)
homebrew python environment construction
Python development environment construction
python2.7 development environment construction
Anaconda environment construction memo
Mac environment construction Python
From Python environment construction to virtual environment construction with anaconda
Python environment construction @ Win7
Create a comfortable Python 3 (Anaconda) development environment on windows
Python development environment with Windows + Anaconda3 + Visual Studio Code
QGIS3 Python plugin development environment construction with VSCode (macOS)
[Python] Django environment construction (pyenv + pyenv-virtualenv + Anaconda) for macOS
Install Python environment with Anaconda
Anaconda environment construction on CentOS7
CI environment construction ~ Python edition ~
Python environment construction For Mac
Build Python environment on Windows
Version upgrade of python Anaconda
Anaconda3 × Pycharm environment construction memo
Python3 environment construction (for beginners)
Python environment construction and TensorFlow
Build python environment on windows
[MEMO] [Development environment construction] Python
[Tensorflow] Tensorflow environment construction on Windows 10
Environment construction of python2 & 3 (OSX)
Python environment construction on Mac (pyenv, virtualenv, anaconda, ipython notebook)
OpenJTalk on Windows10 (Speak Japanese with Python from environment construction)
[Python] Anaconda environment construction (installation, startup, virtual environment, package management) Mac environment
Prepare Python development environment with Mac + Windows + VisualStudio Code (Windows version)
Prepare Python development environment with Mac + Windows + VisualStudio Code (Mac version)
Python environment construction procedure memo using Docker on Windows10 Home
VScode environment construction (on Mac) & graph display in Python (@browser)
I built an environment for machine learning from scratch (windows10 + Anaconda + VSCode + Tensorflow + GPU version)
Environment construction of python and opencv
Virtual Environment Version Control Summary Python
Get started with Python! ~ ① Environment construction ~
Anaconda and Python version correspondence table