How to build Python and Jupyter execution environment with VS Code

I summarized how to build Python and Jupyter execution environment with VS Code. I use Anaconda.

Also, I think that many people who analyze data with Python use Jupyter notebook or Jupyter Lab from a browser. VS Code is also recommended for such people.

This article is also part of the article How to create a Python package using VS Code (https://qiita.com/SolKul/items/9208163c79dc4002733c).

We will proceed on the assumption that Anaconda has been downloaded. (Install VS Code separately instead of installing VS Code at the same time when installing Anaconda)

Also, the screen display may differ depending on the version of each software. In that case, please check accordingly.

environment

Remarks
OS Windows10
conda 4.8.3 With Anaconda Promptconda -V
Anaconda 2020.02 With Anaconda Promptconda list anaconda
Python 3.8.2
VSCode 1.43.2

Also, I will use the data of Kaggle's Titanic as an example, but you do not need to prepare it because you can build a Python execution environment with VS Code even if you do not have the data.

00titanic_Explorer.png

VS Code installation

Please install VS Code. Basically, I think you can [Next] without thinking about anything.

For more information on VS Code, see this article. If you have any questions or are confused, you can read this article first to get an overall picture and deal with the troubles. How to use Visual Studio Code, basic "key"

Launching a virtual environment in Anaconda

Launched Anaconda Prompt,

conda create -n Favorite environment name python=Python version

Please start the virtual environment with. 01create_env.png

This time, the environment name is titanic. Then start the virtual environment.

conda activate titanic

Install the required modules from here. Let's install pandas as an example.

conda install pandas

Try running Python code in VS Code

Let's run Python in VS Code from here. Search for VS Code in the Start menu to launch VS Code.

As a flow --Install Python extension --Writing a program --Prepare Python execution environment --Run the program

It will be the flow. I will explain in order.

Install Python extension

07InstallPythonExtension.png First, when VS Code opens, ① click Extensions from the leftmost sidebar and search for ② python. And ③ Install the Pytho extension made by Microsoft.

Write a program

Open the folder and write the actual program.

02OpenFolder.jpg
Select File> ʻOpen Folder` from the menu bar above VS Code and open the folder where you want to create the program.

05NewFile.png After opening the folder, create a test py file.

06TestCode.png Create a suitable program. A program that reads and displays training data. If there is no data

print('Hello')

You can use a suitable program such as. Let's run this program on python.

Prepare the Python execution environment

Change the execution environment to Command Prompt

Change the execution environment (Terminal) in VS Code to Command Prompt.

08NewTerminal.png
Then select Terminal> New Terminal from the menu bar above.

09NewTerminalMoza.png

Then a terminal will open at the bottom of the window.

10SelectDefaultShell.png

Then select "Select Default Shell" from the "1: bash" (display name may be different) pull-down menu at the top right of this terminal.

11SelectCMD.png

Then, you will be able to select the terminal type from the top as shown in the image above, so select "Command Proompt".

This is changed because Python cannot be executed if Terminal is still bash or PowerShell. (Maybe only for Windows)

Set up the Python interpreter.

Next, set the interpreter (execution environment) for executing Python code.

12PythonSelectInterpreter.png

① Press Ctrl + Shift + P to open the VS Code command palette and search for ② python select. Then select ③ Python Select Interpreter.

13Interpreters.png Then select the environment of the "favorite environment name" created earlier. In this case it is titanic: conda.

14Setting.json.png Then, a json file called .vscode> setting.json will be created in the folder as shown above. The file contains the location of python.exe for the specified environment.

** Anaconda environment name does not appear in Python interpreter **

However, even if you operate in the same way, the environment name of Anaconda may not appear in the Python interpreter. This may be due to changing the installation location of Anaconda from the default.

In that case, please refer to the following article and set the environment variable PATH etc.

Building Anaconda's Python debugging environment with VS Code (Visual Studio Code)

Also, VS Code may not be able to find the Python interpreter on your PC, so try restarting VS Code, waiting a few seconds, and then reselecting Python Select Interpreter.

Set Python execution settings

15DebugRun.png

① With the python code (test.py in this case) open in VS Code, ② select Run in the leftmost sidebar, and ③ select create a launch.json file.

16DebugConfig.png Select Python File in Debug Configuration.

17LaunchJson.png Then, a json file called .vscode> launch.json will be created in the folder as shown above. This file contains Python execution settings.

Run the program

18SelectTestpy.png With the code you want to execute (test.py in this case) open, you can execute the code by pressing the F5 key.

19DebugError.png However, I think that the first execution will result in an error as shown in the above figure.

20StopDebug.png In that case, click the red square button (Stop) of the button as shown above that appears when you execute it, and stop the execution.

21ActivateEnv.png

When you stop the execution, you can see that the environment switches to your own environment (titanic this time) as shown in the above figure.

22SuccessRun.png If you press the F5 key again in this state, the code can be executed as shown in the above figure, and the contents of the training data are output.

Now that the config file has been created and the settings are complete, you can now run Python by simply pressing the F5 key in VS Code.

Utilization of debugging

VS Code's Python execution has many features.

23BreakPoint.png Rewrite test.py to display the shape of the training data as shown above. Then press the F9 key on the 6th line which isprint (train_shape). You will see a red dot at the left end of this line. This is called a ** breakpoint **. And if you press the F5 key in this state to execute,

24Debug.png Execution is paused on line 6 as shown above, and the variable declared at that time (here train_shape) is displayed in the left sidebar. By using breakpoints in this way, I think that bug fixing (= ** debugging **) of the program will be improved.

There are various other functions, so please read the articles below and check them out.

VS Code makes it easy to debug Python code! !! (1/4)

Run Jupyter Notebook

However, when analyzing data, I think that you often use Jupyter Notebook, which makes it easy to visualize the data. In fact, VS Code can also run Jupyter!

30InstalJupyter.png

conda install jupyter

First, install Jupyter in your environment (this time titanic) with Anaconda Prompt.

31SelectInterpreterJupyter.png

Then press ①Ctrl + Shift + P in VS Code to open the VS Code command palette and enter ② python select interpreter. Then select ③ python select interpreter to start jupyter server

13Interpreters.png Then, as before, select the environment of "favorite environment name" (titanic: conda in this case).

32CreateIpynb.png Then create a ʻipynb` file to run Jupyter.

33JupyterUpperRight.png When you open this ʻipynb` file, make sure that the environment name in the upper right is the specified environment. If it has a different environment name, click here and specify the environment again.

34RunJupyter.png You can now run Jupyter as well.

However, VS Code's Jupyter is unstable and often doesn't work. In that case, use Jupyter from your browser as usual.

At the end

I've explained it for a long time, but honestly, I think that there is little merit in using VS Code for data analysis if it is just this.

In fact, you can do the same with Jupyter Notebook or nbextensions variable inspector.

When combined with the Python packaging described in How to create a Python package using VS Code, you can see various benefits. Thank you for this article as well.

Recommended Posts

How to build Python and Jupyter execution environment with VS Code
Build a python execution environment with VS Code
Build a Python environment with WSL + Pyenv + Jupyter + VS Code
Build a development environment using Jupyter and Flask with Python in Docker (supports both VS Code / code-server)
How to build a python2.7 series development environment with Vagrant
Build Jupyter Lab (Python) environment with Docker
Operate Jupyter with REST API to extract and save Python code
Create a simple Python development environment with VS Code and Docker
Build jupyter notebook environment with Visual Studio Code (VS Code) Mac version
How to build Anaconda virtual environment used in Azure Machine Learning and link with Jupyter
Build Python development environment with Visual Studio Code
Python (Windows 10) Virtual Environment / Package with VS Code
How to measure execution time with Python Part 1
Make your Python environment "easy" with VS Code
How to measure execution time with Python Part 2
I was addicted to creating a Python venv environment with VS Code
Steps to create a Python virtual environment with VS Code on Windows
How to make VS Code aware of the venv environment and its benefits
How to debug a Python program by remotely connecting to a Docker container in WSL2 environment with VS Code
Build python3 environment with ubuntu 16.04
How to build a Django (python) environment on docker
Build python environment with direnv
How to make a Python package using VS Code
Build a python virtual environment with virtualenv and virtualenvwrapper
Python local development environment construction template [Flask / Django / Jupyter with Docker + VS Code]
How to build a Python environment on amazon linux 2
[Python] How to create a local web server environment with SimpleHTTPServer and CGIHTTPServer
How to build a beautiful Python environment on a new Mac and install Jupter Notebook
[Note] How to write QR code and description in the same image with python
Environment construction for those who want to study python easily with VS Code (for Mac)
Let's run jupyter natively supported by VS Code with python3.8
How to build a new python virtual environment on Ubuntu
How to use VS Code in venv environment on windows
[Python] How to play with class variables with decorator and metaclass
Build a Python environment and transfer data to the server
How to do Bulk Update with PyMySQL and notes [Python]
How to get into the python development environment with Vagrant
UpNext2 Development Record # 1 Build Python CI environment in VS Code
How to log in to AtCoder with Python and submit automatically
Overview of Python virtual environment and how to create it
python / tensorflow beginners build jupyter + tensorflow environment and do Hello World
Python: How to use async with
Build python virtual environment with virtualenv
Build Mysql + Python environment with docker
How to get started with Python
Build PyPy execution environment with Docker
Try running Jupyter with VS Code
How to use FTP with Python
How to calculate date with python
Install python with mac vs code
Build a python execution environment with VS Code
Strengthen with code test ⑦
Strengthen with code test ⑨
Strengthen with code test ③
Strengthen with code test ⑤
Strengthen with code test ④
Strengthen with code test ②
Strengthen with code test ①
Strengthen with code test ⑧
Strengthen with code test ⑨
PyCharm test code automatic generation
Debug Python with VS Code
How to build Python and Jupyter execution environment with VS Code
How to parse Java source code with AST (Abstract Syntax Tree) using ANTLR and Python
[Python / Ruby] Understanding with code How to get data from online and write it to CSV
[Sakura Rental Server] (For beginners) How to build an environment for Python, pyenv, and Flask. | For csh
How to deal with python installation error in pyenv (BUILD FAILED)
How to batch start a python program created with Jupyter notebook
Get the strongest environment with VS Code, Remote-Containers and remote docker-daemon
Build a 64-bit Python 2.7 environment with TDM-GCC and MinGW-w64 on Windows 7
Build a Python environment on your Mac with Anaconda and PyCharm
How to import CSV and TSV files into SQLite with Python
Try to create a python environment with Visual Studio Code & WSL