当前位置:   article > 正文

the python path in your debug_Build your own environment in Visual Studio Code for Python

you must use visual studio to build a python extension on windows. if you ar

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.

After finishing the download, run the installer.

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,

then the installing is successful!

If this message shows up,

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.

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.

Choose your new folder.

Create a folder ".VScode".

Inside the ".VScode" folder, create a file called "settings.json".

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,

}

You can see the python path in left-bottom.

Install the extension "Python".

Create a file called "test.py".

You can start to type your python code, and click the left-bottom button to open a terminal.

Type the following command in your terminal.

python test.py

Run the python program successfully.

Next, we are going to install a tool for debugging.

Type the command in your terminal.

pip install pylint

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.

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.

Finally, install the extension "Run code".

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.

If you want to change the font-family or font-size, go to File->Preferences->Settings.

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".

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.

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/836672
推荐阅读
相关标签
  

闽ICP备14008679号