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.
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.
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"
Launched Anaconda Prompt,
conda create -n Favorite environment name python=Python version
Please start the virtual environment with.
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
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.
First, when VS Code opens, ① click Extensions from the leftmost sidebar and search for ② python
. And ③ Install the Pytho extension made by Microsoft.
Open the folder and write the actual program.
Select File
> ʻOpen Folder` from the menu bar above VS Code and open the folder where you want to create the program.
After opening the folder, create a test py file.
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.
Change the execution environment (Terminal) in VS Code to Command Prompt.
Then select Terminal
> New Terminal
from the menu bar above.
Then a terminal will open at the bottom of the window.
Then select "Select Default Shell" from the "1: bash" (display name may be different) pull-down menu at the top right of this terminal.
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)
Next, set the interpreter (execution environment) for executing Python code.
① Press Ctrl
+ Shift
+ P
to open the VS Code command palette and search for ② python select
. Then select ③ Python Select Interpreter
.
Then select the environment of the "favorite environment name" created earlier. In this case it is titanic: conda
.
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
.
① 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
.
Select Python File
in Debug Configuration
.
Then, a json
file called .vscode> launch.json
will be created in the folder as shown above. This file contains Python execution settings.
With the code you want to execute (test.py
in this case) open, you can execute the code by pressing the F5
key.
However, I think that the first execution will result in an error as shown in the above figure.
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.
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.
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.
VS Code's Python execution has many features.
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,
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)
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!
conda install jupyter
First, install Jupyter in your environment (this time titanic
) with Anaconda Prompt.
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
Then, as before, select the environment of "favorite environment name" (titanic: conda
in this case).
Then create a ʻipynb` file to run Jupyter.
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.
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.
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