Visual Studio Code 终端在 Powershell 中继续运行 Python 脚本

wol*_*tza 5 python powershell python-3.x visual-studio-code

我最近安装了 Python 和 Visual Studio Code。参加完入门课程后,我编写了一个基本脚本并在 Visual Studio Code 中运行它。就在那时,我注意到 Visual Studio 代码中的 Python 设置方式存在问题。问题:当我启动 Visual Studio Code 并打开 python 文件时,终端默​​认为“C:\Users\my_name\Documents Python”(这是存储我的 python 文件的文件夹)。据我了解,当你使用Python时,提示符应该是“>>>”。我能够运行我的脚本,但无法在终端中运行任何其他 Python 代码(即像 z = 5 这样简单的代码)。如果我输入“Python”,系统会提示我“>>>”,但无法再运行我的脚本。

我认为这是一个安装问题,所以我卸载并重新安装了 Python 和 Visual,但问题仍然存在。

我尝试在系统设置下将程序安装位置的Python文件路径添加到Windows环境中,并在重新安装Python时单击“添加到路径”,但这些解决方案似乎都不起作用。

当基本的 python 代码(即 z=5)似乎不起作用但脚本运行良好时,我收到以下错误消息:

PS C:\Users\my_name\Documents\Python 2> z=5
z=5 : The term 'z=5' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ z=5
+ ~~~
    + CategoryInfo          : ObjectNotFound: (z=5:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Run Code Online (Sandbox Code Playgroud)

下面是我切换到 python 并尝试运行我的脚本后收到的错误消息

PS C:\Users\my_name\Documents\Python 2> python
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> & C:/Users/my_name/AppData/Local/Programs/Python/Python37-32/python.exe "c:/Users/my_name/Documents/Python 2/new2.py"
  File "<stdin>", line 1
    & C:/Users/my_name/AppData/Local/Programs/Python/Python37-32/python.exe "c:/Users/my_name/Documents/Python 2/new2.py"
    ^
SyntaxError: invalid syntax
>>>
Run Code Online (Sandbox Code Playgroud)

wol*_*tza 1

我明白问题出在哪里了。视觉代码认为 Python 安装在保存我的 .py 文件的目录中,而不是程序文件下的实际位置。必须在设置下编辑路径。

  • 好的,但是你是怎么做的呢?(请出示代码) (2认同)