简体   繁体   English

Microsoft Visual Code 中的差异“运行 python 文件”和“终端中的运行行”

[英]Difference 'run python file' & 'run lines in terminal' in Microsoft Visual Code

I just started a python bootcamp and am using Microsoft Visual Studio Code (latest version with Python 3.10.5) but have a couple of questions.我刚刚开始了一个 python 训练营,并且正在使用 Microsoft Visual Studio Code(Python 3.10.5 的最新版本),但有几个问题。 (apologies for the long post) (为长篇道歉)

I have the following code:我有以下代码:

def weather_condition(temperature):
    if temperature > 7:
        return "Warm"
    else:
        return "Cold"

input("What temperature: ")

To my knowledge there are three options to run the code据我所知,运行代码有三个选项

  1. Right mouse click and 'run python file in terminal鼠标右键单击并'在终端中运行python文件
  2. Select lines and press SHIFT + ENTER选择行并按 SHIFT + ENTER
  3. RUN (with or without debugging)运行(带或不带调试)

However even though the script is the same, each choice shows a complete different result in the terminal.然而,即使脚本相同,每个选项在终端中都会显示完全不同的结果。

  1. If I choose to run the python file, it shows the following error in the terminal:如果我选择运行 python 文件,它会在终端中显示以下错误:

terminal error message终端错误信息

>>> & C:/Users/..../AppData/Local/Microsoft/WindowsApps/python3.10.exe d:/..../_SCRIPTING_/Python/Python001/user_input2.py
  File "<stdin>", line 1
    & C:/Users/fine/AppData/Local/Microsoft/WindowsApps/python3.10.exe d:/..../_SCRIPTING_/Python/Python001/user_input2.py
    ^
