如何在Visual Studio Code中开始使用Python

介绍 (Introduction)

Python is one of the most popular and easy to learn languages, which is why it is often one of the first languages you learn. Let’s see how to work with and run Python inside of Visual Studio Code.

Python是最流行和易于学习的语言之一,这就是为什么它通常是您学习的第一门语言之一的原因。 让我们看看如何在Visual Studio Code中使用和运行Python。

In this tutorial you’ll install the Python extension then use intellisense and shortcuts to run your Python code.

在本教程中,您将安装Python扩展,然后使用智能感知和快捷方式来运行Python代码。

先决条件 (Prerequisites)

第1步-从内置终端运行Python (Step 1 — Running Python From the Built-in Terminal)

With Python installed and your local programming environment set up, open Visual Studio Code.

安装Python并设置本地编程环境后,打开Visual Studio Code。

Inside of Visual Studio Code, open the directory you’re working in by going to File -> Open and selecting the directory. After that, you’ll see your folder open in the explorer window on the left.

在Visual Studio Code中,通过转到文件 -> 打开并选择目录来打开您正在使用的目录。 之后,您将在左侧的资源管理器窗口中看到文件夹打开。

With the directory open, you can create your first Python file (.py extension) with some code to print "Hello World".

打开目录后,您可以使用一些代码创建第一个Python文件(扩展名为.py )以打印"Hello World"

Save the file.

保存文件。

Now that you have your Hello World code ready, we can run it by using the built-in terminal in Visual Studio Code. If if is not open already, you can open it by going to View -> Terminal or use the shortcut, CTRL + ~.

现在您已经准备好Hello World代码,我们可以使用Visual Studio Code中的内置终端来运行它。 如果尚未打开,则可以通过查看 -> 终端或使用快捷键CTRL + ~打开它。

The terminal that you just opened will automatically start in the current directory that you are editing in Visual Studio Code. This is exactly why we created and opened a directory before getting started. We can prove this by running the following command:

您刚打开的终端将自动在您正在Visual Studio Code中编辑的当前目录中启动。 这就是为什么我们在开始之前创建并打开目录的原因。 我们可以通过运行以下命令来证明这一点:

  • pwd

    密码

This command will print the path to the current directory. From there, you can verify that your Python file is also inside of the current directory by running the following command to print a list of files in the directory:

此命令将打印当前目录的路径。 从那里,您可以通过运行以下命令以打印目录中的文件列表,来验证您的Python文件是否也在当前目录中:

  • ls

    ls

Now, you can run your Python file with the following command:

现在,您可以使用以下命令运行Python文件:

  • python filename

    python 文件名

After running, you’ll see Hello World printed out in the console.

运行后,您会在控制台中看到Hello World的信息。

第2步-安装Python扩展 (Step 2 — Installing the Python Extension)

We can streamline the process of working with Python in Visual Studio by installing the Python extension created by Microsoft. To install the extension, open up the extension menu on the left (the icon looks like a square inside of a square) and search Python.

通过安装Microsoft创建的Python扩展,我们可以简化在Visual Studio中使用Python的过程。 要安装扩展程序,请打开左侧的扩展程序菜单(图标看起来像正方形内的正方形)并搜索Python。

It will be the first one that pops up, and you can click on it to view the extension details and click Install.

这将是第一个弹出的窗口,您可以单击它以查看扩展详细信息,然后单击Install

After installing, you might need to reload, so go ahead and do that.

安装后,您可能需要重新加载,所以继续执行此操作。

After you restart, you can now take advantage of the Python extension’s features:

重新启动后,您现在可以利用Python扩展程序的功能:

  • IntelliSense

    智能感知
  • Auto-completion

    自动补全
  • Shortcuts for running Python Files

    运行Python文件的快捷方式
  • Additional info on hovering Python variables, functions, and so on.

    有关悬停Python变量,函数等的其他信息。

To start working with IntelliSense, create an empty array called list.

要开始使用IntelliSense,请创建一个名为list的空数组。

list = []

Then following type list. followed by a period and notice that some information pops up. The extension is providing you all the functions and properties of a list that you can use.

