Build your own environment in Visual Studio Code for Python

Introduction

As a python coder, you want to find a better environment for coding. However, PyCharm is too heavy-weight to use. In addition, its running speed and efficiency are slow and not good. I want to recommend a light-weight editor "Visual Studio Code". Although you should set up all of your personal settings, I still think it is a good tool for python coding. In this article, I will step by step teach you how to build the environment in Visual Studio Code for Python.

Download and install Python

First, go to the official website.
https://www.python.org/
Becasue my OS is Win10, I choose Windows.
https://ithelp.ithome.com.tw/upload/images/20190802/201188899q3ymAXKK8.png
After finishing the download, run the installer.
https://ithelp.ithome.com.tw/upload/images/20190802/20118889wRZVKZQEsh.png
Choose "Add Python 3.7 to PATH", then it will be added into the environment variable in your computer automatically.
Click "Next" until the installing is finished.
Open the command line prompt(cmd), type "python", and press Enter.
If this message shows up,
https://ithelp.ithome.com.tw/upload/images/20190802/20118889ah8hqRFHzM.png
then the installing is successful!
If this message shows up,
https://ithelp.ithome.com.tw/upload/images/20190802/20118889izLuAI3RyE.png
then you might not set the environment variable successfully.

Set up the environment variable

Go to Control Panel->System and Security->System->Advaned system setting->Environment variables->choose the System variables "Path"->Edit->New->type your python path->OK->OK.
https://ithelp.ithome.com.tw/upload/images/20190802/201188893lzYrukESv.png
Check the two ones are added.
C:\Users\user\AppData\Local\Programs\Python\Python37-32\Scripts
C:\Users\user\AppData\Local\Programs\Python\Python37-32
Note that you need to re-open your cmd after your setting is changed, or the setting will not take effect!!

Build the environment for Visual Studio Code(VScode)

You should go to https://code.visualstudio.com/download to install VScode.
After installing VScode, open VScode->File->Open folder->click right->New->Folder.
https://ithelp.ithome.com.tw/upload/images/20190711/201188898VpWOdhhmw.png
Choose your new folder.
https://ithelp.ithome.com.tw/upload/images/20190711/20118889iZc5fzdOcG.png
Create a folder ".VScode".
https://ithelp.ithome.com.tw/upload/images/20190711/20118889SOotiwxaRm.png
https://ithelp.ithome.com.tw/upload/images/20190711/20118889uTjyP9J70g.png
Inside the ".VScode" folder, create a file called "settings.json".
https://ithelp.ithome.com.tw/upload/images/20190711/201188893e0emQcLzs.png
https://ithelp.ithome.com.tw/upload/images/20190711/201188892YEjOIyhDZ.png
Add the following code into "settings.json".
(your python path may not be the same as mine, but will be similar)

{
    "python.pythonPath": "C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python37-32\\python.exe",
    "python.linting.enabled": true,
    "python.linting.pylintEnabled": true,
}

https://ithelp.ithome.com.tw/upload/images/20190711/20118889Nd8l9FMt1h.png
You can see the python path in left-bottom.
https://ithelp.ithome.com.tw/upload/images/20190711/20118889pvVoRIi9xq.png
Install the extension "Python".
https://ithelp.ithome.com.tw/upload/images/20190711/20118889dvIxB1Hucl.png
Create a file called "test.py".
https://ithelp.ithome.com.tw/upload/images/20190711/20118889kcvFJVxDW4.png
You can start to type your python code, and click the left-bottom button to open a terminal.
https://ithelp.ithome.com.tw/upload/images/20190711/20118889XH0VaPWSXr.png
Type the following command in your terminal.

python test.py

Run the python program successfully.
https://ithelp.ithome.com.tw/upload/images/20190711/20118889xc3TdH2ZJc.png
Next, we are going to install a tool for debugging.
Type the command in your terminal.

pip install pylint

https://ithelp.ithome.com.tw/upload/images/20190711/201188899URVunRGMp.png
After installing pylint, it will help you to debug your python code.
Let's change "print" to "printf", and you can see the error message show up.
https://ithelp.ithome.com.tw/upload/images/20190711/20118889TBBC0ntAQP.png
This is why we add the following code in "settings.json".

"python.linting.enabled": true,
"python.linting.pylintEnabled": true,

This is to enable the debugging capability.
If you have not installed pylint, an error message wil show up.
https://ithelp.ithome.com.tw/upload/images/20190729/20118889OjXiMlYi2n.png
Finally, install the extension "Run code".
https://ithelp.ithome.com.tw/upload/images/20190713/20118889rCg5leRTRV.png
After getting this tool, you can right click in your editor and choose "Run Code" to run your program.
Also, you can click the triangle button in right-top to run your program.
https://ithelp.ithome.com.tw/upload/images/20190711/201188893hkGU5MaGh.png
If you want to change the font-family or font-size, go to File->Preferences->Settings.
https://ithelp.ithome.com.tw/upload/images/20190711/20118889CGi31Skinl.png
Note that the "User" part in Settings is the global setting for VScode.
It means that the settings will apply to everywhere.
If you want to change the settings only for the current folder, you should change the settings in "Workspace" part.
After you change some settings for "Workspace", you will see the ".json" file is added or modified in ".vscode" folder.
If you want to change English to Chinese, press Ctrl+Shift+P, and search for "language".
https://ithelp.ithome.com.tw/upload/images/20190713/20118889wEIqVT7rC6.png
Choose "Configure Display Language"->press Enter->choose zh-tw.
Then it will become the Chinese version.
In order to get an auto formatted tool, Run the command in your terminal.

pip install autopep8

Add the following contents into "settings.json".

{
    "python.pythonPath": "C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python37-32\\python.exe",
    "python.linting.enabled": true,
    "python.linting.pylintEnabled": true,
    "files.trimTrailingWhitespace": true, // it will trim your space when saving file
    "files.autoSave": "onFocusChange", // whether you want to enable autosave
    "[python]":{  // add
        "editor.formatOnType": true,
        "editor.formatOnSave": true,
        "editor.renderIndentGuides": true,
        "editor.insertSpaces": true,
        "editor.detectIndentation": true,
        "editor.tabSize": 4
    },
}

After the settings are changed, VScode will help you to format your code when you press Enter in editor or save your file.
Now, you can get started to coding with python in VScode!

This article is translated from my original one https://ithelp.ithome.com.tw/articles/10212365
If you want to read the Chinese version, you can check this.


<<:  如何在Windows 10中启用或禁用系统保留存储

>>:  JS 样板字面值 DAY75

Day17 - 语音辨识神级工具-Kaldi part2

首先介绍的是发音词典处理,我们必须先准备一份发音词典(lexicon),格式会长得像以下的样子。左边...

近似最短路径 (7)

11.6a 把树合并起来 让我们简单描述一下把树合并起来的过程,补上昨天 Peleg’s Dista...

30天打造品牌特色电商网站 Day.26 了解滚动视差

有没有听过视差滚动这个词?网页上带有视差滚动的效果,可以让网站更为吸睛喔! 什麽是滚动视差? 随着滑...

DAY5 起手式--Nuxt.js(细)说pages(下)

layout 属性 我们可以使用 layout 属性来套用放置於 layouts 的布局。 expo...

Day 14: 人工智慧在音乐领域的应用 (AI作曲-演算法基础概念)

接下来的几天我们会比较详细的介绍几个演算法是如何运用在作曲上,但在我们开始进入演算法作曲之前,我们要...