SyntaxError: invalid syntax
  1. If I choose select lines (same lines used as #1),如果我选择选择行(与 #1 使用的行相同),

selected lines选定的行

it runs the script but it displays the entire script run process in the terminal (which doesnt happen on the teacher's visual code:它运行脚本,但它在终端中显示整个脚本运行过程(这不会发生在教师的可视化代码中:

mine:矿:

 >>> def weather_condition(temperature):
 ...     if temperature > 7:
 ...             return "Warm"
 ...     else:
 ...             return "Cold"
 ...
 >>> input("What temperature: ")
 What temperature: 

teachers: teacher's screen老师:老师的屏幕

  1. And last but not least is the Run script (with or without debug).最后但并非最不重要的是运行脚本(带或不带调试)。

debug run调试运行

Which opens a completely new Python 'debug' terminal.这会打开一个全新的 Python“调试”终端。 Here the script runs normally (it seems) and looks more like the teacher's version although his screen doesn't show 'debug' or the small toolbar这里脚本运行正常(看起来)并且看起来更像老师的版本,尽管他的屏幕没有显示“调试”或小工具栏

small toolbar小工具栏

anywhere in his visual code.在他的视觉代码中的任何地方。

A. So what is the difference between each of the choices? A. 那么每个选择之间有什么区别?

B. Which of the 3 should I be using? B. 我应该使用 3 个中的哪一个?

C. Why does the first option give an error even though the script is written correctly? C. 为什么第一个选项即使脚本编写正确也会出错?

The three options you mention are all specific to VSCode, but you're right that they are intended to give you different ways to run a script.您提到的三个选项都是 VSCode 特有的,但您说得对,它们旨在为您提供运行脚本的不同方式。

To answer your questions:要回答您的问题:

A. So what is the difference between each of the choices? A. 那么每个选择之间有什么区别?

The first option attempts to start Python in your terminal in VSCode, running a command like:第一个选项尝试在 VSCode 的终端中启动 Python,运行如下命令:

& "C:/Program Files/Python310/python.exe" c:/project/hello.py

The error message you are seeing is because in your terminal, Python was already running and VSCode just copies and pastes the above into the terminal, expecting it to land on a waiting terminal prompt, not on the Python interactive prompt.您看到的错误消息是因为在您的终端中,Python 已经在运行,而 VSCode 只是将上面的内容复制并粘贴到终端中,期望它出现在等待的终端提示符上,而不是 Python 交互式提示符上。 If you ran exit() first, to close Python and return to the terminal prompt, and then tried again, it would work.如果您先运行exit()以关闭 Python 并返回终端提示符,然后再试一次,它将起作用。

What the command means is to start that version of Python, running your script, in the current working directory of the terminal, in the current environment of the terminal.该命令的含义是在终端的当前工作目录中,在终端的当前环境中启动该版本的 Python,运行您的脚本。 (in the background, returning control to you, hence the & ) (在后台,将控制权返回给您,因此&

The second option does something similar, but instead of issuing the command above, just puts everything you selected on the clipboard and pastes it into the terminal.第二个选项执行类似的操作,但不是发出上面的命令,而是将您选择的所有内容放在剪贴板上并将其粘贴到终端中。 If that happens to be running Python on the interactive prompt, and the text selected is actually Python code, your script may work (depending on the code, and what was run before).如果碰巧在交互式提示下运行 Python,并且所选文本实际上是 Python 代码,那么您的脚本可能会工作(取决于代码以及之前运行的内容)。 If it was sitting on the terminal prompt, it won't because Windows, Linux or Mac OS doesn't understand Python without an interpreter.如果它位于终端提示符上,则不会因为 Windows、Linux 或 Mac OS 在没有解释器的情况下无法理解 Python。

The third option is very similar to the first, but instead of just dumping the simple run command in the terminal, it instead adds a few more commands, changing drive and directory and then trying to start the script.第三个选项与第一个选项非常相似,但不是将简单的运行命令转储到终端中,而是添加了更多命令,更改驱动器和目录,然后尝试启动脚本。 It still tries to use the active terminal for it though and it will fail (just as the first option) if Python happened to be running there already.尽管如此,它仍然会尝试使用活动终端,如果 Python 碰巧已经在那里运行,它将失败(就像第一个选项一样)。

So, the 1st and 3rd are very similar, but completely different from the 2nd, which is trying to paste Python code instead of terminal commands.因此,第 1 次和第 3 次非常相似,但与第 2 次完全不同,后者试图粘贴 Python 代码而不是终端命令。

B. Which of the 3 should I be using? B. 我应该使用 3 个中的哪一个?

Depends on what you need.取决于你需要什么。 If you have a few lines of code you just want to see the effect of, you can use the 2nd method, assuming the lines of code will work in context of what you may have run before.如果你有几行代码只是想看看效果,你可以使用第二种方法,假设这些代码行可以在你之前运行的上下文中工作。

If you just want to run a script, it depends on where it needs to be run.如果你只是想运行一个脚本,这取决于它需要在哪里运行。 VSCode gives you some more options to set up your environment for the script to run successfully, but it doesn't give you as much control as something like PyCharm (then again, it's also a lot smaller and quicker to start up than PyCharm and has fewer confusing complicated controls - it's a matter of taste and need). VSCode 为您提供了更多选项来设置环境以使脚本成功运行,但它并没有像 PyCharm 那样为您提供更多的控制权(再说一次,它也比 PyCharm 更小更快启动并且有更少令人困惑的复杂控件 - 这是品味和需求的问题)。

C. Why does the first option give an error even though the script is written correctly? C. 为什么第一个选项即使脚本编写正确也会出错?

As indicated above, it only generates that error when the terminal has an interactive Python session running (you can tell from the >>> prompt).如上所述,它仅在终端运行交互式 Python 会话时才会生成该错误(您可以从>>>提示符中看出)。 The third one would give you a similar error if you tried it in that setting.如果您在该设置中尝试,第三个会给您类似的错误。

Similarly, the second option will cause problems if you don't have an interactive session started (ie running some python.exe ).同样,如果您没有启动交互式会话(即运行一些python.exe ),第二个选项将导致问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM