Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Using VS Code to share the skills of writing Python

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "using VS Code to share the skills of writing Python". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

This article is based on VS Code 1.36.1

Why use VS Code? Isn't it good to use PyCharm?

● VS Code is open source and free, and PyCharm is free.

● VS Code can write other languages besides Python, but not PyCharm.

● VS Code starts up a little bit faster than PyCharm.

1. The terminal runs Python

Python is not included in VS Code, so you have to install a Python first.

After installing python, we can start writing python code with any of the text editing tools, and then run the code in cmd.

In VS Code, you can also run python code without installing any plug-ins.

Create a new test.py file and enter print ('Hello wordings').

Click View-> Terminal (shortcut key Ctrl + `) to open the terminal, and type python test.py to run it, which is the same as running python code in cmd.

VS Code configure Python environment (1) .png

2. Install the Python plug-in

Without installing the plug-in, there is no way to run the code by clicking debug-> start debugging (shortcut key F5). It makes you choose your environment, and you don't have an environment at all.

Search for the Python plugin in VS Code's App Store (shortcut: Ctrl + Shift + X) and install it. As shown below:

VS Code configure Python environment (2) .png

Press Ctrl + Shift + P (or F1), enter Python: Select Interpreter search in the open input box, and select the Python parser.

VS Code configure Python environment (3) .png

VS Code configure Python environment (4) .png

After choosing the parser, we can run (shortcut Ctrl + F5) or debug (shortcut F5) Python code directly in VS Code.

3. View and install external libraries

The most powerful thing about Python is the external library, which can be viewed, searched, and installed in Settings in PyCharm. So how do you install it in VS Code?

As mentioned above, you can run the .py file directly on the terminal. In fact, the terminal is equivalent to a command prompt (cmd), so you can enter the corresponding command directly on the terminal.

View a list of installed packages:

Pip list

Install external libraries:

Pip install xxx Note: when multiple Python parsers are installed locally, pip may not be installed in the current parser directory. After testing this many times, I found that it should be related to the order in the environment variables and would be installed in the directory of the highest parser in the environment variables.

4. Code completion tool

The code completion plug-in in VS Code is Visual Studio IntelliCode. It is also installed through the app store.

Visual Studio IntelliCode is a set of AI auxiliary functions that can improve the productivity of developers through intelligent context awareness, code-style reasoning and execution.

IntelliCode generates suggestions by using machine learning models trained in thousands of common code bases, and will become more accurate as you write code. It gives programming advice based on context, rather than simply recommending API based on alphabetical order.

The most important IntelliCode already supports Python. After installation, you can have a smart prompt.

VS Code configure Python environment (5) .png

5. Code checking tool

PyCharm comes with a code review tool that is not available in VS Code.

5.1 、 pylint

When you create a new .py file and start writing code, this may pop up in the lower right corner (the old version will, but the new version won't):

VS Code configure Python environment (6) .png

Pylint is a Python code review tool. It needs to be installed through pip.

Open the settings and add the following settings:

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

Then for code that does not conform to the specification, there will be a wavy line hint.

In VS Code version 1.36.1, it seems that you don't have to design python.linting.enabled ": true.

5.2, flake8 and yapf

Flake8 is a tool officially released by Python to help detect whether the Python code is standardized. Compared with the current hot Pylint, flake8 inspection rules are flexible, support the integration of additional plug-ins, and strong scalability. Flake8 is an encapsulation of the following three tools:

● PyFlakes: a tool to statically check for logic errors in Python code.

● Pep8: a tool for statically checking PEP8 coding styles.

● NedBatchelder's McCabe: a tool for statically analyzing the complexity of Python code.

Yapf is a tool for formatting code.

After installing Flake8 and Yapf through pip, configure as follows.

"python.linting.enabled": true, "python.linting.pylintEnabled": false, "python.linting.flake8Enabled": true, "python.formatting.provider": "yapf"

If pylint is installed, set its Enabled = false, otherwise you don't need it.

After installation, we right-click in the code file (.py), select format document, and we can format the code automatically.

VS Code configure Python environment (7) .png

Although the code review tool is good, but some strict checks are not in line with our habits, for example, the maximum number of characters on the default line of flake8 is 79, if it exceeds it, it will show a wavy line that your code is too long.

VS Code configure Python environment (8) .png

79 characters is too short, we can make it a little longer, add the following configuration, and set the maximum length to 128 characters.

"python.linting.flake8Args": ["--max-line-length=128"sharing skills in writing Python with VS Code" ends here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report