VS Code 无法识别 Python 虚拟环境 [英] VS Code Doesn't Recognize Python Virtual Environment

查看:25
本文介绍了VS Code 无法识别 Python 虚拟环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Mac 上使用 VS Code 编写 Python 代码.我在我的项目文件夹中创建了一个名为venv"的虚拟环境,并在我的项目文件夹中打开了 VS Code.我可以在资源管理器窗格中看到 venv 文件夹.但是,如果我将包安装到虚拟环境中,并尝试将包导入 Python 模块然后运行该模块,VS Code 会引发 ModuleNotFound 错误,提示没有模块.

I'm using VS Code on a Mac to write Python code. I've created a virtual environment named 'venv' inside my project folder and opened VS Code in my project folder. I can see the venv folder in the Explorer pane. However, if I install a package into the virtual environment and I try to import the package into a Python module and then run the module, VS Code raises a ModuleNotFound error saying there is no module.

我按照 VS Code 文档中的说明进行操作在 VS Code 中使用 Python 环境通过打开命令面板,选择 Python:选择解释器,然后选择venv/bin/folder".但是当我这样做时,我收到此错误:

I followed the instructions in the VS Code document Using Python environments in VS Code by opening the Command Palette, choosing Python: Select Interpreter, and then selecting "venv/bin/folder". But when I do that, I get this error:

Failed to set 'pythonPath'. Error: Unable to write into folder settings.  Please open the 'my_project' folder settings to correct errors/warnings in it and try again.

这些文件夹设置"是什么?我在上面引用的文档中没有看到任何关于我的虚拟环境目录的文件夹设置的内容.

What are these "folder settings?" I don't see anything in the document I cited above that talks about a folder setting for my virtual environment directory.

环境:
VS 代码 1.35.1
Python for VS Code 0.2.3
Python 3.7.1

Environment:
VS Code 1.35.1
Python for VS Code 0.2.3
Python 3.7.1

更新

根据@khuynh 的建议,我打开 settings.json 并发现一个错误,即我试图用//"注释掉一行.我没有意识到 JSON 文件不能包含注释.

Taking @khuynh advice, I opened settings.json and found one error which was that I tried to comment out a line with "//". I didn't realize that JSON files can't include comments.

取出那条线后,我再次运行Python:选择解释器",但这次是一个标签,上面写着 .vscode >settings.json >>code-runner.executorMap.python 出现在顶部.该选项卡包含以下代码:

After taking that line out, I ran "Python: Select Interpreter" again but this time a tab that says .vscode > settings.json >> code-runner.executorMap.python at the top appeared. The tab contained the following code:

    {
        "python.pythonPath": "/usr/local/bin/python3"
        "code-runner.executorMap.python": "python3 -u"
    }

code-runner.executorMap.python"下方有一条红色波浪线,下方的问题窗口显示未知配置设置".我不明白这个设置有什么问题.

There is a red squiggly line beneath "code-runner.executorMap.python" and the Problems window below says "Unknown configuration setting." I don't understand what's wrong with this setting.

推荐答案

如果工作区设置文件中有错误,则选择 Python 解释器不起作用.您在python.pythonPath"行之后缺少一个逗号.

Selecting a Python Interpreter does not work if you have errors in your workplace settings file. You are missing a comma after your "python.pythonPath" line.

(我看到其他人在评论中回答了这个问题,但我想发布一个答案,让其他人更容易找到.)

(I see that others answered this in comments, but I wanted to post an Answer to make this easier for others to find.)

这篇关于VS Code 无法识别 Python 虚拟环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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