iT邦幫忙

0

Build your own environment in Visual Studio Code for Python

  • 分享至 

  • xImage
  •  

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.


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言