首页 > 解决方案 > 我在 Mac 中 Visual Studi Code 中的 Python 代码一直运行相同的代码

问题描述

所以,我是 Python 的新手(通常是编程),我确实在 Visual Studio 代码中下载了 Python 3.7、Visual Studio Code、Python、代码运行器扩展,并创建了 python 文件并运行了一个代码(打印('me')),之后我对代码进行了更改,例如 print ('him') ,它仍然运行 'me' 而不是 'him' 。我不确定为什么。只有当我打开一个新文件并保存它时它才有效。但我不想那样做。我想对现有的 python 文件进行更改,而不是每次都创建一个新文件。

我确实按照该用户在 Youtube 上给出的安装说明进行操作。看来我安装正确。

https://www.youtube.com/watch?v=saz6A20E9Mk

标签: pythonmacosvisual-studio-code

解决方案


It seems like you did not actually save your changes. It is important to understand what is happening when you "edit" a file. Your file is "saved" on your computer's hard drive (somewhere). That is what is being run when you press run -- the saved file.

If you have your "old code", your (print ('me')), change that to (print ('hello world')) and navigate to File -> Save in Visual Studio Code. From there, try running that file from within VSCode. See what happens.

When you open it in VisualStudioCode, you are not actually editing that file that is saved on your hard drive! Your computer has placed a temporary copy of it in your computer's memory, which is what is being edited. Therefore, if you want to run your edited code, you need to Save the file you are editing. It is common practice to use keyboard shortcuts, like Commmand + S to do that easier.

If you are interested in enabling autosave, to avoid needing to manually save, you can find more details on how to do that at this visual studio documentation link


推荐阅读