然后跟随类型list. 接着是句点,请注意会弹出一些信息。 该扩展为您提供了可以使用的列表的所有功能和属性。

If you want to use one of those functions, you can press ENTER or TAB to auto-complete that function name. This means that don’t have to memorize every function in Python because the extension will give you hints as to what is available. Notice also that it shows you a brief description of what the function does and what parameters it takes.

如果要使用这些功能之一,则可以按ENTERTAB键自动完成该功能名称。 这意味着不必记住Python中的每个函数,因为扩展名会提示您可用的功能。 还要注意,它向您显示了该函数的功能及其所用参数的简要说明。

You can also get intellisense when importing modules in Python. Notice if you type random, intellisense pops up to complete the name of the module as well as providing some background info on what it does.

您也可以在Python中导入模块时获得智能感知。 请注意,如果键入random ,则会弹出intellisense来完成模块的名称,并提供有关其功能的一些背景信息。

If you then start to use the random module, you’ll continue to get intellisense for functions that you can access with that module.

然后,如果您开始使用random模块,则将继续获得对该模块可访问的功能的智能感知。

Lastly, you can hover on existing variables, module imports, and so on, for additional information whenever you need it.

最后,您可以将鼠标悬停在现有变量,模块导入等上,以在需要时获取其他信息。

第3步-使用快捷方式运行Python代码 (Step 3 — Using Shortcuts to Run Python Code)

If you want to do more in your Python file, here’s a short snippet for the Bubble Sort algorithm. It calls the bubble_sort function and prints out the result. You can copy this code into your file:

如果您想在Python文件中执行更多操作,以下是Bubble Sort算法的一小段代码。 它调用bubble_sort函数并打印出结果。 您可以将此代码复制到文件中:

def bubble_sort(list):
    sorted_list = list[:]
    is_sorted = False
    while is_sorted == False:
        swaps = 0
        for i in range(len(list) - 1):
            if sorted_list[i] > sorted_list[i + 1]: # swap
                temp = sorted_list[i]
                sorted_list[i] = sorted_list[i + 1]
                sorted_list[i + 1] = temp
                swaps += 1
        print(swaps)
        if swaps == 0:
                is_sorted = True
    return sorted_list

print(bubble_sort([2, 1, 3]))

With this new piece of code, let’s explore a new way to run our Python file. The typical first workflow for working with Python files is to save your file and then run that Python file in the terminal. With the Python extension, there are a few shortcuts to help with this process.

通过这段新代码,让我们探索一种运行Python文件的新方法。 使用Python文件的典型的第一个工作流程是保存文件,然后在终端中运行该Python文件。 使用Python扩展,有一些快捷方式可以帮助完成此过程。

Inside of any Python file, you can right click in the editor and choose Run Python File In Terminal. This command will do each of the individual steps that we talked about before.

在任何Python文件中,您都可以右键单击编辑器,然后选择在终端中运行Python文件 。 该命令将完成我们之前讨论的每个步骤。

After using the shortcut, you can see the bubble_sort output in your console.

使用该快捷方式后,您可以在控制台中看到bubble_sort输出。

You also have a shortcut to open the Python REPL where you can quickly type Python code directly into your console and see the output. Open the command palette using the shortcut CMD + SHIFT + P on Mac or CTRL + SHIFT + P on Windows and select Python Start REPL.

您还具有打开Python REPL的快捷方式,您可以在其中快速将Python代码直接输入到控制台中并查看输出。 在Mac上使用快捷方式CMD + SHIFT + P或在Windows上使用CTRL + SHIFT + P打开命令面板,然后选择Python Start REPL

After typing in a print command, you will see Hello World immediately displayed in the console.

输入print命令后,您将看到Hello World立即显示在控制台中。

结论 (Conclusion)

Python is an incredibly popular language with strong support in Visual Studio Code. By installing the Python extension, you’ll get Python intellisense, auto-completion, and other useful miscellaneous shortcuts.

Python是一种非常流行的语言,在Visual Studio Code中提供了强大的支持。 通过安装Python扩展,您将获得Python智能感知,自动完成和其他有用的其他快捷方式。

翻译自: https://www.digitalocean.com/community/tutorials/getting-started-with-python-in-visual-studio-code

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值