VSCode:术语 'python' 无法识别...但 py 有效
VSCode: The term 'python' is not recognized...but py works
我刚刚在 VS Code 上安装了 python,但我无法使用 python
命令 运行 任何 python 代码。
python 命令:
运行 代码似乎默认为 运行 python
命令,但它无法识别它。
当我右键单击并选择 Run Code
时,它会抱怨:
'python' is not recognized as an internal or external command, operable program or batch file
同样适用于手动 运行ning python main.py
。
当我打开提升的 PowerShell 和 运行 python
时,它会抱怨:
python : The term 'python' 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
+ python
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (python:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
py 命令:
它不会尝试使用 py
命令,但它会识别它。 当我手动调用 py main.py
时,它起作用了。
当我手动执行 >py main.py
时,它会写入我的 Goodbye, World!
问题:
如何使用 CodeRunner 的右键单击功能 (Run Code
) 在 VS Code 中实现 compile/run?
我已经在 PATH 中同时拥有 Python 文件夹及其脚本文件夹。
我正在使用 VS Code 1.27.2
,我已经在我的机器上安装了 python 3.7.0
,并选中了它的安装程序复选框以自动添加环境变量。 (路径没问题)
我还在 VS Code 上安装了:ms-python.python
、tht13.python
和 formulahendry.code-runner
扩展。
这是我的 main.py
代码:
print("Goodbye, World!")
Python 的 Windows 安装程序默认不会将 python
放在您的路径中(安装过程中有一个复选框可以添加它)。确保您选择了由 运行 Select Interpreter
安装的解释器并选择您想要的解释器(扩展程序将通过注册表找到它们)。
事实证明,在 VS 上安装 ms-python.python
和 tht13.python
和 formulahendry.code-runner
扩展后,我只需要 重新启动计算机 编码并在 PATH
.
中添加 python 的 Scripts
文件夹
现在 py
和 python
命令都可以在任何地方使用,例如右键菜单中的 cmd
或 Run Code
。
安装 Python 扩展并更改 PATH 以包含 Python 及其脚本文件夹后重新启动您的 PC 将会有所帮助。为我工作
您需要先确认 python 是否已安装,因为 运行 python/python3 在 terminal/cmd.
如果它 运行 存在并且它不在 VS Code 中 运行 则重新启动系统以反映更改。
如果 运行 在 terminal/cmd 中也没有,那么首先检查 python 的目录是否放在环境变量中。
如果您已经设置了路径变量,请在命令提示符下测试相同的命令,看看它是否有效。如果是这样,只需通过 运行 从您的 vs code PowerShell 终端更新 PowerShell 的 path
设置:
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" +
[System.Environment]::GetEnvironmentVariable("Path","User")`
这个技巧可以让你省去很多重启的时间。
我在重新 Windows 重新安装后也遇到了这个问题,vscode 无法识别 PS 终端中的 python
或 pip freeze
之类的命令。
重新安装 python 和 vscode 后,我阅读了 python vscode 的教程:https://code.visualstudio.com/docs/python/python-tutorial。创建一个新的 venv 对我有用 py -3 -m venv .venv
,然后导航到 venv:.venv\scripts\activate
。在新的 venv 中,所有 python 命令都正常工作。
将 Python 路径(例如 C:\Users\johndoe\AppData\Local\Programs\Python\Python39)添加到 %PATH% 环境变量
我刚刚在 VS Code 上安装了 python,但我无法使用 python
命令 运行 任何 python 代码。
python 命令:
运行 代码似乎默认为 运行 python
命令,但它无法识别它。
当我右键单击并选择 Run Code
时,它会抱怨:
'python' is not recognized as an internal or external command, operable program or batch file
同样适用于手动 运行ning python main.py
。
当我打开提升的 PowerShell 和 运行 python
时,它会抱怨:
python : The term 'python' 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
+ python
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (python:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
py 命令:
它不会尝试使用 py
命令,但它会识别它。 当我手动调用 py main.py
时,它起作用了。
当我手动执行 >py main.py
时,它会写入我的 Goodbye, World!
问题:
如何使用 CodeRunner 的右键单击功能 (Run Code
) 在 VS Code 中实现 compile/run?
我已经在 PATH 中同时拥有 Python 文件夹及其脚本文件夹。
我正在使用 VS Code 1.27.2
,我已经在我的机器上安装了 python 3.7.0
,并选中了它的安装程序复选框以自动添加环境变量。 (路径没问题)
我还在 VS Code 上安装了:ms-python.python
、tht13.python
和 formulahendry.code-runner
扩展。
这是我的 main.py
代码:
print("Goodbye, World!")
Python 的 Windows 安装程序默认不会将 python
放在您的路径中(安装过程中有一个复选框可以添加它)。确保您选择了由 运行 Select Interpreter
安装的解释器并选择您想要的解释器(扩展程序将通过注册表找到它们)。
事实证明,在 VS 上安装 ms-python.python
和 tht13.python
和 formulahendry.code-runner
扩展后,我只需要 重新启动计算机 编码并在 PATH
.
Scripts
文件夹
现在 py
和 python
命令都可以在任何地方使用,例如右键菜单中的 cmd
或 Run Code
。
安装 Python 扩展并更改 PATH 以包含 Python 及其脚本文件夹后重新启动您的 PC 将会有所帮助。为我工作
您需要先确认 python 是否已安装,因为 运行 python/python3 在 terminal/cmd.
如果它 运行 存在并且它不在 VS Code 中 运行 则重新启动系统以反映更改。
如果 运行 在 terminal/cmd 中也没有,那么首先检查 python 的目录是否放在环境变量中。
如果您已经设置了路径变量,请在命令提示符下测试相同的命令,看看它是否有效。如果是这样,只需通过 运行 从您的 vs code PowerShell 终端更新 PowerShell 的 path
设置:
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" +
[System.Environment]::GetEnvironmentVariable("Path","User")`
这个技巧可以让你省去很多重启的时间。
我在重新 Windows 重新安装后也遇到了这个问题,vscode 无法识别 PS 终端中的 python
或 pip freeze
之类的命令。
重新安装 python 和 vscode 后,我阅读了 python vscode 的教程:https://code.visualstudio.com/docs/python/python-tutorial。创建一个新的 venv 对我有用 py -3 -m venv .venv
,然后导航到 venv:.venv\scripts\activate
。在新的 venv 中,所有 python 命令都正常工作。
将 Python 路径(例如 C:\Users\johndoe\AppData\Local\Programs\Python\Python39)添加到 %PATH% 环境变量