简体   繁体   English

如何让 VSCode 将 Python3 识别为我的默认值

[英]How do I get VSCode to recognize Python3 as my default

I have python3 install on my Mac and I'm in the terminal, I use python3 by default.我在我的 Mac 上安装了 python3 并且我在终端中,默认情况下我使用 python3。 However when I'm in VSCode it is not recognizing python3 as my default, it's still pulling in python2.7.但是,当我在 VSCode 中时,它并没有将 python3 识别为我的默认值,它仍在使用 python2.7。

Here is a screenshot of my VScode environement: I have code-runner with python3 selected as well as my interpreter as 3.8这是我的 VScode 环境的屏幕截图:我选择了带有 python3 的代码运行器以及我的解释器 3.8

在此处输入图片说明

When I run my code, I get this annoying error:当我运行我的代码时,我收到了这个烦人的错误:

Exception: Python in worker has different version 2.7 than that in driver 3.8, PySpark cannot run with different minor versions. Please check environment variables PYSPARK_PYTHON and PYSPARK_DRIVER_PYTHON are correctly set.

I have to call export PYSPARK_PYTHON=python3 to get my code to run, but I've already got that built in my bash.profile.我必须调用export PYSPARK_PYTHON=python3才能运行我的代码,但我已经在我的 bash.profile 中构建了它。

What am I missing in order to get this to run properly?为了让它正常运行,我缺少什么?

Not clear why you're exporting things unless you are running code from the terminal window.不清楚为什么要导出内容,除非您从终端窗口运行代码。 Your terminal is not connected to the vscode settings.您的终端未连接到 vscode 设置。

You need to run scripts actually from vscode if you want that environment to be applied如果要应用该环境,则需要实际从 vscode 运行脚本

First Check Please import you packages through the ".py" files in vs code and check again if it works.首先检查请通过 vs 代码中的“.py”文件导入你的包,然后再次检查它是否有效。 If it still doesn't work then follow the steps below.如果它仍然不起作用,请按照以下步骤操作。

Method 1: Please check your environment on vs code;方法一:请在vs code上检查你的环境; if it is set to the python version you intend to use.如果它设置为您打算使用的python版本。 This problem could be because your environment on vs code is set aa python version which you don't want to use, henceforth you can follow the method mentioned earlier in order to fix the error.这个问题可能是因为你在 vs code 上的环境设置了一个你不想使用的 python 版本,此后你可以按照前面提到的方法来修复错误。

Method 2: If this doesn't work change you python interpreter through command palette (this will be accessible only when a python file is opened or a folder containing python files).方法 2:如果这不起作用,请通过命令面板更改您的 python 解释器(仅当打开 python 文件或包含 python 文件的文件夹时才可访问)。

IMPORTANT: In order for the second method to work, you'll need to download this extension in vs code, only then you can change your interpreter.重要提示:为了使第二种方法起作用,您需要在 vs 代码中下载此扩展程序,然后才能更改解释器。

Link:关联:

https://marketplace.visualstudio.com/items?itemName=ms-python.vscode-pylance https://marketplace.visualstudio.com/items?itemName=ms-python.vscode-pylance

settings.json添加以下代码并重新加载窗口以使设置生效:

"python.defaultInterpreterPath": "\path to python3\",

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM