为Python开发设置Visual Studio代码

In this article, I am going to explain how to set up your Visual Studio Code for Python Development. Visual Studio Code or, popularly known as VS Code, is one of the free and open-source code editors developed by Microsoft and is mostly preferred by developers of all the major programming languages due to its flexibility and other integrated development tools like debugging, IntelliSense, etc. Visual Studio Code is available to be downloaded for all the major operating systems like Windows, Linux, and macOS. You can visit https://visualstudio.microsoft.com/ to download it based on the OS you are using.

在本文中,我将解释如何为Python开发设置Visual Studio代码。 Visual Studio Code或通常称为VS Code,是Microsoft开发的免费和开源代码编辑器之一,由于其灵活性和其他集成开发工具(如调试,IntelliSense),因此最受所有主要编程语言的开发人员青睐等等。VisualStudio Code可用于所有主要操作系统,例如Windows,Linux和macOS。 您可以访问https://visualstudio.microsoft.com/,以根据所使用的操作系统进行下载。

下载并安装Visual Studio代码 (Download and Install Visual Studio Code)

As I have already mentioned, VS Code can be downloaded and installed on any of the operating systems. For the sake of this article, I am going to show how to install the same on Windows. Also, I will be using Windows as the development environment for setting up the python environment also. For Linux and macOS, the process will be almost similar; however, there might be some changes in the commands.

正如我已经提到的,VS Code可以下载并安装在任何操作系统上。 为了本文的目的,我将展示如何在Windows上安装相同的内容。 另外,我还将使用Windows作为开发环境来设置python环境。 对于Linux和macOS,该过程几乎是相同的。 但是,命令中可能会有一些更改。

Navigate to https://code.visualstudio.com/download and select the appropriate platform to install the software. I am going to choose Windows, System Installer 64 bit, and proceed with the installation:

导航到https://code.visualstudio.com/download,然后选择适当的平台来安装软件。 我将选择Windows,64位系统安装程序,然后继续进行安装:

Download Visual Studio Code for Python

Once the download is completed, proceed with the normal installation. After the installation is successful, you can see Visual Studio Code being started, which looks like below:

下载完成后,继续进行常规安装。 安装成功后,您可以看到正在启动Visual Studio Code,如下所示:

Visual Studio Code for Python started

下载并安装Python (Download and Install Python)

Once Visual Studio Code is up and running, the next thing that we would need to set up is a python runtime environment. Again, we can install python on all three operating systems like Windows, Linux, and macOS. Let us first navigate to https://www.python.org/downloads/ and download Python 3.8.3:

一旦Visual Studio Code启动并运行,接下来需要设置的是python运行时环境。 同样,我们可以在Windows,Linux和macOS等所有三种操作系统上安装python。 让我们首先导航到https://www.python.org/downloads/并下载Python 3.8.3:

Download Python 3.8.3

Once python has been downloaded and installed, you can open up the command prompt and run the following command to verify if the installation has been successful.

下载并安装python之后,您可以打开命令提示符并运行以下命令,以验证安装是否成功。

python –version

python –版本

If python has been installed successfully, this command will return the version of the python that has been installed. I have previously downloaded python, and my installed version is 3.8.1 and not 3.8.3:

如果已成功安装python,则此命令将返回已安装的python的版本。 我以前下载了python,安装的版本是3.8.1,而不是3.8.3:

Verify Python Installation

Now that both the pre-requisites are installed, we can go ahead and start setting up the development environment for Visual Studio Code for python.

现在已经安装了两个前提条件,我们可以继续并为Visual Studio Code for python设置开发环境。

安装VS Code的Python扩展 (Install the Python extension for VS Code)

First things first. By default, VS Code works just like a simple text editor and has no built-in support for python. What I mean by the previous statement is even though you can write python code in VS Code now, you won’t be able to leverage some of the developer tools or techniques that make writing code a lot easier. For example, let us start creating a python file with the name “main.py”, import the JSON module and print the statement “Hello World”. As you write the code, you can see that there is no auto-complete or no IntelliSense provided by the code editor and you need to write the entire statement on your own:

首先是第一件事。 默认情况下,VS Code就像一个简单的文本编辑器一样工作,并且不内置对python的支持。 我在前一条语句中的意思是,即使您现在可以在VS Code中编写python代码,也无法利用某些开发人员工具或技术来简化编写代码。 例如,让我们开始创建一个名为“ main.py ”的python文件,导入JSON模块并打印语句“ Hello World ”。 在编写代码时,您可以看到代码编辑器没有提供自动完成功能或没有IntelliSense,并且您需要自己编写整个语句:

Setting up Visual Studio Code for Python

To avoid that, we need to install the Python extension for Visual Studio Code by navigating to https://marketplace.visualstudio.com/items?itemName=ms-python.python and download the extension:

为了避免这种情况,我们需要通过导航到https://marketplace.visualstudio.com/items?itemName=ms-python.python来安装Visual Studio Code的Python扩展 ,并下载该扩展:

Download Python extension for Visual Studio Code

Alternatively, you can also hit the Extensions icon in the left-hand pane inside Visual Studio Code and search for the keyword “python”. Select the extension which is provided by Microsoft and click on Install. I have already installed the extension on my machine:

或者,您也可以点击Visual Studio Code左侧窗格中的Extensions图标,然后搜索关键字“ python ”。 选择Microsoft提供的扩展名,然后单击“安装”。 我已经在机器上安装了扩展程序:

Search and Install the Python extension for VS Code

As soon as the Python extension has been installed, you can see that a “Run” button on the extreme right-hand top appears using which you can run the python file directly within the integrated terminal in VS Code. Also, another thing that you can notice here is that you get several options as to how would you like to run the open python file:

