Do you guys have a good Python life?
Suddenly, how do you usually deal with Python code?
I think that there are many people who normally create a .py``` file and run it, or write code in a code block using Jupyter Notebook or Jupyter Lab and execute it block by block. I will. Since I am a file in
`.ipynb``` format that Jupyter can handle Python code materials distributed in university classes, I used to browse and execute code using Jupyter Notebook.
But everyone, don't you hate launching Jupyter Notebook and taking one or two browser tabs? Many Chrome users, in particular, think so.
I used to use Microsoft's Visual Studio Code (VS Code) to handle `` `.ipynb``` files because of that idea, but honestly it's hard to handle. It is not possible to intuitively restart the kernel or forcibly terminate it. (By the way, I recently learned that VS Code seems to terminate the kernel when the file is closed.)
So, in my case, I continued to use Jupyter Notebook in the end by first using Jupyter Notebook → trying to open `` `.ipynb``` with VS Code → returning to Jupyter Notebook. (I didn't use Jupyter Lab personally because I'm not used to UI.)
However, when I was looking at Microsoft's documentation, I found something interesting. I'm new to Python, so you might say "I've had it before!", But VS Code says that you can touch the code in a `` .py``` file like Jupyter. I knew.
If you say "I know the function! I've had it for a long time!", Please go back to the browser ... and if you don't, I hope you can see it till the end. (I would be grateful if you could do LGTM!)
The following is the image described in the Microsoft documentation, but please prepare a .py file and write `# %%`
on the first line there. It is declared that this is the head of the code block in Jupyter, and if you write the code under it, it will work on VS Code like a single code block.
Normally, as shown in the image below, there is a code block, and it is written in it and executed, but the same thing can be done with the `` `.py``` file.
I personally feel that there is only merit (laugh)
First, making it a `.py``` file makes it easier to manage on your Git system. In the case of the
.ipynb``` file, the contents are arranged like JSON, so it is very difficult to check the difference on Git. There were times when the array of contents was corrupted and the ``
.ipynb file couldn't be opened at all (although I self-healed). So, Git users can use `` `.py
because it is easy to manage the `` `.py``` file, which has a simple content and contains only the source code. I personally think it's better to manage it.
Also, from the above points, you can browse the source code without opening the file in the Jupyter environment, so even if you want to share only this part in Slack, you can share it quickly.
I think this .ipynb_checkpoints``` is the one that is sober and annoying. When executing the
.ipynb``` file in this folder, the automatic save function is applied in Jupyter Notebook etc. and the temporary execution state is saved, but the destination is this `` .ipynb_checkpoints ``
Folder. Especially when fetching files from a folder by machine learning etc., if there is `` `.ipynb_checkpoints```, it may cause a malfunction, so you need to be careful. Isn't this not generated only for the benefit, especially for those who are touching code such as machine learning?
When opening a .ipynb
file in VS Code, it takes a long time after the user selects the file until the file is read and the code block is displayed. On the other hand, whether you open the `.py``` file with Jupyter Notebook or VS Code, the response when opening this file is very fast, so handle
.ipynb``` with VS Code. I think there are many people who feel a hurdle to this.
Also, when using it personally, when I used `` `.ipynb``` in VS Code, it sometimes freezes or I do not know the execution status, but now I think that the response is quick and stable because such a thing has not happened (thanks to merit 4).
As you can see by looking at the first image again, if you use this function, the execution result will be output in the separate window on the right. It's actually like a Jupyter environment, but until now, if you want to restart / terminate the kernel, save the `.ipynb``` file once, close it, and reopen it. Must be. Also, as mentioned earlier in Merit 3, when opening
`.ipynb, the response is often quite slow and frustrating ... but Jupyter in the `` .py
file If you want to restart / terminate the kernel after opening the environment, just close the Jupyter window on the right! I think that stress will be reduced considerably.
One of the merits of handling Jupyter files with VS Code is that you can check the storage status of variables. I think it's a simple and convenient function, but when you handle Jupyter with `.py```, you can check the variable storage status from the Jupyter window. There is also a section at the bottom of the Jupyter window called
`Type code here and press shift-enter to run```, where you can quickly execute any code. It is convenient because it can be used when you do not need to write it in a file such as a print statement but want to check the situation.
It's been a little long, but please stay with me for a while ...
VS Code has a feature called Workspace, which allows you to manage the environment and code for each project. The same is true for Python projects, and multiple projects are placed on one computer, such as the directory where the Python execution environment and code are placed, and the files that were opened the last time the workspace was opened are displayed again. If you do (I think most people do), this is a useful feature. Also, since the Python execution environment can be managed for each Workspace, isn't it a useful function especially for those who are using Virtualenv, Pipenv, etc.?
This is very important for heavy Chrome users ... The Jupyter Notebook page doesn't have tabs and the icons are even easier to see.
Eh, there are too many tabs? It's because of that, I'm sure.
The last merit is the affinity with WSL. Many Windows users are already familiar with WSL, which has been booming recently. It is very useful to be able to set up a Linux environment such as Ubuntu in Windows. In this WSL and VS Code, there is an extension function dedicated to WSL so that it can be handled conveniently. By using this, you can operate the Python environment in the WSL environment more conveniently. Personally, this extension is important.
On the other hand, there are some disadvantages, so I will introduce them properly.
As mentioned in Merit 1, the contents of the .ipynb
file contains JSON-like array data. Therefore, unlike the `.py``` file, you can see the execution result, that is, the contents output by the print statement, the matplotlib image, the pandas table, etc. as they are. If you want to share the execution result with someone, it is better to pass it as a file in
`.ipynb``` format.
The downside, I could only come up with one ... but personally, I'm very much looking forward to this feature of using the Jupyter environment with `.py``` files in this VS Code. It's very easy to set up (just prepare the file and add
# %%
``), so why not give it a try?