前言

如题。本文给出解决方案。

问题场景

  • Ubuntu
  • VSCode
  • Anaconda env
  • pylint

首先,我在VSCode中打开了一个python project,然后给这个project指派了一个python环境(解释器interpreter),然后在终端pip install pylint,由于我的Python文件aaa.py中含有from module_xxx import method_xxx,这个module_xxx 是自定义的模块,所以pylint直接报错说unable to import module_xxx,而实际上这个module_xxx就和aaa.py在一个文件夹下。

原因

不在VSCode,而在于pylint。它是静态的分析,无法找到module_xxx,(可能是路径设定的原因),所以自然报错了。

解决方案

在该project目录下有个.vscode文件夹,里面有settings.json文件,原内容如下:

{
    "python.pythonPath": "/home/apr/anaconda3/envs/api_misuse/bin/python"
}

修改内容如下:

{
    "python.pythonPath": "/home/apr/anaconda3/envs/api_misuse/bin/python",
    // "terminal.integrated.env.osx": {"PYTHONPATH": "${workspaceFolder}"},
    "python.linting.pylintArgs": [
        // "--disable=F0401"
        "--disable=E0401"
    ] 
}

这样就可以了,此时已经不报错了。

“强迫症”也好了。

小结

走了很多弯路,所以才在这里记。
很多失败的尝试,比如:

  • 修改VSCode的setting (UI)
  • 去VSCode command pallette中找linting相关的操作,onoffrun linting之类的,没用
  • 看各种参考网页,越看越乱,没有人讲清楚其中原因
  • 配置$PYTHONPATH. python.pythonPath
  • 去找~/.pylintrc,没找到,没有。

等等。

最后找到关键词:pylint Unable to import,然后看了几个网页,就大概懂了。

参考文献

反思:自己找文献的时候一定不能盲目找,要想清楚,想好关键字,每看一处,不要急着试,琢磨明白了再试。

  1. https://stackoverflow.com/questions/53653083/how-to-correctly-set-pythonpath-for-visual-studio-code 这个是主要参考。
  2. https://stackoverflow.com/questions/1899436/pylint-unable-to-import-error-how-to-set-pythonpath
  3. http://wgundamj44.github.io/post/2015-10-23-pylint-import-error-in-django/
  4. https://exceptionshub.com/pylint-unable-to-import-error-how-to-set-pythonpath.html
  5. https://github.com/Microsoft/vscode-python/issues/1185
  6. https://stackoverflow.com/questions/37096364/python-importerror-cannot-import-name-utils
  7. https://www.xspdf.com/resolution/53624143.html
  8. https://stackoverflow.com/questions/59702230/how-to-correctly-import-a-python-module-in-vs-code
  9. https://stackoverflow.com/questions/50777849/from-conda-create-requirements-txt-for-pip3
  10. https://code.visualstudio.com/docs/python/linting
  11. https://stackoverflow.com/questions/53939751/pylint-unresolved-import-error-in-visual-studio-code
  12. https://github.com/microsoft/python-language-server/blob/master/TROUBLESHOOTING.md#unresolved-import-warnings
  13. https://github.com/microsoft/python-language-server/issues/1085#issuecomment-492919382
  14. https://stackoverflow.com/questions/55726180/pylint-not-running-as-expected-in-vscode
  15. https://www.askpython.com/python-modules/python-system-command
  16. https://blog.csdn.net/sinat_24527463/article/details/104254652
  17. https://blog.csdn.net/weixin_33716557/article/details/93275107
  18. https://blog.csdn.net/qq_23009105/article/details/106375341
  19. https://blog.csdn.net/zj010206/article/details/92806700
  20. https://code.visualstudio.com/docs/python/debugging
  21. https://stackoverflow.com/questions/55726180/pylint-not-running-as-expected-in-vscode
  22. https://stackoverflow.com/questions/62397982/vs-code-pylint-is-not-running
  23. https://stackoverflow.com/questions/50300785/vs-code-linter-pylint-not-installed-pip-install-pylint-not-working
  24. https://code.visualstudio.com/docs/python/linting
  25. https://www.jianshu.com/p/56ab5d6a95c8?utm_campaign=haruki
Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