VS Code - pylinter 找不到模块 [英] VS Code - pylinter cannot find module

查看:30
本文介绍了VS Code - pylinter 找不到模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始在 Mac 上使用 VS Code 进行 Python 开发,但无法让 pylint 找到模块.

I started using VS Code for Python development on a Mac and cannot make pylint find a module.

这是我的项目文件夹结构:

This is my project folder structure:

project_root/
   .env
   .vscode/
       settings.json
   lib/
       # lib containing necessary modules
   sample/
       client/
           EDAMTest.py
   # many more files

我使用安装了 pylint 的 virtualenv.虚拟环境在终端中被激活.我通过终端中的 code .project_root 文件夹中启动了 code.

I use a virtualenv in which I have installed pylint. The virtual env is activated in the terminal. I started code from within project_root folder via code . in my terminal.

VS Code 说它使用了正确的解释器.我可以在左下角看到它说 Python 3.6.1 (virtualenv)

VS Code says it is using the correct interpreter. I can see on the bottom left that it says Python 3.6.1 (virtualenv)

如果我想在终端内测试 project_root/sample/client/EDAMTest.py 代码,我可以通过 export PYTHONPATH=../../lib 来完成;python EDAMTest.py 在文件夹 project_root/sample/client/ 中.

If I want to test the project_root/sample/client/EDAMTest.py code within terminal I can do it via export PYTHONPATH=../../lib; python EDAMTest.py while being in folder project_root/sample/client/.

现在如果我在 VS Code 中,打开文件 EDAMTest.pypylint 告诉我它不能从 lib 导入模块.

Now if I am in VS Code, open the file EDAMTest.py, pylint is telling me that it cannot import modules from lib.

现在我的问题:

如何在 VS Code 中将 lib 添加到 PYTHONPATH?

How can I add lib to PYTHONPATH in VS Code?

我找到了几种可能的方法:

I found several possible ways to do so:

  1. 创建一个 .env 文件(参见下面的 [1]).
  2. .vscode/launch.json 文件中指定 PYTHONPATH(见 [2])
  1. Create a .env file (see [1] below).
  2. Specify PYTHONPATH in .vscode/launch.json file (see [2])

我找到的所有可能的解决方案似乎都不起作用.

None of the possible solutions I found seem to work.

我错过了什么?

[1] 环境变量定义文件

这告诉我如何定义全局 (env) 变量.所以我指定了这个:

This tells me how to define global (env) vars. So I specified this:

PYTHONPATH="~/.virtualenvs/evernote/bin/python;lib"

但这行不通.pylint

[2] 所以我确实创建了一个 launch.json 文件,如下所示:

[2] So I did create a launch.json file like so:

{
    "name": "Python",
    "type": "python",
    "request": "launch",
    "stopOnEntry": false,
    "pythonPath": "${config.python.pythonPath}",
    "program": "${file}",
    "cwd": "${workspaceRoot}",
    "debugOptions": [
        "WaitOnAbnormalExit",
        "WaitOnNormalExit",
        "RedirectOutput"
    ],
    "env": {
        "PYTHONPATH": "~/.virtualenvs/evernote/bin/python:lib"
    }
}

---

编辑

这是一个尝试解决此问题的链接:

Here is a link that tries to address this problem:

linting 疑难解答

该链接试图解决几个可能的问题,其中一个是:

That link tries to address several possible problems, one is this:

...无法导入

建议的解决方案是:

确保 pythonPath 设置指向安装了 Pylint 的有效 Python 安装.

Ensure that the pythonPath setting points to a valid Python installation where Pylint is installed.

=> 是的,我做到了.

=> Yes, I did.

或者,将 python.linting.pylintPath 设置为所使用的 Python 解释器的适当 Pylint 版本.

Alternately, set the python.linting.pylintPath to an appropriate version of Pylint for the Python interpreter being used.

=> 我做了,仍然没有成功:

=> I did, still no success:

我的.vscode/settings.json:

{
    "python.pythonPath": "~/.virtualenvs/evernote/bin/python",
    "python.linting.pylintPath": "~/.virtualenvs/evernote/bin/pylint"
}

推荐答案

看来我不得不在 .env 文件中使用冒号而不是分号,如下所示:PYTHONPATH="~/.virtualenvs/evernote/bin/python:lib".这似乎解决了问题.

It seems that I had to use a colon instead of a semicolon in .env file like so: PYTHONPATH="~/.virtualenvs/evernote/bin/python:lib". That seems to solve the problem.

这篇关于VS Code - pylinter 找不到模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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