如何通过命令行在 VS Code 中访问更新的 Python 3.9? [英] How do I access updated Python 3.9 in VS Code via command line?

查看:28
本文介绍了如何通过命令行在 VS Code 中访问更新的 Python 3.9?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这周刚刚下载了 VS Code,但在访问不同版本的 Python 时遇到了问题.

I just downloaded VS Code this week and have run into a problem accessing different versions of Python.

当我通过 python set.py 从命令行运行名为 set.py 的 python 文件时,VS Code 正在访问我的 MacBook 附带的 Python 2.7 版本.当我运行 python --version 时,它确认 VS Code 使用的是 Python 2.7.16.

When I run a python file called set.py from the command line via python set.py, VS Code is accessing the Python 2.7 version that came with my MacBook. When I run python --version it confirms that VS Code is using Python 2.7.16.

但是,当我在编辑器中右键单击并选择在终端中运行 Python 文件时,它似乎使用的是更新版本 3.9.4.我已经使用 fstring(在 3.9 中可用,但在 2.7 中不可用)对此进行了测试.fstring 代码在第一种方法中引发错误,但在第二种方法中清除正常.这是我的代码:

However, when I right-click in the editor and select Run Python File In Terminal it appears to be using the updated version 3.9.4. I have tested this by using an fstring (available in 3.9 but not in 2.7). The fstring code throws an error in the first method but clears fine in the second. Here's my code:

s = set()

s.add(1)
s.add(2)
s.add(3)

print(f"The set has {len(s)} elements.")

这是我的 终端,显示了两种方法的结果.

And here's my terminal with the results of both methods shown.

有谁知道到底发生了什么,以及我如何通过命令行执行 python 文件?

Does anyone know what exactly is going on, and how I can execute python files via the command line?

TIA

规格:

我正在运行 VS Code 1.55.2 并已将 Python 3.9.4 下载到运行 Big Sur 11.2.3 的 MacBook Pro

I am running VS Code 1.55.2 and have downloaded Python 3.9.4 to my MacBook Pro running Big Sur 11.2.3

我还在 VS Code 中下载了 Python 扩展,并选择了 Python 3.9.4 64 位作为我的解释器.

I have also downloaded the Python extension in VS Code and have selected Python 3.9.4 64-bit as my interpreter.

推荐答案

在 macOS 和 linux 上,你会有一个系统 python,它往往很旧,你不想碰它,因为有系统实用程序依赖

On macOS and linux, you will have a system python that tends to be old and you don't want to touch it because there are system utilities that rely on it.

您还将拥有用户安装的 python,这里是 python 3.9.

You will also have the user-installed python, which here is python 3.9.

如果系统python是python2,那么命令python通常会调用python2,命令python3会调用python3.

If the system python is python2, then the command python will typically invoke python2 and the command python3 will invoke python3.

在 Windows 上的 VS Code 中,当您从 IDE 启动终端窗口时,它将激活您为项目选择的解释器/环境(通常通过 CTRL-SHIFT-P 然后是 Python:选择解释器).我注意到在 Linux 上它不会这样做(在 macOS 上也可能是这种情况),所以如果我想使用特定版本或环境,我需要指定或激活它作为我在终端类似于 conda activate myenvname.

In VS Code on Windows, when you launch a terminal window from the IDE, it will activate the interpreter/environment you've chosen for your project (typically by CTRL-SHIFT-P and then Python: Select Interpreter). I've noticed on Linux that it will not do this (and this may also be the case on macOS), so if I want to use a specific version or environment, I need to specify it or activate it as my first command in the terminal with something like conda activate myenvname.

与在终端中不同,如果您直接从 IDE 运行代码(使用 Run without Debugging 或 Control + F5),VS Code 将使用选定的解释器.

Unlike in the terminal, VS Code will use the selected interpreter if you run code directly from the IDE (using Run Without Debugging or Control + F5).

这篇关于如何通过命令行在 VS Code 中访问更新的 Python 3.9?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