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]
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 ...)
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".
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
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.
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)
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.
Launch Visual Studio Code from the Start menu. It is described as VS Code below.
Include Python extensions Click the extension button on the VS Code toolbar, search for "Python", and enter the Python extension for Visual Studio Code.
Since there was a problem with the image version, I finally added Python Extention 2020.1.58038.
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
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).
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.
「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.
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?
Official: Python with VS Code Official: Environment Variable Settings
Recommended Posts