VSCode 更改默认 python 版本 [英] VSCode change default python version

查看:160
本文介绍了VSCode 更改默认 python 版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 VSCode 上编码并且没有指定 python 版本时,它被设置为 python2.7.17

When I code on VSCode and don't specify a python version it is set to python2.7.17

我想默认设置为python3.6

I would like to set it by default to python3.6

我尝试通过在脚本顶部添加以下内容来这样做:#!/usr/bin/python3.6 但它不起作用

I tried doing so by adding this at the top of my script : #!/usr/bin/python3.6 But it is not working

#!/usr/bin/python3.6
from flask import Flask, render_template, request, session
from datetime import timedelta
import sys
print(sys.version)

此代码输出:

2.7.17 (default, Apr 15 2020, 17:20:14) 
[GCC 7.5.0]

我能输出3.6版本吗?

I can it output the version 3.6 ?

我的 settings.json 看起来像这样:

My settings.json looks like this :

即使使用指定的路径,sys.version 也会输出 python2

Even with a specified path the sys.version outputs python2

推荐答案

在 VSCode 的命令面板中选择选项

In VSCode's command palette choose the option

Python:选择解释器

然后选择您要使用的解释器.这将自动将设置正确添加到您工作区的 .vscode 文件夹中的 settings.json 文件中.

And choose the interpreter you want to use. This will automatically add the setting correctly to your settings.json file in your .vscode folder for your workspace.

这将负责 VSCode 中的 Python 管理.

This will take care of Python management in VSCode.

现在开始设置在尝试运行脚本时由您的操作系统/终端使用的正确 python.

Now coming to setting the right python to be used by your os/terminal when trying to run scripts.

由于您使用的是 linux,请确保您的 python 是链接的正确版本

Since you are using linux, make sure your python is the right version thats linked

  1. 在终端上检查 python 版本 - python --version
  2. 执行 sudo update-alternatives --install/usr/bin/python python/usr/bin/python3.8 1 <<正确验证您的路径
  3. 然后运行 ​​sudo update-alternatives --config python
  4. 选择python版本
  5. 再次尝试 python --version 并检查
  1. Check python version on terminal - python --version
  2. execute sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1 << verfiy your paths correctly
  3. then run sudo update-alternatives --config python
  4. Select the python version
  5. Try python --version again and check

如果在 Windows 上,请确保在路径环境变量中将 Python 3.8.x 的路径设置为更高的优先级.Py3 应该在你的其他路径之上

If on Windows make sure path to Python 3.8.x is set at a higher priority in your Path Environment Variable. Py3 should be above your other paths

这篇关于VSCode 更改默认 python 版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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