Visual Studio Code中的Python入门

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!

TLDR (TLDR)

Install the Python extension to get intellisense and shortcuts for running Pythin in Visual Studio Code.

安装Python扩展程序以获得智能感知和在Visual Studio Code中运行Pythin的快捷方式。

Check out Learn Visual Studio Code to learn everything you need to know about about the hottest editor in Web Development for just \$10!

查看“ 学习Visual Studio Code” ,只需10美元即可了解有关Web开发中最热门的编辑器的所有信息。

在您的机器上安装Python ( Install Python on Your Machine )

The first thing you'll need to take care of is installing Python on your machine. To do that, you can head to official Python download page and walk through the install process.

您需要注意的第一件事是在计算机上安装Python。 为此,您可以转到Python的官方下载页面并逐步完成安装过程。

Mac安装详细信息 (Mac Install Details)

If you are using a Mac, you probably already have a version of Python installed, but it is an older version (2.7 for me). If you install the latest version (currently 3.x), you'll have two different version installed. Applications on your mac are dependent on the older, so you can't get rid of it.

如果您使用的是Mac,则可能已经安装了Python版本,但是它是旧版本(对我来说是2.7)。 如果您安装最新版本(当前为3.x),则将安装两个不同的版本。 Mac上的应用程序取决于较旧的版本,因此您无法摆脱它。

Because of this, to run your python from the terminal you would need to use the "python3" command. Alternatively, you could set an alias, so that any time you type "python" you're actually using the "python3" command. To setup this alias, you can type this into your terminal. You can use this post for reference.

因此,要从终端运行python,您需要使用“ python3”命令。 另外,您可以设置一个别名,这样,每次键入“ python”时,您实际上就在使用“ python3”命令。 要设置此别名,您可以在终端中键入此别名。 您可以将此帖子用作参考。

alias python = 'python3'

从内置终端运行Python ( Run Python From the Built in Terminal )

A typical workflow for running your Python files will look like this.

运行Python文件的典型工作流程如下所示。

  • Edit and Save your python file

    编辑并保存您的python文件
  • Press the up arrow in terminal to get your last run command

    按终端中的向上箭头以获取您的最后一个运行命令
  • Press enter to run your python file and see the results

    按Enter运行您的python文件并查看结果

To get started I recommend creating a folder on your Desktop (or a directory that you're comfortable with) to put your Python files in for now. With that directory created and Python installed, now open up Visual Studio Code.

首先,我建议您在桌面上创建一个文件夹(或您喜欢的目录)以暂时放置Python文件。 创建该目录并安装Python之后,现在打开Visual Studio Code。

Inside of VS Code, open the folder/directory that you just created by going to File->Open and then choosing that directory. After that, you'll see your folder open in the explorer window on the left.

在VS 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”。

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

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

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

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

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 (on Mac), which will print a list of files in the directory.

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

ls

Now, you can run your python file with the following command.

现在,您可以使用以下命令运行python文件。

python<filename>

After running, you should see "Hello World" printed out in the console.

运行后,您应该会在控制台中看到“ Hello World”的输出。

安装Python扩展 ( Install the Python Extension )

That process wasn't so bad, but working with Python gets a lot easier with 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扩展,使用Python变得容易得多。 要安装扩展程序,请打开左侧的扩展程序菜单(图标看起来像正方形内的正方形)并搜索Python。

The Python extension provides amazing intellisense, auto-completion, and muchmore!

Python扩展提供了惊人的智能感知,自动完成功能以及更多功能!

It will be the first one that pops up, and you can click on it to view the extension details. Should look like this. Go ahead and click install.

这将是第一个弹出的窗口,您可以单击它以查看扩展名详细信息。 应该看起来像这样。 继续并单击安装。

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

安装后,您可能需要重新加载,所以继续进行。

After you restart, you can now take advantage of several awesome features of this extension.

重新启动后,您现在可以利用此扩展程序的一些强大功能。

  • Intellisense

    智能感知
  • Autio-completion

    自完成
  • Shortcuts for running Python Files

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

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

To get a sense for intellisense, create an empty array called "list". Then type "list" followed by a period and notice that a bunch of 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.

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

You can also get intellisense when importing modules in Python. Notice that as I 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!

如果您随后开始使用随机模块,那么您将继续获得对该模块可以使用的功能的智能感知!

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

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

使用快捷方式运行Python代码 ( Use Shortcuts to Run Python Code )

If you want to spice things up a bit 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 if you're interested.

如果您想在Python文件中增加一些趣味性,下面是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 our fancy new piece of code taken care of, let's explore a new way to run our python file. The typical first workflow for working with Python files (as we talked about previously) 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 basically 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.

使用快捷方式后,您可以在控制台中看到气泡排序输出。

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 pallette using the shortcut Command+Shift+P on Mac or Control+Shift+P on Windows and use the "Python Start REPL" command.

您还具有打开Python REPL的快捷方式,您可以在其中快速将Python代码直接输入到控制台中并查看输出。 在Mac上使用快捷键Command + Shift + P或在Windows上使用Control + Shift + P打开命令调色板,然后使用“ Python Start REPL ”命令。

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

输入打印命令后,您将立即在控制台中看到“ Hello World”。

回顾 ( Recap )

Python is incredibly popular language, and it has some really good support in VS Code. By installing the "Python" extension, you'll get Python intellisense, auto-completion, and other useful misc. shortcuts.

Python是令人难以置信的流行语言,并且在VS Code中提供了一些非常好的支持。 通过安装“ Python”扩展,您将获得Python智能感知,自动完成和其他有用的杂项。 捷径。

Curious...Are you using Python? If so, are you using VS Code and the Python extension? Any other tools you're using that you find useful?

好奇...您正在使用Python吗? 如果是这样,您是否正在使用VS Code和Python扩展? 您是否发现其他有用的工具?

翻译自: https://scotch.io/tutorials/getting-started-with-python-in-visual-studio-code

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值