安装Python扩展程序后,您会看到在最右上角出现一个“ 运行 ”按钮,您可以使用该按钮在VS Code中的集成终端中直接运行python文件。 另外,您还可以在这里注意到的另一件事是,您获得了一些有关如何运行打开的python文件的选项:

Features from the Python extension

Another point that you might notice after installing this extension is that it provides us with some sort of handy documentation when we hover the cursor over a specific object. For example, in the same file, if we hover over the keyword “json”, you can now see that a small tooltip is visible, which contains information about the json module. You can scroll below and find other information regarding how to use the module and so on:

安装此扩展后,您可能会注意到的另一点是,当我们将光标悬停在特定对象上时,它为我们提供了一些方便的文档。 例如,在同一文件中,如果将鼠标悬停在关键字“ json ”上,现在可以看到一个小的工具提示,其中包含有关json模块的信息。 您可以在下面滚动并找到有关如何使用该模块的其他信息,等等:

Documentation support in VS Code

适用于Python的Visual Studio Code中的IntelliSense (IntelliSense in Visual Studio Code for Python)

Secondly, talking about IntelliSense, you can see that it has now been enabled, and you can easily get the autocomplete keywords from a drop-down. For example, if you now start typing “import”, the editor will automatically start suggesting keywords that match the word as you are typing. This helps to achieve a faster speed in writing code as you need not manually type the entire words to make the script work:

其次,在谈到IntelliSense时,您可以看到它已经启用,并且可以从下拉列表中轻松获取autocomplete关键字。 例如,如果您现在开始输入“ import ”,那么编辑器将自动开始建议与您键入的单词匹配的关键字。 这有助于提高编写代码的速度,因为您无需手动键入整个单词即可使脚本起作用:

Intellisense in Visual Studio Code for Python

Visual Studio代码中的代码片段 (Snippets in Visual Studio Code)

Snippets come in quite handy when you need to implement a block of statements in your code without having to write the entire syntax. You can consider this as a code template for a block such as an if-else block, and for statement block or a try-catch block. Let us now go ahead and try to write an if-else block by using the code snippet:

当您需要在代码中实现语句块而无需编写整个语法时,代码片段会非常方便。 你可以认为这是一个块代码模板如if-else块, 语句块或try-catch块。 现在让我们继续尝试使用代码片段编写if-else块:

Using the If Else code snippet

As soon as you select the if/else code snippet, you can see that a block of pre-defined code appears on the editor, and you can just edit the values without altering the block. You can use the “TAB” key and jump from the condition to the pass values:

选择if / else代码段后,您会看到一个预定义代码块出现在编辑器上,并且您可以编辑值而无需更改该块。 您可以使用“ TAB”键从条件跳转至通过值:

If Else Snippet

在Visual Studio Code for Python中进行调试 (Debugging in Visual Studio Code for Python)

One of the most important requirements while developing code is to set up a debugger, which can be used to execute the script line by line as the interpreter starts executing the program. It is useful to identify local variables and also potential bugs in the code by stepping over each line as the code executes. To start debugging the code, click on the Debug icon on the left-hand pane and select “Run and Debug”:

开发代码时最重要的要求之一是设置调试器,当解释器开始执行程序时,可用于逐行执行脚本。 通过在代码执行时跨过每一行来标识局部变量以及代码中的潜在错误,这很有用。 要开始调试代码,请单击左侧窗格上的“ 调试”图标,然后选择“ 运行并调试 ”:

Run and Debug Code

Select “Python File” from the Debug configuration drop-down, and that is it. You can now see that your code has been successfully executed by the debugger and the output is available on the terminal window below:

从“调试”配置下拉列表中选择“ Python文件”,就是这样。 现在,您可以看到您的代码已被调试器成功执行,并且输出在以下终端窗口中可用:

Debugging Python code in Visual Studio Code

You can also attach breakpoints to your code by clicking on the left-hand side of the line number. This will add a red dot on that line, which means that the debugger will stop on that line and wait for your command before moving forward. You can use the navigation buttons to step forward and backward through your code and also see the variable values on the left hand “Variables” pane:

您还可以通过单击行号的左侧将断点附加到代码中。 这将在该行上添加一个红点,这意味着调试器将在该行上停止并等待命令继续前进。 您可以使用导航按钮在代码中前进和后退,还可以在左侧“ 变量 ”窗格中看到变量值:

Debug session in Visual Studio Code for Python

结论 (Conclusion)

In this article, I have explained how to download and install Visual Studio Code on your local machine and how to install an extension inside VS Code. I have also explained how to set up the Visual Studio Code for Python programming by installing the necessary extensions, which offers the developers with much more flexibility and IntelliSense and helps in writing code faster in a more productive way. Although in the article, I have mentioned only one extension as I am using it, there are a plethora of extensions that you can use to set up and customize your development environment accordingly. All the extensions for Visual Studio Code can be found in the marketplace for Visual Studio Code. I will write another article with a few more important articles that we can use to make or work more productive.

在本文中,我已经解释了如何在本地计算机上下载和安装Visual Studio Code,以及如何在VS Code中安装扩展。 我还解释了如何通过安装必要的扩展来为Python编程设置Visual Studio Code,这为开发人员提供了更大的灵活性和IntelliSense,并有助于以更有效率的方式更快地编写代码。 尽管在本文中,我仅提到了一个扩展名,但可以使用大量扩展名来相应地设置和自定义开发环境。 Visual Studio Code的所有扩展都可以在Visual Studio Code市场中找到。 我将写另一篇文章,其中包含一些更重要的文章,我们可以使用它们来提高工作效率。

翻译自: https://www.sqlshack.com/setting-up-visual-studio-code-for-python-development/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值